当我登录我的应用程序时,我将删除两个以模态形式呈现的VC,然后添加一个新VC并将其设为root 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的情况下编写它。
答案 0 :(得分:2)
两种方式都是正确的。这称为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
}
了解详情