故事板看起来像这样NavigationController -> HomeScreenVC -> LogInVC(模态)当我的登录成功时,我想关闭LogInVC并推送MyAccountVC我做了一个委托,当LogInVC被关闭时宣布HomeScreenVC,但我的推送不起作用,我会让下面的代码:
func logInSucceded() {
print("delegate123")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "MyAccountViewController") as! MyAccountViewController
navigationController?.pushViewController(viewController, animated: true)
}
@IBAction func loginbutton(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "MyAccountViewController") as! MyAccountViewController
navigationController?.pushViewController(viewController, animated: true)
}
我试着做一个直接的uibutton(在HomeScreenVC中)作为测试,如果我正确地编写了我的push,并且从按钮它可以工作,但是logInSucceded()不能。打印出现在控制台,所以我猜委托设置正确。有什么想法吗?谢谢!
1条答案
按热度按时间m4pnthwp1#
在LogInVC中创建委托协议
在LogInVC中,声明弱委托属性
登录成功后,调用delegate方法并关闭LogInVC
在HomeScreenVC中,遵循LogInDelegate协议,实现delegate方法
最后,在HomeScreenVC中,当呈现LogInVC时,将HomeScreenVC设置为delegate:
使用这种方法,当登录在LogInVC中成功时,它将调用delegate方法,该方法反过来将解除LogInVC并从HomeScreenVC推送MyAccountVC。