繁体   English   中英

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

[英]How to dismiss viewcontroller in ios?

我创建了一个库,如果主应用程序调用我的库,它会显示它,并从服务器下载一些数据。 但是如果服务器有一些错误,我想终止库视图,但它不起作用我在主机应用程序中有一个委托:

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

我在 viewWillAppear 方法中从服务器下载数据,下载有一个这样的委托方法:

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

我在日志中看到,应用程序返回到主应用程序,但视图没有改变。 如何解决这个问题? 我的代码有什么问题?

改变这一行

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

[self dismissViewControllerAnimated:YES completion:nil];

试试这些..

[self dismissViewControllerAnimated:YES completion:nil];

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

[self.presentingViewController dismissViewControllerAnimated:YES completion:^{

    }]

如果您以这样一种方式设置事物,即 VC1 会收到有关 VC2 内部发生的事件的通知,您可以使用:

[self dismissViewControllerAnimated:YES completion:nil];

然而,第一种方法更受欢迎,因为它是一种更好的设计实践,并且有助于 VC1 和 VC2 之间更松散的耦合。

如果发生错误,请尝试从“库结果”方法发出通知,并将观察者添加到当前控制器视图中。

[[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