模态Viewcontroller被取消后如何更新父Viewcontroller的视图

时间:2019-05-17 09:24:36

标签: ios swift uiviewcontroller uinavigationcontroller

我有一个UIBarButtonItem,我想从模式viewController的值关闭时对其进行更新。目前,我只能通过获取当前不可见的viewController来执行此操作。有没有一种方法可以消除模态后更新父viewController。

class HomeViewController: UIViewController {

    @IBOutlet weak var accountButton: UIBarButtonItem!

    override func viewWillAppear(_ pAnimated: Bool) {
        super.viewWillAppear(pAnimated)
        self.accountButton.title = User.current!.firstName
   }

@IBAction func accountButton(_ pSender: UIBarButtonItem) {
        let editUserAccountVC = UIStoryboard.fs_instantiateFromStoryboard("Main", identifier: "EditUserAccountViewController") as! EditUserAccountViewController
        let navVC = UINavigationController(rootViewController: editUserAccountVC)
        navVC.view.tintColor = self.view.tintColor
        self.present(navVC, animated: true)
       }
   }
}

这是编辑后关闭的模态

class EditUserAccountViewController: UIViewController {

    var firstName: String?

    @IBAction func saveButton(_ sender: Any) {
        self.dismiss(animated: true) {
            if let thePresentedViewController = self.presentingViewController as? HomeViewController {
                thePresentedViewController.accountButton.title = self.firstName
            }
        }    
    }
}

2 个答案:

答案 0 :(得分:2)

我认为您应该在模态控制器实例中存储对父控制器的引用。您可以使用协议使其更通用。声明以下protocol

protocol AccountButtonProvider: AnyObject {
    var accountButtonTitle: String? { set get }
}

AnyObject必须声明对AccountButtonProvider的引用为weak。我想您将需要它来避免内存泄漏。

使HomeViewController符合AccountButtonProvider

extension HomeViewController: AccountButtonProvider {
    var accountButtonTitle: String? {
        set {
            accountButton.title = newValue
        }
        get {
            return accountButton.title
        }
    }
}

然后将以下属性添加到EditUserAccountViewController

weak var accountButtonProvider: AccountButtonProvider?

在显示EditUserAccountViewController之前初始化此属性。为此,将editUserAccountVC.accountButtonProvider = self添加到accountButton(_:)

@IBAction func accountButton(_ pSender: UIBarButtonItem) {
    let editUserAccountVC = UIStoryboard.fs_instantiateFromStoryboard("Main", identifier: "EditUserAccountViewController") as! EditUserAccountViewController
    editUserAccountVC.accountButtonProvider = self
}

您还应该修改saveButton(_:)

@IBAction func saveButton(_ sender: Any) {
    self.dismiss(animated: true) {
        self.accountButtonProvider?.actionButtonTitle = self.firstName
    }
}

答案 1 :(得分:1)

您可以使用 closure 解决问题陈述。

1。。在closure

中创建一个EditUserAccountViewController
var handler: ((String?)->())?

2。。设置closure中出现的EditUserAccountViewController时该HomeViewController的值

controller.handler = {(name) in
    self.accountButton.title = name
}

3。。在按closure

取消EditUserAccountViewController时呼叫saveButton
self.dismiss(animated: true) {
    self.handler?(self.firstName)
}

编译代码:

class HomeViewController: UIViewController {
    @IBOutlet weak var accountButton: UIBarButtonItem!

    @IBAction func openEditUserAccountVC(_ sender: Any) {
        if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "EditUserAccountViewController") as? EditUserAccountViewController {
            controller.handler = {(name) in
                self.accountButton.title = name
            }
            self.present(controller, animated: true, completion: nil)
        }
    }
}

class EditUserAccountViewController: UIViewController {
    var firstName: String?
    var handler: ((String?)->())?

    @IBAction func saveButton(_ sender: Any) {
        self.dismiss(animated: true) {
            self.handler?(self.firstName)
        }
    }
}