当我登录到我的应用程序时,我将删除两个显示为模态的 VC,然后添加一个新的 VC 并通过以下方式使其成为根 VC:
@IBAction func loginButtonDidTouch(sender: AnyObject) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let AccountVc = storyboard.instantiateViewControllerWithIdentifier("AccountVc")
let navigationController = self.view.window?.rootViewController as! UINavigationController
self.presentingViewController!.presentingViewController!.dismissViewControllerAnimated(false) { () -> Void in
navigationController.setViewControllers([AccountVc], animated: true)
}
}
我想确保 navigationController.setViewControllers([AccountVc], animated: true)
运行一次 self.presentingViewController!.presentingViewController!.dismissViewControllerAnimated(false)
完成了。
它可以工作,但我不确定这是否是正确的写法?
我在写:{ () -> Void in
我看到一些例子,人们只写它而没有 Void。
最佳答案
两种方式都是对的。这称为 Trailing Closures
。
func someFunctionThatTakesAClosure(closure: () -> Void) {
// function body goes here
}
// here's how you call this function without using a trailing closure:
someFunctionThatTakesAClosure({
// closure's body goes here
})
// here's how you call this function with a trailing closure instead:
someFunctionThatTakesAClosure() {
// trailing closure's body goes here
}
查看更多信息 Apple Document
关于ios - Swift ios 不确定我是否正确使用完成处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33403455/