gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:43 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com