简体   繁体   中英

Closing a view controller from a another view controller

I am very new to iPhone application development.
I am trying to close/remove one view from another view but some how it is not working for me.

How should i close/remove view controller from a another view controller.

There are two views A and B .
View A is opening view B using following code.

ViewBScreenController *mViewB = [[ViewBScreenController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:mViewB animated:YES];

But some how i am not able to close view B from view controller A.
How to do it?

----UPDATE----

As asked in the answers why i need to close view B from view A, as i am getting events from underneath application layer to view controller A and on some events i want to close view B from view A.

Also is it possible to send some events to view B to close itself.

Thanks.

in A

- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(XXXXX)        name:@"RRRR" object:nil];
}

-(void)XXXXX{

/* any method  you wont */
[self dismissModalViewControllerAnimated:YES];
//////////////////////////

}

in B or C or D etc....

-(IBAction)Btn:(id)sender{

[[NSNotificationCenter defaultCenter] postNotificationName:@"RRRR" object:nil];

}

its will do the trick

The method you are using is deprecated, use this instead:

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;  

With this you present B from A.
When you want back to A, you do the opposite: you present A from B.
How? There are several ways, for example with an observer notification.

Try to read this amazing documentation about view controller programming guide: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457-CH1-SW1

It will answers all of your questions.

You are looking for dismissViewControllerAnimated:completion:

The Docs are here:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

Generally you should have the view controller that presented the modal view controller dismiss it, but it can also dismiss itself. The message will just be automatically forwarded to the VC that presented it. I'd do it the less "magical" way:

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

Why would you dismiss View B from View A if the user is at View B. Since it doesn't matter where the close action is coming from, why not simply dismiss View B with a simple dismissModalViewController When the action to close View B comes in simply input

 - (IBAction)goBack:(id)sender {
    [self dismissModalViewControllerAnimated:YES];

}

Therefore using this code, you should be able to dismiss a modal view controller with no issues. And it can be dismissed from either view, but if View B is present then the user can not interact with buttons or create an action on view A.

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