ios presentViewController 的回调方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9518765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Callback method for presentViewController
提问by Milos Pesic
I'm having trouble with presentViewController method and its last parameter.
我在使用presentViewController 方法及其最后一个参数时遇到了问题。
[self presentViewController:navigationController animated:YES completion:nil];
I'm new in objective-c syntax and can't find out what object should I pass to 'completion' parameter. (also didn't find any examples that use it)
我是objective-c语法的新手,无法找出应该将什么对象传递给“完成”参数。(也没有找到任何使用它的例子)
I want to have callback method when my presented View Controller dismisses.
当我呈现的视图控制器关闭时,我想要回调方法。
Thanks,
谢谢,
Milos
米洛斯
回答by Aymarick
Example of creating a completion block:
创建完成块的示例:
[self presentViewController:navigationController
animated:YES
completion:^(){
//put your code here
}];
This block does not take parameters. other blocks may take parameters and you can define them like this example:
此块不带参数。其他块可能需要参数,你可以像这个例子一样定义它们:
^(BOOL bFinished){
//put your code here
}
回答by jrturton
This method isn't going to give you what you want. The completion block is for when the view controller has completed presenting, not when it is dismissed. You'll need to use a different pattern, such as delegation, to get a callback when the controller is dismissed.
这种方法不会给你你想要的。完成块用于当视图控制器完成呈现时,而不是当它被解除时。您需要使用不同的模式(例如委托)来在控制器关闭时获得回调。
The completion handler is called after the viewDidAppear: method is called on the presented view controller.
在呈现的视图控制器上调用 viewDidAppear: 方法后,将调用完成处理程序。
回答by Alexander
That's a place for a block
.
那是一个地方block
。
The completion handler is called after the viewDidAppear: method is called on the presented view controller.
在呈现的视图控制器上调用 viewDidAppear: 方法后,将调用完成处理程序。
For operations on dismissal you could place your code in the viewWillDisappear:
or viewDidDisappear:
method.
对于解雇操作,您可以将代码放在viewWillDisappear:
orviewDidDisappear:
方法中。
回答by Jan Ehrhardt
For anyone still reading this after all these years, I wanted to alert the user when an error happened inside a UIImagePickerController. Solution
对于这么多年后仍在阅读本文的任何人,我想在 UIImagePickerController 中发生错误时提醒用户。解决方案
if (error condition) {
[self dismissViewControllerAnimated:YES completion:^(void) {
dispatch_async(dispatch_get_main_queue(), ^{
// Alert the user here, for instance with a UIAlertController
});
}];
}