问题
I had a UIViewController
and a UIContainer
View.
I want to simulate the UIAlertView
effect, But I don't know how to build the Container View show and hide about the great method.
I had put the UITableView
in my UIContainer
View, I use delegate to send selected item to the UIViewController
(ParentViewController
) and show in the label.( segue name with alertSegue
)
There have a show button in the ParentViewController
,and I need click the selected item ,it will close(hide/dismiss?) the UIContainer
View.
Now UIContainer
View default is hidden,and storybaord screen shot like below:
My ParentViewController.h
@interface ViewController : UIViewController<ContainerViewBLETableDelegate>
@property (weak, nonatomic) IBOutlet UIButton *btn;
- (IBAction)btnAction:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *lb;
@property (weak, nonatomic) IBOutlet UIView *containerView;
-(IBAction)unwindSegue:(UIStoryboardSegue *)segue;
@end
.m file:
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)btnAction:(id)sender {
_containerView.hidden = NO;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if( [segue.identifier isEqualToString:@"alertSegue"])
{
ContainerViewBLETable *vc = segue.destinationViewController;
vc.delegate = self;
}
}
-(void) ContainerViewBLETable:(ContainerViewBLETable *)vc andSelectedDone:(NSString *)selectedStr
{
self.lb.text = selectedStr;
vc.view.hidden = YES;
}
-(IBAction)unwindSegue:(UIStoryboardSegue *)segue
{
}
Container ViewController .h :
@class ContainerViewBLETable;
@protocol ContainerViewBLETableDelegate <NSObject>
-(void) ContainerViewBLETable:(ContainerViewBLETable*)vc andSelectedDone:(NSString*)selectedStr;
@end
@interface ContainerViewBLETable : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,assign) id<ContainerViewBLETableDelegate>delegate;
@property (weak, nonatomic) IBOutlet UITableView *tableVW;
@end
UIContainerView .m part:
.....
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.delegate ContainerViewBLETable:self andSelectedDone:[NSString stringWithFormat:@"%ld",indexPath.row]];
}
.....
Although this code can pass the value to the UIViewController
(ParentViewController
),and the UIContainer
View was hidden,but when I click the show button again , it was not any effect(I feel the container view was overlap lead to touch lose efficacy).
( by the way, I had try
[self.parentViewController dismissViewControllerAnimated:YES completion:nil];
it's not effect )
Question:
How to hide the container view use the delegate and that can touch the show button show container again?
And What great method to control the UIContainer
view show and disappear in ParentViewController
and UIContainerView
?
I really thank everyone and you can help me and better than better.
回答1:
containerView
and show button
are in different viewControllers. So you can't just hide it. The simple way to achieve it is when you select the cell in containerView
, present the ParentViewController
, and the view in ContainerViewController
will dismiss automatically.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.delegate ContainerViewBLETable:self andSelectedDone:[NSString stringWithFormat:@"%ld",indexPath.row]];
ParentViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"ParentViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
To show the container, you can just dismiss the ParentViewController
that presenting.
1.
- (IBAction)btnAction:(id)sender
{
[self.presentingViewController dismissModalViewControllerAnimated:YES];
}
Or 2. Set up segue to dismiss viewController and use prepareForSegue for delegate to communicate with other viewController.
Also REMOVE vc.view.hidden = YES
in delegate you had implemented
-(void) ContainerViewBLETable:(ContainerViewBLETable *)vc andSelectedDone:(NSString *)selectedStr.
来源:https://stackoverflow.com/questions/28823019/ios-how-to-control-the-uicontainer-view-show-and-disappear