【问题标题】:Swift how to update label in previous view controllerSwift如何更新先前视图控制器中的标签
【发布时间】:2016-05-05 01:16:06
【问题描述】:

当在 AccountViewController 中按下后退按钮时,我想从我的 AccountViewController 更新我的 DashboardViewController 中的标签。

我尝试将变量从第二个视图传回给第一个视图并更新 viewDidLoad 和 viewWillAppear 中的标签,但是当第一个视图返回屏幕时它永远不会更新标签。

我尝试在第一个视图中创建一个函数,以使用传递给函数的字符串更新标签并从第二个视图调用该函数,但它说标签为 nil,因此无法更新。

我最近的尝试是创建一个委托,但也没有用。

这是我的委托尝试。

 class DashboardViewController: UIViewController, AccountViewControllerDelegate {
 @IBOutlet weak var welcome_lbl: UILabel!

    func nameChanged(name: String){
    var full_name = "Welcome \(name)"
    welcome_lbl.text = "\(full_name)"
}
    override func viewDidLoad() {
    super.viewDidLoad()

    AccountViewController.delegate = self
}
}

然后在我的 AccountViewController 我有这个

protocol AccountViewControllerDelegate{
func name_changed(name: String)
}

class AccountViewController: UIViewController, UITextFieldDelegate {
   var info_changed = false
static var delegate: AccountViewControllerDelegate!
    @IBAction func back_btn(sender: AnyObject) {
    if(info_changed){
        AccountViewController.delegate.name_changed(name_tf.text!)
    }
    self.dismissViewControllerAnimated(true, completion: nil)
}

我是否以某种方式搞砸了委托流程?或者有没有更简单的方法来做到这一点?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    首先。您的委托应该是 AccountViewController 的正常属性。当用户回击时,无需更新您的姓名。当用户在 AccountViewController 中更改名称时,您可以更改 DashboardViewController 的名称。当用户返回 DashboardViewController 时。它已经显示了更改后的名称。

    protocol AccountViewControllerDelegate{
        func name_changed(name: String)
    }
    
    class AccountViewController: UIViewController, UITextFieldDelegate {
    
      var delegate: AccountViewControllerDelegate?
    
        // when user change name through textfield or other control
        func changeName(name: String) {
            delegate?.name_changed(name)
        }
    
    }
    

    第二。当 DashboardViewController 显示 AccountViewController。我觉得应该是推。将 DashboardViewController 实例设置为 AccountViewController 实例的委托。

    class DashboardViewController: UIViewController, AccountViewControllerDelegate {
        @IBOutlet weak var welcome_lbl: UILabel!
    
        func nameChanged(name: String){
        var full_name = "Welcome \(name)"
        welcome_lbl.text = "\(full_name)"
        }
    
        // present or push to AccountViewController
        func showAccountViewController {
            let accountViewController = AccountViewController()
            accountViewController.delegate = self
            // do push view controller
        }
    }
    

    【讨论】:

    • 推送视图控制器是什么意思?
    • 不知道怎么把 DashboardViewController 拿到 AccountViewController。它应该像下面的代码:self.navigationController?.pushViewController(accountViewController, animated: true)
    • 我没有使用导航控制器。它们是由 segue 连接的两个独立的视图控制器。
    • @MoCode 你可以在 DashboardViewController 中覆盖- prepareForSegue:sender: 来设置委托
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2021-08-09
    • 2015-10-25
    • 1970-01-01
    • 2017-11-26
    • 2011-05-10
    相关资源
    最近更新 更多