简体   繁体   English

如何在 ios 中关闭视图控制器?

[英]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:我在 viewWillAppear 方法中从服务器下载数据,下载有一个这样的委托方法:

-(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:如果您有 VC1 呈现 VC2,那么在 VC2 内部的相应事件(比如关闭按钮点击,或服务器错误等),您应该调用:

[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:如果您以这样一种方式设置事物,即 VC1 会收到有关 VC2 内部发生的事件的通知,您可以使用:

[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.然而,第一种方法更受欢迎,因为它是一种更好的设计实践,并且有助于 VC1 和 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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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