[英]Action to Navigation bar back button
當用戶單擊后退按鈕時,我想顯示帶有確認的警報。 這就是我嘗試添加動作的方式。
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "<", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.save(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
這工作正常,但我想要其中的默認后退按鈕圖像而不是自定義標題。 怎么做?
我也嘗試了以下代碼:
self.navigationItem.backBarButtonItem?.action = #selector(ViewController.save(sender:))
...但也沒有執行操作。
這可能會有所幫助。 這不會覆蓋返回操作,但您可以執行其他任務。
目標c
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if(self.isMovingFromParentViewController)
{
//On click of back or swipe back
}
if(self.isBeingDismissed)
{
//Dismissed
}
NSLog(@"%d",self.isBeingDismissed);
NSLog(@"%d",self.isMovingFromParentViewController);
}
迅速
override func viewWillDisappear(_ animated: Bool)
{
super.viewWillDisappear(animated);
if self.isMovingFromParentViewController
{
//On click of back or swipe back
}
if self.isBeingDismissed
{
//Dismissed
}
}
我找到了解決辦法!
我在 iOS 11 和 iOS 13 上測試過它,它工作正常:)
protocol CustomNavigationViewControllerDelegate {
func shouldPop() -> Bool
}
class CustomNavigationViewController: UINavigationController, UINavigationBarDelegate {
var backDelegate: CustomNavigationViewControllerDelegate?
func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
return backDelegate?.shouldPop() ?? true
}
}
class SecondViewController: UIViewController, CustomNavigationViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
(self.navigationController as? CustomNavigationViewController)?.backDelegate = self
}
func shouldPop() -> Bool {
if (needToShowAlert) {
showExitAlert()
return false
} else {
return true
}
}
}
您需要向導航欄添加自定義后退按鈕,並向其添加操作
let backButton = UIBarButtonItem (image: UIImage(named: "ico-nav-prev")!, style: .plain, target: self, action: #selector(GoToBack))
self.navigationItem.leftBarButtonItem = backButton
self.navigationItem.hidesBackButton = true
func GoToBack(){
self.navigationController!.popViewController(animated: true)
}
我不知道你想達到什么目的,但這難道不是一個可行的解決方案,只要vieWillDisappear:
被調用,你就可以做你想做的事嗎? 這可能是一個匹配的入口點,無需使用其他答案中建議的后退按鈕。
更好的另一種選擇是實施UINavigationControllerDelegate
,將您的控制器設置為delegate
,然后在navigationController(_:willShow:animated:)
中實施您的警報。
看看這里以供參考。
在ParentViewController
的viewDidLoad
方法而不是ChildViewController
中調用這行代碼
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
從您的ChildViewController
中刪除以下代碼行
self.navigationItem.hidesBackButton = true
你會沒事的,如果你需要為這個過渡創建一個動作,我的意思是每當用戶點擊你的ChildViewController
的后退按鈕時。 只需在您的ChildViewController
中調用此方法
override func didMove(toParentViewController parent: UIViewController?) {
super.didMove(toParentViewController: parent)
if parent == nil {
} else {
}
}
編輯:
子視圖控制器
override func didMove(toParentViewController parent: UIViewController?) {
super.didMove(toParentViewController: parent)
if parent == nil {
NotificationCenter.default.post(name: NSNotification.Name.init("Post"), object: nil)
} else {
}
}
父視圖控制器
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(self.handler(notification:)), name: NSNotification.Name.init(rawValue: "Post"), object: nil)
}
func handler(notification: Notification) {
let alertController = UIAlertController(title: "Hello", message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
這是有效的,但有這樣的問題:
警告:在演示過程中嘗試繼續演示!
因此我不推薦。 祝你好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.