英文:
Pushing ViewController works by button, but not with delegate
问题
The storyboard looks like this
NavigationController -> HomeScreenVC -> LogInVC (modally)
When my logIn is successful I want to dismiss LogInVC and push MyAccountVC
I made a delegate to announce HomeScreenVC when LogInVC is dismissed, but my push does not work
I'll let the code below:
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)
}
I tried to make a direct uibutton(in HomeScreenVC) as a test if I write my push correctly and from the button it works, but logInSucceded() does not.
The print appears in console so I guess the delegate is set up correctly.
Any ideas? Thanks!
英文:
The storyboard looks like this
NavigationController -> HomeScreenVC -> LogInVC (modally)
When my logIn is successful I want to dismiss LogInVC and push MyAccountVC
I made a delegate to announce HomeScreenVC when LogInVC is dismissed, but my push does not work
I'll let the code below:
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)
}
I tried to make a direct uibutton(in HomeScreenVC) as a test if I write my push correctly and from the button it works, but logInSucceded() does not.
The print appears in console so I guess the delegate is set up correctly.
Any ideas? Thanks!
答案1
得分: 1
Create a delegate protocol in your LogInVC:
在你的 LogInVC 中创建一个委托协议:
protocol LogInDelegate: AnyObject {
func logInSucceeded()
}
In LogInVC, declare a weak delegate property:
在 LogInVC 中声明一个弱引用的委托属性:
weak var delegate: LogInDelegate?
When the login is successful, call the delegate method and dismiss LogInVC:
当登录成功时,调用委托方法并关闭 LogInVC:
func logInSucceeded() {
delegate?.logInSucceeded()
dismiss(animated: true, completion: nil)
}
In HomeScreenVC, conform to the LogInDelegate protocol and implement the delegate method:
在 HomeScreenVC 中,遵循 LogInDelegate 协议并实现委托方法:
extension HomeScreenVC: LogInDelegate {
func logInSucceeded() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "MyAccountViewController") as! MyAccountViewController
navigationController?.pushViewController(viewController, animated: true)
}
}
Finally, in HomeScreenVC, when presenting the LogInVC, set HomeScreenVC as the delegate:
最后,在 HomeScreenVC 中,在呈现 LogInVC 时,将 HomeScreenVC 设置为委托:
@IBAction func loginButton(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = storyboard.instantiateViewController(withIdentifier: "LogInViewController") as! LogInViewController
loginVC.delegate = self
present(loginVC, animated: true, completion: nil)
}
With this approach, when the login is successful in LogInVC, it will call the delegate method, which in turn will dismiss LogInVC and push MyAccountVC from HomeScreenVC.
英文:
Create a delegate protocol in your LogInVC
protocol LogInDelegate: AnyObject {
func logInSucceeded()
}
In LogInVC, declare a weak delegate property
weak var delegate: LogInDelegate?
When the login is successful, call the delegate method and dismiss LogInVC
func logInSucceeded() {
delegate?.logInSucceeded()
dismiss(animated: true, completion: nil)
}
In HomeScreenVC, conform to the LogInDelegate protocol and implement the delegate method
extension HomeScreenVC: LogInDelegate {
func logInSucceeded() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "MyAccountViewController") as! MyAccountViewController
navigationController?.pushViewController(viewController, animated: true)
}
}
Finally, in HomeScreenVC, when presenting the LogInVC, set HomeScreenVC as the delegate:
@IBAction func loginButton(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = storyboard.instantiateViewController(withIdentifier: "LogInViewController") as! LogInViewController
loginVC.delegate = self
present(loginVC, animated: true, completion: nil)
}
With this approach, when the login is successful in LogInVC, it will call the delegate method, which in turn will dismiss LogInVC and push MyAccountVC from HomeScreenVC.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论