Swift ios不确定我是否正确使用完成处理程序

时间:2015-10-28 23:38:01

标签: ios swift

当我登录我的应用程序时,我将删除两个以模态形式呈现的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的情况下编写它。

1 个答案:

答案 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
}

Apple Document

了解详情