我有麻煩讓我取消按鈕的工作。我有這樣的動作連接到我的取消按鈕,我打算關閉該視圖控制器,像這樣:如何創建取消按鈕?
@IBAction func cancel(sender: UIBarButtonItem) {
dismissViewControllerAnimated(true, completion: nil)
}
我想知道我錯過了什麼明顯的一點。 (這是表格視圖的酒吧按鈕項目)。
我有麻煩讓我取消按鈕的工作。我有這樣的動作連接到我的取消按鈕,我打算關閉該視圖控制器,像這樣:如何創建取消按鈕?
@IBAction func cancel(sender: UIBarButtonItem) {
dismissViewControllerAnimated(true, completion: nil)
}
我想知道我錯過了什麼明顯的一點。 (這是表格視圖的酒吧按鈕項目)。
var b = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: "Cancel clicked")
如果你想採取發件人作爲參數的方法,你會放一個冒號結尾:
var b = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: "CancelClicked:")
func CancelClicked(sender: UIBarButtonItem) {
}
在哪裏你分配這個變種b,完全寫你的代碼 –
我可以指定按鈕作爲插座? – needshelp
試試這個:
@IBAction func close(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
您添加導航欄(和導航項目)後使用IB故事板,以navigationItem VAR參考將自動映射到你的ViewController文件。添加以下代碼以將取消按鈕附加到導航項以關閉模式視圖控制器:
@IBOutlet weak var navigationBar:UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(CancelClicked(sender:)))
}
func CancelClicked(sender: UIBarButtonItem) {
print("Cancel clicked!")
self.dismiss(animated: true, completion: nil)
}
您是否在表格視圖或導航欄上添加了欄按鈕項? –
您的意見是否使用'navigationController'?您可以嘗試'navigationController!.popViewControllerAnimated(true)',但如果視圖是以模態方式呈現的,則這不起作用。 –
上面的代碼將嘗試解除模態顯示的子視圖控制器。我添加了一個答案來解釋多一點 – Lukas