2017-05-04 98 views
0

我有一個導航控制器,我試圖把一個按鈕放在導航欄的右側,但我無法處理輕擊操作。我宣佈的UIBarButtonItem這樣UIBarButtonItem不工作迅速3.0

let navigationButton = UIBarButtonItem.init(title: "Logout", style: .done, target: self, action: #selector(RestaurantsListViewController.logoutAction)) 

我加入的viewDidLoad中FUNC

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.navigationItem.rightBarButtonItem = navigationButton 
} 

而且我想用來處理自來水事件的功能按鈕是這樣

func logoutAction(sender: AnyObject?){ 
    print("Logout") 
} 

但是當我按下按鈕時,消息不會被打印在控制檯中。

+0

讓navigationButton = UIBarButtonItem.init(標題: 「註銷」,風格:.done ,目標:自我,行動​​:#選擇器(RestaurantsListViewController.logoutAction))在視圖中加載..? –

+0

你在哪個控制器中寫過logoutAction功能? –

+0

@AnilKumar我有主類的初始化,所有方法 –

回答

3

試試這個:

override func viewDidLoad() { 
    super.viewDidLoad() 

self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "ButtonName", style: .done, target: self, action: #selector(YourViewController.yourAction)) 

} 
+0

初始化UIBarButtonItem裏面viewDidLoad func修復問題,謝謝! –

0
let okbtn = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.logoutAction)) 

嘗試這個 否則你需要更換FUNC像

func logoutAction() 
{ 
    print("logout") 
}