当我单击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
}
}
最佳答案
您可以使用协议。编写协议
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
}
}
关于ios - 如何从其他ViewController启用leftBarButtonItem?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39892805/