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

标签 ios swift

当我登录到我的应用程序时,我将删除两个显示为模态的 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/

相关文章:

ios - 使用自定义后退按钮后导航标题消失

ios - 如何修复 faSTLane-plugin-firebase_app_distribution 未定义

swift - tvOS AVPlayerItem 外部艺术品元数据大小

使用 CAShapeLayer() 和 UIBezierPath 快速绘制和调整圆

ios - 转换之间的奇怪延迟(UIViewController -> UIViewController)

ios - 从导航 Controller 中弹出多个 Controller 为零?

ios - 在用户拖动 map 时获取 MKMapView 回调?

ios - 是否可以创建多行 UISegmentedControl?

ios - 警告 : "implicit declaration of function ' .。 .' is invalid in C99"

swift :#warning等价物