簡體   English   中英

如何以編程方式按下 UINavigationController 中的“后退”按鈕

[英]How to press “Back” button in UINavigationController programmatically

我在UINavigationController有一個名為FriendsViewControllerUIViewController 第二個UIViewController稱為FriendsDetailedViewController 從第一個視圖控制器導航到第二個視圖控制器時,我想在需要時以編程方式按下Back按鈕。 這個怎么做?

只需使用

[self.navigationController popViewControllerAnimated:YES]

來自 FriendsDetailedViewController。 您的視圖將彈出,即后退按鈕的行為

如果按“后退”按鈕只是想轉到上一個視圖控制器,則可以調用:

[self.navigationController popViewControllerAnimated:YES];

這是快速方法

if let navController = self.navigationController {
    navController.popViewControllerAnimated(true)
}

斯威夫特 5

 self.navigationController?.popViewController(animated: true)

在代碼片段中的用法:

func unwindViewController() {
    self.navigationController?.popViewController(animated: true)
}

這是我在 Swift 3 中的做法

_ = self.navigationController?.popViewController(animated: true)

_用於抑制 XCode 生成的丑陋警告。

1) 當你彈出當前的 NavigationController 然后

在斯威夫特

self.navigationController?.popViewControllerAnimated(true)

目標 C

[self.navigationController popViewControllerAnimated:YES];

2)當你支持另一個導航控制器然后

在斯威夫特

let story = UIStoryboard(name: "Main", bundle: nil)
let pushVC = story.instantiateViewControllerWithIdentifier("PushVC")
let navigation = story.instantiateViewControllerWithIdentifier("homeNavigation") as! UINavigationController
navigation.pushViewController(pushVC!, animated: true)

在目標 C

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoardName" bundle:nil];
pushVC* ObjectOfPushVC = [storyboard instantiateViewControllerWithIdentifier:@"pushVC"];

[self.navigationController pushViewController:ObjectOfPushVC animated:YES];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM