在swift上从另一个viewcontroller更改标签

时间:2021-01-13 16:03:14

I want to change label from another viewController.

我想从另一个viewController更改标签。

First viewcontroller is MenuController. Second one is LoginViewController.

第一个viewcontroller是MenuController。第二个是LoginViewController。

I want to change MenuController's Label.text from LoginViewController.

我想从LoginViewController更改MenuController的Label.text。

In LoginViewController:

在LoginViewController中:

let viewController = MenuController()
viewController.changeLabel("logout")

In MenuController:

在MenuController中:

class MenuController: UITableViewController {
    var attractionImages = [String]()
    var attractionNames = [String]()
    var webAddresses = [String]()

    @IBOutlet weak var loginLabel: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

        loginLabel.text = "Login"
        print(loginLabel.text)

    }

    func changeLabel(Log: String)O {
        self.loginLabel.text = log
        print (log)
    }

But an error occur.

但是会发生错误。

fatal error: unexpectedly found nil while unwrapping an Optional value

致命错误:在展开Optional值时意外发现nil

How can I solve it?

我该如何解决?

Thanks for your help.

谢谢你的帮助。

2 个解决方案

#1


11  

Another way to achieve that is you can use NSNotificationCenter. Blow is the example for that:

另一种实现方法是使用NSNotificationCenter。打击就是这样的例子:

In your MenuController add this code:

在您的MenuController中添加以下代码:

override func viewDidLoad() {
    super.viewDidLoad()
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshLbl:", name: "refresh", object: nil)
}

Also add this helper method:

还要添加此辅助方法:

func refreshLbl(notification: NSNotification) {

    print("Received Notification")
    lbl.text = "LogOut"
}

Now in your LoginViewController your back button action will look like:

现在在您的LoginViewController中,您的后退按钮操作将如下所示:

@IBAction func back(sender: AnyObject) {
    NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil, userInfo: nil)
    self.dismissViewControllerAnimated(true, completion: nil)
}

Now when ever you press back button from LoginViewController your refreshLbl method will call from MenuController.

现在,当您从LoginViewController按下后退按钮时,您的refreshLbl方法将从MenuController调用。

For more info refer THIS example.

有关详细信息,请参阅此示例。

#2


3  

Swift 3 version:

Swift 3版本:

In your MenuController (where the label needs to be changed) add this code:

在您的MenuController(需要更改标签的位置)中添加以下代码:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, 
                                           selector: #selector(refreshLbl),
                                           name: NSNotification.Name(rawValue: "refresh"),
                                           object: nil)
}

Also add this helper method:

还要添加此辅助方法:

func refreshLbl() {
    print("Received Notification")
    lbl.text = "LogOut"
}

Now in your LoginViewController your back button action will look like:

现在在您的LoginViewController中,您的后退按钮操作将如下所示:

@IBAction func backButton(_ sender: Any) {
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refresh"), object: nil)
    // Any additional code...
}

Now when ever you press back button from LoginViewController your refreshLbl() method will call from MenuController.

现在,当您从LoginViewController按下后退按钮时,您的refreshLbl()方法将从MenuController调用。

#1


11  

Another way to achieve that is you can use NSNotificationCenter. Blow is the example for that:

另一种实现方法是使用NSNotificationCenter。打击就是这样的例子:

In your MenuController add this code:

在您的MenuController中添加以下代码:

override func viewDidLoad() {
    super.viewDidLoad()
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshLbl:", name: "refresh", object: nil)
}

Also add this helper method:

还要添加此辅助方法:

func refreshLbl(notification: NSNotification) {

    print("Received Notification")
    lbl.text = "LogOut"
}

Now in your LoginViewController your back button action will look like:

现在在您的LoginViewController中,您的后退按钮操作将如下所示:

@IBAction func back(sender: AnyObject) {
    NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil, userInfo: nil)
    self.dismissViewControllerAnimated(true, completion: nil)
}

Now when ever you press back button from LoginViewController your refreshLbl method will call from MenuController.

现在,当您从LoginViewController按下后退按钮时,您的refreshLbl方法将从MenuController调用。

For more info refer THIS example.

有关详细信息,请参阅此示例。

#2


3  

Swift 3 version:

Swift 3版本:

In your MenuController (where the label needs to be changed) add this code:

在您的MenuController(需要更改标签的位置)中添加以下代码:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, 
                                           selector: #selector(refreshLbl),
                                           name: NSNotification.Name(rawValue: "refresh"),
                                           object: nil)
}

Also add this helper method:

还要添加此辅助方法:

func refreshLbl() {
    print("Received Notification")
    lbl.text = "LogOut"
}

Now in your LoginViewController your back button action will look like:

现在在您的LoginViewController中,您的后退按钮操作将如下所示:

@IBAction func backButton(_ sender: Any) {
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refresh"), object: nil)
    // Any additional code...
}

Now when ever you press back button from LoginViewController your refreshLbl() method will call from MenuController.

现在,当您从LoginViewController按下后退按钮时,您的refreshLbl()方法将从MenuController调用。