2017-10-13 79 views
0

在這裏,我有兩個視圖控制器,並在第二視圖控制器按鈕點擊我希望在第一視圖控制器更改標籤的名稱,這樣的按鈕,點擊更改標籤名稱我怎麼能做這個??如何創建委派到一個視圖控制器上的另一個視圖控制器

第一個視圖控制器在這裏我想更改用戶名的標籤文本

class MenuViewController1: UIViewController,UITableViewDataSource, UITableViewDelegate{ 

    /** 
    * Array to display menu options 
    */ 
    @IBOutlet var tblMenuOptions : UITableView! 

    /** 
    * Transparent button to hide menu 
    */ 
    @IBOutlet var btnCloseMenuOverlay : UIButton! 

    /** 
    * Array containing menu options 
    */ 
    var arrayMenuOptions = [Dictionary<String,String>]() 

    /** 
    * Menu button which was tapped to display the menu 
    */ 
    var btnMenu : UIButton! 

    /** 
    * Delegate of the MenuVC 
    */ 
    var delegate : SlideMenuDelegate? 

    // var delegateP: Profile? 

    @IBOutlet weak var userProfilePhoto: UIImageView! 

    @IBOutlet weak var userName: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tblMenuOptions.tableFooterView = UIView() 
     // Do any additional setup after loading the view. 


     //let name = userName.text 
     self.delegateP?.name_changed(name: userName.text!) 

     } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     updateArrayMenuOptions() 
    } 

    func updateArrayMenuOptions(){ 
     arrayMenuOptions.append(["title":"Home", "icon":"HomeIcon"]) 
     arrayMenuOptions.append(["title":"LogIn", "icon":"LogIn"]) 
     arrayMenuOptions.append(["title":"House Owner","icon":"House Owner"]) 
     arrayMenuOptions.append(["title":"ShortTerm", "icon":"ShortTerm"]) 
     arrayMenuOptions.append(["title":"LongTerm","icon":"LongTerm"]) 

     tblMenuOptions.reloadData() 
    } 

    @IBAction func onCloseMenuClick(_ button:UIButton!){ 
     btnMenu.tag = 0 

     if (self.delegate != nil) { 
      var index = Int32(button.tag) 
      if(button == self.btnCloseMenuOverlay){ 
       index = -1 
      } 
      delegate?.slideMenuItemSelectedAtIndex(index) 
     } 

     UIView.animate(withDuration: 0.3, animations: {() -> Void in 
      self.view.frame = CGRect(x: -UIScreen.main.bounds.size.width, y: 0, width: UIScreen.main.bounds.size.width,height: UIScreen.main.bounds.size.height) 
      self.view.layoutIfNeeded() 
      self.view.backgroundColor = UIColor.clear 
     }, completion: { (finished) -> Void in 
      self.view.removeFromSuperview() 
      self.removeFromParentViewController() 
     }) 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellMenu")! 
     cell.selectionStyle = UITableViewCellSelectionStyle.none 
     cell.layoutMargins = UIEdgeInsets.zero 
     cell.preservesSuperviewLayoutMargins = false 

     cell.backgroundColor = UIColor.clear 


     let lblTitle : UILabel = cell.contentView.viewWithTag(101) as! UILabel 
     let imgIcon : UIImageView = cell.contentView.viewWithTag(100) as! UIImageView 

     imgIcon.image = UIImage(named: arrayMenuOptions[indexPath.row]["icon"]!) 
     lblTitle.text = arrayMenuOptions[indexPath.row]["title"]! 

     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     let btn = UIButton(type: UIButtonType.custom) 
     btn.tag = indexPath.row 
     self.onCloseMenuClick(btn) 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return arrayMenuOptions.count 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1; 
    } 
} 

第二個視圖控制器從那裏上登錄按鈕的點擊要改變第一個視圖控制器的標籤名稱

class LogInViewController: UIViewController , FBSDKLoginButtonDelegate , GIDSignInUIDelegate{ 
    @IBOutlet weak var loginButton: UIButton! 
    @IBOutlet weak var EnterEmail: UITextField! 
    @IBOutlet weak var EnterPassword: UITextField!  

    // Login in button 

    @IBAction func loginButtton(_ sender: UIButton) { 

     let email = EnterEmail.text?.lowercased() 
     let finalEmail = email?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 
     let password = EnterPassword.text 

     // var name: String! 
     // menuView = self.storyboard?.instantiateViewController(withIdentifier: "MenuViewController1") as! MenuViewController1 

     // Register user in firebase with validations 
     Auth.auth().createUser(withEmail: email!, password: password!) { (user, error) in 

      if (error == nil && (finalEmail != "") && self.isValidEmail(testStr: finalEmail!)) && (password != " " && self.isPasswordValid(password!)) { 
       self.displayMyAlertMessage(userMessage:"You are successfully registered ")     
      } else { 
       self.displayMyAlertMessage(userMessage:"Registration Failed.. Please Try Again !")      
      } 
     }; 
     // self.navigationController?.pushViewController(MenuViewController1, animated: true) 
    } 
} 
+0

您能否讓它更清楚哪兩個sc reens推/顯示哪個? LogInViewController'是否推/顯示MenuViewController1或其他方式? – Glenn

+0

@Glenn LogInViewController必須顯示在LogInViewController –

+0

點擊按鈕MenuViewController1它只是跟隨代表團的概念,請參閱https://stackoverflow.com/questions/6168919/how-do-i-set-up-a-simple-代表之間的溝通之間的兩個視圖控制器 –

回答

2

我們假設MenuViewController將被通知LoginViewController已完成登錄。的方式是:

首先,在LoginViewController創建協議LoginViewController,並聲明委託

protocol LoginViewControllerDelegate:NSObjectProtocol { 
    func LoginDidFinish() 
} 

class LoginViewController: UIViewController{ 

    weak var delegate:LoginViewControllerDelegate? 
    //...... 
} 

,當登錄結束時,調用委託方法

Auth.auth().createUser(withEmail: email!, password: password!) { (user, error) in 
    if (error == nil && (finalEmail != "") && self.isValidEmail(testStr: finalEmail!)) && (password != " " && self.isPasswordValid(password!)){ 
     self.displayMyAlertMessage(userMessage:"You are successfully registered ") 
     self.delegate?.LoginDidFinish() //Key point 
    }else{ 
     self.displayMyAlertMessage(userMessage:"Registration Failed.. Please Try Again !") 
    } 
}; 

,在MenuViewController,分配selfLoginViewController代表創建LoginViewController時:

let loginViewController = LoginViewController() 
loginViewController.delegate = self 

最後,實現委託方法MenuViewController,它會被調用時,登錄完成:

//MARK: - LoginViewControllerDelegate 
func LoginDidFinish() { 
    print("Login did finish") 
    //And change your label's text here. 
} 
+0

謝謝陳芸,但它仍然不打印登錄去把在loginDidFinish寫()函數... –

+0

就'self.delegate斷點?.LoginDidFinish()'檢查,如果它被稱爲。並確保'self.delegate'不是順便說一句。 –

+0

是的,對.. self.delegate?.LoginDidFinish()這裏是問題。發件人\t的UIButton \t 0x00007f962a55aaa0 自\t DivaStays.LogInViewController \t 0x00007f962a455510 電子郵件\t字符串? \t 「[email protected]」 \t一些 finalEmail \t字符串? \t 「[email protected]」 \t一些 密碼\t字符串?\t「pooja @ 45ff」\t有些人在斷點處得到了這個 –

相關問題