调用函数将 UIViewController 推入闭包内部,就好像它在外面一样



我在另外两个闭包(entireDescView.customize(第二个闭包(和description(第一个闭包((中有一个闭包(handleMentionTap(。现在我想调用位于同一类中的函数pushProfileController(of: String)(为了推送另一个视图控制器(newController(。这些函数需要在闭包内部调用,因为在闭包外部调用它们会导致内存泄漏,原因我目前不知道(我正在使用一个名为 ActiveLabel 的库,不是自己编写的(。但是,newController 没有被推送(即使我按照有关类似问题的堆栈溢出的一些答案中的建议使用DispatchQueue.main.async(。尽管如此,newController似乎已创建及其数据集(我正在使用var passedData: String? { didSet { print(passedData) } }打印数据(。如何推动控制器,就好像它在闭合之外一样(例如,用self.pushProfileController(of: username)?我真的很感激你的帮助!

斯威夫特(每个在同一个类中(:

var pushingViewController = false
//...
func pushProfileController(of: String) {
if self.pushingViewController == false {
self.pushingViewController = true
DispatchQueue.main.async {
let newController = NewController()
newController.passedData = of //passedData is a string
self.navigationController?.pushViewController(newController, animated: true)
}
self.pushingViewController = false
}
}
let description: ActiveLabel = {
let entireDescView = ActiveLabel()
//set properties of controls container view
//recognize (@hotel_x) and be able to push controller of 'hotel_x'
entireDescView.customize({ (entireDescView) in
entireDescView.enabledTypes = [.mention]
entireDescView.mentionColor = UIColor(red: 25/255, green: 153/255, blue: 1, alpha: 1)
entireDescView.handleMentionTap { username in
self.pushProfileController(of: username) //also tried ThisClass().pushProfileController(of: username) which didn't push the controller either
}
})
return entireDescView
}()

你检查过navigationController?存在吗?

在尝试推送视图控制器之前,请尝试添加以下内容:

func pushProfileController(of: String) {
if self.pushingViewController == false {
self.pushingViewController = true
DispatchQueue.main.async {
guard let navController = self.navigationController else { return print("navigationController was nil") }
let newController = NewController()
newController.passedData = of //passedData is a string
navController.pushViewController(newController, animated: true)
}
self.pushingViewController = false
}
}

最新更新