三个视图控制器之间的导航,其中所有三个视图控制器都在一个标签栏视图中
Navigation between three view controllers, where all three of them are in one tab bar view
我有一个标签栏视图。其中,三个选项卡之一是联系人应用程序。在那个联系人应用程序中,有三个页面,一个显示所有联系人,第二个显示在第一个 VC 中选择的特定联系人,第三个是添加新联系人。 (与内置 iOS 应用程序非常相似)如何在这三个页面之间创建流畅的流程。 (不要忘记它们应该在一个标签栏内)
我已经添加了所有需要的按钮。
- 首页 - 显示所有联系人
- 第二页 - 显示一位特定联系人
- 第三页 - 添加新联系人
我嵌入了 3 个导航视图控制器(3 个页面各一个)
第一页有 'add' 按钮,应该会转到第三页。
第二页有两个按钮,一个返回第一页,另一个返回第三页。
第三个有两个按钮,每个按钮转到其他两个页面。
我使用此函数在导航栏内以编程方式创建了这些按钮 -
override func viewDidAppear(_ animated: Bool) {
navigationItem.title = "Add New Contact"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(theTransition))
}
@objc func theTransition() {
let second = self.storyboard?.instantiateViewController(withIdentifier: "OneNavID") as! OneNavigationViewController
self.present(second, animated: true, completion: nil)
}
(以上代码是从第3页跳到第1页的例子)
这些按钮工作正常,但它们显示标签栏控制器之外的新页面。
我希望他们留在选项卡视图中。请帮忙!如果你对这个问题有任何疑问,一定要问。我尽量用简单和简短的词来解释它。
我在互联网上看了很多,但我找不到任何三个视图控制器中的每一个都有一个导航控制器的实例(我认为这是必需的,因为它们每个都有传入和传出链接 to/from 其他页)。
第二页-
override func viewDidAppear(_ animated: Bool) {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItem.Style.plain, target: self, action: #selector(theTransition))
}
@objc func theTransition() {
let second = self.storyboard?.instantiateViewController(withIdentifier: "ThreeNavID") as! ThreeNavigationViewController
self.present(second, animated: true, completion: nil)
}
第三页-
override func viewDidAppear(_ animated: Bool) {
navigationItem.title = "Add New Contact"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(theTransition))
}
@objc func theTransition() {
let second = self.storyboard?.instantiateViewController(withIdentifier: "NavID") as! NavigationViewController
self.present(second, animated: true, completion: nil)
}
不要使用 3 个导航控制器。像这样使用一个导航控制器
UITabBarController_____ Another View Controller1
|
|____ Another View Controller2
|
|____ Contact - UINavigationController -- FirstPage
UINavigationController -- FirstPage --> SecondPage --> ThirdPage
在导航控制器中嵌入第一个页面,然后push second and third pages. Don't create a new instance for the first page. Use popViewController转到上一个视图控制器。
您可以使用 pushViewController 将数据传递到下一个视图控制器,并使用自定义委托或闭包将数据发送到上一个视图控制器。或者像这样在自定义导航控制器 class 中创建一个变量
class CustomNavigationController: UINavigationController {
var name: String?
}
然后像这样从其他视图控制器读取和写入数据
class FirstPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print((self.navigationController as? CustomNavigationController)?.name)
(self.navigationController as? CustomNavigationController)?.name = "FirstPage"
}
@objc func goToSecondPage() {
if let secondPage = self.storyboard?.instantiateViewController(withIdentifier: "SecondPage") as? SecondPage {
self.navigationController?.pushViewController(secondPage, animated: true)
}
}
@objc func goToThirdPage() {
if let thirdPage = self.storyboard?.instantiateViewController(withIdentifier: "ThirdPage") as? ThirdPage {
self.navigationController?.pushViewController(thirdPage, animated: true)
}
}
}
class SecondPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print((self.navigationController as? CustomNavigationController)?.name)
(self.navigationController as? CustomNavigationController)?.name = "SecondPage"
}
@objc func goToThirdPage() {
if let thirdPage = self.storyboard?.instantiateViewController(withIdentifier: "ThirdPage") as? ThirdPage {
self.navigationController?.pushViewController(thirdPage, animated: true)
}
}
@objc func goToFirstPage() {
self.navigationController?.popViewController(animated: true)
}
}
class ThirdPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print((self.navigationController as? CustomNavigationController)?.name)
(self.navigationController as? CustomNavigationController)?.name = "ThirdPage"
}
@objc func goToFirstPage() {
self.navigationController?.popToRootViewController(animated: true)
}
@objc func goToSecondPage() {
self.navigationController?.popViewController(animated: true)
}
}
我有一个标签栏视图。其中,三个选项卡之一是联系人应用程序。在那个联系人应用程序中,有三个页面,一个显示所有联系人,第二个显示在第一个 VC 中选择的特定联系人,第三个是添加新联系人。 (与内置 iOS 应用程序非常相似)如何在这三个页面之间创建流畅的流程。 (不要忘记它们应该在一个标签栏内)
我已经添加了所有需要的按钮。
- 首页 - 显示所有联系人
- 第二页 - 显示一位特定联系人
- 第三页 - 添加新联系人
我嵌入了 3 个导航视图控制器(3 个页面各一个)
第一页有 'add' 按钮,应该会转到第三页。 第二页有两个按钮,一个返回第一页,另一个返回第三页。 第三个有两个按钮,每个按钮转到其他两个页面。
我使用此函数在导航栏内以编程方式创建了这些按钮 -
override func viewDidAppear(_ animated: Bool) {
navigationItem.title = "Add New Contact"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(theTransition))
}
@objc func theTransition() {
let second = self.storyboard?.instantiateViewController(withIdentifier: "OneNavID") as! OneNavigationViewController
self.present(second, animated: true, completion: nil)
}
(以上代码是从第3页跳到第1页的例子)
这些按钮工作正常,但它们显示标签栏控制器之外的新页面。 我希望他们留在选项卡视图中。请帮忙!如果你对这个问题有任何疑问,一定要问。我尽量用简单和简短的词来解释它。 我在互联网上看了很多,但我找不到任何三个视图控制器中的每一个都有一个导航控制器的实例(我认为这是必需的,因为它们每个都有传入和传出链接 to/from 其他页)。
第二页-
override func viewDidAppear(_ animated: Bool) {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItem.Style.plain, target: self, action: #selector(theTransition))
}
@objc func theTransition() {
let second = self.storyboard?.instantiateViewController(withIdentifier: "ThreeNavID") as! ThreeNavigationViewController
self.present(second, animated: true, completion: nil)
}
第三页-
override func viewDidAppear(_ animated: Bool) {
navigationItem.title = "Add New Contact"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(theTransition))
}
@objc func theTransition() {
let second = self.storyboard?.instantiateViewController(withIdentifier: "NavID") as! NavigationViewController
self.present(second, animated: true, completion: nil)
}
不要使用 3 个导航控制器。像这样使用一个导航控制器
UITabBarController_____ Another View Controller1
|
|____ Another View Controller2
|
|____ Contact - UINavigationController -- FirstPage
UINavigationController -- FirstPage --> SecondPage --> ThirdPage
在导航控制器中嵌入第一个页面,然后push second and third pages. Don't create a new instance for the first page. Use popViewController转到上一个视图控制器。
您可以使用 pushViewController 将数据传递到下一个视图控制器,并使用自定义委托或闭包将数据发送到上一个视图控制器。或者像这样在自定义导航控制器 class 中创建一个变量
class CustomNavigationController: UINavigationController {
var name: String?
}
然后像这样从其他视图控制器读取和写入数据
class FirstPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print((self.navigationController as? CustomNavigationController)?.name)
(self.navigationController as? CustomNavigationController)?.name = "FirstPage"
}
@objc func goToSecondPage() {
if let secondPage = self.storyboard?.instantiateViewController(withIdentifier: "SecondPage") as? SecondPage {
self.navigationController?.pushViewController(secondPage, animated: true)
}
}
@objc func goToThirdPage() {
if let thirdPage = self.storyboard?.instantiateViewController(withIdentifier: "ThirdPage") as? ThirdPage {
self.navigationController?.pushViewController(thirdPage, animated: true)
}
}
}
class SecondPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print((self.navigationController as? CustomNavigationController)?.name)
(self.navigationController as? CustomNavigationController)?.name = "SecondPage"
}
@objc func goToThirdPage() {
if let thirdPage = self.storyboard?.instantiateViewController(withIdentifier: "ThirdPage") as? ThirdPage {
self.navigationController?.pushViewController(thirdPage, animated: true)
}
}
@objc func goToFirstPage() {
self.navigationController?.popViewController(animated: true)
}
}
class ThirdPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print((self.navigationController as? CustomNavigationController)?.name)
(self.navigationController as? CustomNavigationController)?.name = "ThirdPage"
}
@objc func goToFirstPage() {
self.navigationController?.popToRootViewController(animated: true)
}
@objc func goToSecondPage() {
self.navigationController?.popViewController(animated: true)
}
}