如何从其他ViewController启用leftBarButtonItem?

时间:2016-10-06 09:50:48

标签: ios iphone swift swift3 viewcontroller

当我单击MessageController上的左侧栏按钮时,我将其设为isEnabled为false。然后我呈现PopUpViewController并在按钮上单击函数removeAnimate()运行。在该函数中我想将MessageController左栏按钮设置为isEnabled为true。我试过但它不起作用。有人可以帮忙吗?

class PopUpViewController: UIViewController {
...
override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.black.withAlphaComponent(0.8)
    self.showAnimate()
}
func showAnimate()
{
    view.backgroundColor = UIColor.black.withAlphaComponent(0.8)
    self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    self.view.alpha = 0.0;
    UIView.animate(withDuration: 0.25, animations: {
        self.view.alpha = 1.0
        self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    });
}
func removeAnimate()
{
 let messagView = MessageController()
    UIView.animate(withDuration: 0.25, animations: {
        self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
        self.view.alpha = 0.0;
        }, completion:{(finished : Bool)  in
            if (finished)
            {
                self.view.removeFromSuperview()
                messagView.navigationItem.leftBarButtonItem?.isEnabled = true
            }
    });
}
lazy var cancleButton: UIButton = {
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Cancel", for: UIControlState())
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
    button.tintColor = .white
    button.addTarget(self, action: #selector(removeAnimate), for: .touchUpInside)
    return button
}()
}

MessageViewController

class MessageController: UITableViewController {
override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(preformSettings))
}
func preformSettings(){

    let popViewController =  PopUpViewController()
    self.addChildViewController(popViewController)
    popViewController.view.frame = self.view.frame
    self.view.addSubview(popViewController.view)
    popViewController.didMove(toParentViewController: self)
    navigationItem.leftBarButtonItem?.isEnabled = false
}

}

1 个答案:

答案 0 :(得分:1)

您可以使用协议。写一个协议

protocol PopViewControllerDelegate: class {
     func enableBackButton()
}

在PopUpViewController中创建此协议的对象。

class PopUpViewController: UIViewController {
    var delegate: PopViewControllerDelegate?
    ....

    func removeAnimate() {
       ...
       delegate?.enableBackButton()
    }

}

在MessageController中实现此协议。

class MessageController: UITableViewController {

   func preformSettings(){
       let popViewController =  PopUpViewController()
       popViewController.delegate = self
       self.addChildViewController(popViewController)
       ....
   }

}

extension MessageController: PopViewControllerDelegate {
    func enableBackButton() {
        navigationItem.leftBarButtonItem?.isEnabled = true
    }
}