Swift 从闭包中显示视图控制器
我有一个闭包作为添加到UIAlertController的项的处理程序。我按预期在闭包内收到id值。(我在片段中没有使用它) 但我的问题是,我想切换到另一个视图控制器。但我是在闭包时打这个电话的 我得到以下错误: 类型“(ChooseProfile)->()->(ChooseProfile)”的Swift 从闭包中显示视图控制器,swift,Swift,我有一个闭包作为添加到UIAlertController的项的处理程序。我按预期在闭包内收到id值。(我在片段中没有使用它) 但我的问题是,我想切换到另一个视图控制器。但我是在闭包时打这个电话的 我得到以下错误: 类型“(ChooseProfile)->()->(ChooseProfile)”的值没有成员“存在”' 如何从闭包中切换到另一个视图控制器 class ChooseProfile: UIViewController { let closure = { (id: String)
值没有成员“存在”
'
如何从闭包中切换到另一个视图控制器
class ChooseProfile: UIViewController {
let closure = { (id: String) in { (action: UIAlertAction!) -> Void in
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "test") as! UIViewController
self.present(nextViewController, animated:true, completion:nil)
}}
}
我之所以使用此闭包,是因为:
override func viewDidAppear(_ animated: Bool) {
let alert = UIAlertController(title: "Choose a Device", message: "Which device would you like to use in this app?", preferredStyle: UIAlertControllerStyle.alert)
for dev in (DataSingleton.instance.getDevices())! {
alert.addAction(UIAlertAction(title: dev.getName(), style: UIAlertActionStyle.default, handler: closure(dev.getId())))
}
alert.addAction(UIAlertAction(title: "Add a new Profile", style: UIAlertActionStyle.destructive, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
}
我根据设备列表添加警报操作。我想在单击设备时检索id。问题是您在闭包中使用self,但self没有引用VC实例,因为它尚未完全创建。相反,为什么不在调用闭包时将viewController传递给闭包呢
class ChooseProfile: UIViewController {
let closure = { (id: String, vc: UIViewController) in { [weak vc] (action: UIAlertAction!) -> Void in
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "test")
vc?.present(nextViewController, animated:true, completion:nil)
}}
}
override func viewDidAppear(_ animated: Bool) {
let alert = UIAlertController(title: "Choose a Device", message: "Which device would you like to use in this app?", preferredStyle: UIAlertControllerStyle.alert)
for dev in (DataSingleton.instance.getDevices())! {
alert.addAction(UIAlertAction(title: dev.getName(), style: UIAlertActionStyle.default, handler: closure(dev.getId(), self)))
}
alert.addAction(UIAlertAction(title: "Add a new Profile", style: UIAlertActionStyle.destructive, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
}
问题是你在闭包中使用self,但是self并没有引用你的VC实例,因为它还没有完全创建。相反,为什么不在调用闭包时将viewController传递给闭包呢
class ChooseProfile: UIViewController {
let closure = { (id: String, vc: UIViewController) in { [weak vc] (action: UIAlertAction!) -> Void in
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "test")
vc?.present(nextViewController, animated:true, completion:nil)
}}
}
override func viewDidAppear(_ animated: Bool) {
let alert = UIAlertController(title: "Choose a Device", message: "Which device would you like to use in this app?", preferredStyle: UIAlertControllerStyle.alert)
for dev in (DataSingleton.instance.getDevices())! {
alert.addAction(UIAlertAction(title: dev.getName(), style: UIAlertActionStyle.default, handler: closure(dev.getId(), self)))
}
alert.addAction(UIAlertAction(title: "Add a new Profile", style: UIAlertActionStyle.destructive, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
}
你在哪里声明这个闭包?@vacawama我已经编辑了我的问题闭包是你的VC的属性吗?@vacawama我声明它就像上面代码中所示。所以是的,我认为它是一个属性。你展示你是如何声明它的,但不是在哪里。它是你VC的顶级吗?你在哪里声明这个闭包?@vacawama我已经编辑了我的问题闭包是你VC的属性吗?@vacawama我声明它就像上面的代码一样。所以是的,我认为它是一个属性。你展示你是如何声明它的,但不是在哪里。这是你的VC的最高级别吗?