简体   繁体   中英

How to dismiss parent view from View Controller in Tab Bar Controller?

My app have a main screen ( mainView ). On mainView , I have a button. When user taps it, I use

[self presentViewController:libraryView animated:YES completion:nil]

to present another view ( libraryView ). Then, on libraryView , I use

[self.view addSubview:tabBarController.view]

to add a UITabBarController which has 2 View Controllers: featuredBooks and recentBooks

Everything works fine. But when I add a button to featureBooks to dissmiss the libraryView and return to mainView , the following methods don't work

[self dismissViewControllerAnimated: YES completion:nil]
[self.parentViewController dismissViewControllerAnimated: YES completion:nil]
[self.presentingViewController dismissViewControllerAnimated: YES completion:nil]

I know the reason: self refers to featureBooks 's view, not libraryView .

So, how do I refer to libraryView , dismiss it and return to mainView from a view controller ( featureBooks or recentBooks ) inside Tab Bar Controller?

Thank you very much.

Make mainView delegate object of libraryView... Then when you call the delegate method from libraryView, mainView will call in its code the method dismissViewcController.

So:

1)create into libraryView controller .h code:

@protocol LibraryViewDelegate

      - (void) LibraryViewDelegate_DismissButtonClicked;

@end

2)then create a property into mainView .h file:

@property(nonatomic, assign) NSObject<LibraryViewDelegate> *delegate;

and the following into the .m one

@synthesize delegate;

and assign mainView to this property after libraryView object creation and before you will present it

3)write the following code into the mainview .m file:

-(void)LibraryViewDelegate_DismissButtonClicked{
      //put here the code for dismissing mainView created modalViewController (libraryView)
}

4)then write the code that call:

[self.delegate LibraryViewDelegate_DismissButtonClicked];

into libraryView when you press the dismiss button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM