2016-10-06 71 views
1

當我單擊MessageController上的左側欄按鈕時,我將它啓用爲false。然後,我呈現PopUpViewController,並在按鈕上單擊函數removeAnimate()運行。在這個函數中,我想將MessageController左邊的bar按鈕設置爲isEnabled爲true。我已經嘗試過,但不起作用。有人可以幫忙嗎?如何從其他ViewController啓用leftBarButtonItem?

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 
} 

} 
+0

如何從'MessageController'跳轉到'PopUpViewController'? –

+0

@ Mr.Bistra in func preformSettings() – Moonwalker4z

+0

這意味着沒有segue? –

回答

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 
    } 
} 
+0

popViewController.delegate = self 無法設置屬性'委託'是一個常量 即使我更改讓popViewController = PopUpViewController()它變爲 – Moonwalker4z

+0

使'PopViewController'中的委託給var,如圖所示。 – Mathews

+0

它的作品謝謝你 – Moonwalker4z

相關問題