调用函数将 UIViewController 推入闭包内,就好像它在外面一样
Posted
技术标签:
【中文标题】调用函数将 UIViewController 推入闭包内,就好像它在外面一样【英文标题】:Call function pushing UIViewController inside closure as though it were outside 【发布时间】:2017-07-26 07:03:02 【问题描述】:我在另外两个闭包(entireDescView.customize
(第二个闭包)和description
(第一个闭包))中有一个闭包(handleMentionTap
)。现在我想调用位于同一个类中的函数pushProfileController(of: String)
(为了推送另一个视图控制器(newController
)。这些函数需要在闭包内部调用,因为在外部调用它们会导致内存泄漏我目前不知道的一个原因(我正在使用一个名为ActiveLabel 的库,不是我自己编写的)。但是,newController 没有被推送(即使我使用了DispatchQueue.main.async
,正如关于*** 的一些答案中所建议的那样类似的问题). 不过newController
似乎已创建及其数据集(我正在使用var passedData: String? didSet print(passedData)
打印数据). 我怎样才能推动控制器就像它在关闭之外(例如使用self.pushProfileController(of: username)
?我会非常感谢您的帮助!
swift(同一个类):
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
()
【问题讨论】:
【参考方案1】:你检查过navigationController?
存在吗?
在尝试推送 viewController 之前尝试添加以下内容:
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
【讨论】:
以上是关于调用函数将 UIViewController 推入闭包内,就好像它在外面一样的主要内容,如果未能解决你的问题,请参考以下文章
UINavigationController 不会在推送的 UIViewController 上调用 viewDidLoad