[英]UIAlertAction to pop to root view controller in Swift
I am using Swift programming language and using UIAlertController
with "Cancel" button to display alert. 我正在使用Swift编程语言,并使用带有“取消”按钮的UIAlertController
来显示警报。
I'm using below code to create cancel button. 我正在使用下面的代码来创建取消按钮。
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (alert) -> Void in
self.navigationController?.popToRootViewControllerAnimated(true)
})
alertController.addAction(cancelAction)
On tapping "Cancel" button, user has to be taken back to root view controller. 在点击“取消”按钮时,必须将用户带回到根视图控制器。
But the code is not compiling. 但是代码没有编译。 The error its showing is as below: 其显示的错误如下:
Cannot invoke 'init' with an argument list of type `(title: StringLiteralConvertible, style: UIActionSheetStyle, handler: (($T2) -> Void) -> Void)`
I couldn't understand how to resolve this issue. 我不明白如何解决此问题。
The other answer didn't work for me either; 另一个答案对我也不起作用。 this one did. 这个做到了。 Add this function 添加此功能
func popToRoot() {
self.navigationController?.popToRootViewControllerAnimated(true)
}
Then change the original function 然后改变原来的功能
{ (alert :UIAlertAction!) -> Void in
self.popToRoot()
}
You could try this. 你可以试试看。
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler:
{ action in
self.navigationController?.popToRootViewController(animated: true)
} )
尝试将(警报)替换为:
(alert :UIAlertAction!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.