如何使用UIView单元中的firebase用户名重命名其他View控制器中的按钮?

时间:2019-03-31 11:34:34

标签: swift xcode

当我从单元格中选择该用户时,我正在尝试使用Firebase数据库中的用户名重命名“添加保镖”按钮

// Code for the contacts page where a user is selected from cell.


class ContactsTableViewController: UITableViewController { 
var ref: DatabaseReference!,
    posts = [eventsStruct]()

    let cellId = "cellId"
    var users = [CurrentUser]()


    struct eventsStruct {
        let email: String!
        let username: String!
        let phoneNumber: String!
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        fetchUser()
    }

    func fetchUser() {
        ref = Database.database().reference()
        ref.child("Users").queryOrderedByKey().observe(.childAdded, with:{(snapshot) in
            if let usersDictionary = snapshot.value as? [AnyHashable:String]
            {
                let email = usersDictionary["email"]
                let username = usersDictionary["username"]
                let phoneNumber = usersDictionary["phoneNumber"]



                self.posts.insert(eventsStruct (email: email, username: username, phoneNumber: phoneNumber), at: 0)

                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }

                print(eventsStruct.init(email: email, username: username, phoneNumber: phoneNumber))
            }
        })



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


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

    var homeViewController: HomeViewController?
    //tap to dismiss contacts page
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dismiss(animated: true) {
            print("dismiss completed")
            let user = self.posts[indexPath.row]

       self.homeViewController?.addBodyguard.setTitle(user.email, for: .normal)
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
   //     let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)


        let user = posts[indexPath.row]
        cell.textLabel?.text = user.username
        cell.detailTextLabel?.text = user.phoneNumber

        return cell
    }

    @IBAction func cancelTapped(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }



// the HOMEVIEWcontroller containing the button to be renamed

class HomeViewController: UIViewController, UISearchBarDelegate, CLLocationManagerDelegate, MKMapViewDelegate {

 @IBOutlet weak var addBodyguard: UIButton!

  @IBAction func Addguard(_ sender: Any) {
        self.performSegue(withIdentifier: "contactsSegue", sender: nil)

    }
}

2 个答案:

答案 0 :(得分:0)

与其在ContactViewController中设置UIButtons的标题,不如使用委托将事件传递给HomeViewController

protocol ContactSelectionDelegate {
 func contactSelected(email: String) -> Void
}

extension HomeViewController: ContactSelectionDelegate {
 func contactSelected(email: String) -> Void {
   self.addBodyguard.setTitle(email, for: .normal)
 }
}

// In your HomeViewController

@IBAction func Addguard(_ sender: Any) { 
  self.performSegue(withIdentifier: "contactsSegue", sender: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   if (segue.identifier == "contactsSegue")
    {
        let secondViewController = segue.destinationViewController as! ContactViewController
        secondViewController.delegate = self
    }
}


// In your ContactViewController
class ContactViewController {

 ....
 weak var delegate: ContactSelectionDelegate?

 ...

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    dismiss(animated: true) {
        print("dismiss completed")
        let user = self.posts[indexPath.row]

   self.delegate?.contactSelected(email: user.email)
    }
 }
}


答案 1 :(得分:0)

像UIKit这样的Model-View-Controller设计中的常规约定是不要从一个ViewController进入另一个ViewController。

相反,还有其他方法:

  1. 为知道新名称应该在按钮上的类提供delegate属性,并将另一个控制器作为其委托并通知它。 (其他人已经详细回答了这个问题)

  2. 具有一个包含用户名的共享模型对象,并使两个控制器都基于该模型对象显示其数据。在这种情况下,您的Firebase控制器会将新名称写入模型,这将使​​按钮的控制器注意到属性已更改并更改按钮名称。

  3. 发送Notification(使用NotificationCenter)。在这种情况下,我不会这样做。通知通常最适合用于使应用程序大部分感兴趣的事物,因为任何人都可以订阅它们。因此,它们有点削弱您的类的封装。在调试时,查找找到响应通知的所有位置也可能会涉及到一些工作。