通过按钮推送视图控制器有效,但使用委托无效。

huangapple go评论63阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月8日 19:28:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定