2016-05-05 1 views
0

AccountViewControllerで戻るボタンを押すと、自分のDashboardViewControllerのラベルを自分のAccountViewControllerから更新したいと考えています。前のビューコントローラでラベルを更新する方法をスチューデント

2番目のビューから1番目のビューに変数を渡して、viewDidLoadとviewWillAppearでラベルを更新しようとしましたが、最初のビューが画面に戻ったときにラベルを更新しません。

関数に渡された文字列でラベルを更新し、その関数を2番目のビューから呼び出すことを試みましたが、ラベルが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) 
} 

を持っているが何とか委任プロセスをI混乱をしましたか?またはこれを行う簡単な方法はありますか?

答えて

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) 
    } 

} 

第2。 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 
    } 
} 
+0

プッシュビューコントローラとはどういう意味ですか? –

+0

DashboardViewControllerをどのようにAccountViewControllerに取得するのか分からない。それは次のコードのようにする必要があります: 'self.navigationController?.pushViewController(accountViewController、animated:true)' –

+0

私はナビゲーションコントローラを使用していません。それらは、セグで接続された2つの別々のView Controllerです。 –

関連する問題