简体   繁体   中英

How to dismiss viewcontroller in ios?

I created a library, and if the main app call my library its showing it, and download some data from server. But if the server has some error I would like to kill the library view, but it's not working I have a delegate in the host app:

-(void)libraryResult:(NSString*)result{
NSLog(@"result: %@", result);
}

And I download data from server in the viewWillAppear method, and the download has a delegate method like this:

-(void)networkManagerError:(NSString *)error{
[hud hide:YES];
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
[self.delegate libraryResult:error];
}

I see in the log, that the app return to the main app, but the view don't change. How to solve this? Whats wrong with my code?

Change this line

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

To

[self dismissViewControllerAnimated:YES completion:nil];

试试这些..

[self dismissViewControllerAnimated:YES completion:nil];

If you have VC1 which presents VC2, then inside VC2 at the corresponding event (say a Close button tap, or error from server etc.), you should call:

[self.presentingViewController dismissViewControllerAnimated:YES completion:^{

    }]

If you set up things in such a way that VC1 is notified about those events that happen inside VC2, you can use:

[self dismissViewControllerAnimated:YES completion:nil];

However, the 1st method is more preferred, as it's a better design practice, and contributes to more loose coupling between VC1 and VC2.

Try to fire a notification from your "library result" method if error occurred and add an observer to your current controller view.

[[NSNotificationCenter defaultCenter] postNotificationName:Remove_CurrentView object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remove) name:Remove_CurrentView object:nil];

-(void)remove{
    [self dismissViewControllerAnimated:YES completion:nil];
}

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