Ios 三个视图控制器之间的导航,其中所有三个都位于一个选项卡栏视图中
我有一个选项卡栏视图。在这三个选项卡中,有一个是contacts应用程序。在联系人应用程序中,有三个页面,一个显示所有联系人,第二个显示在第一个VC中选择的一个特定联系人,第三个是添加新联系人。(与内置iOS应用程序非常相似)如何在这三个页面之间创建流畅的流(不要忘记它们应该在一个选项卡栏中) 我已经添加了所有必需的按钮Ios 三个视图控制器之间的导航,其中所有三个都位于一个选项卡栏视图中,ios,swift,uinavigationcontroller,uitabbarcontroller,Ios,Swift,Uinavigationcontroller,Uitabbarcontroller,我有一个选项卡栏视图。在这三个选项卡中,有一个是contacts应用程序。在联系人应用程序中,有三个页面,一个显示所有联系人,第二个显示在第一个VC中选择的一个特定联系人,第三个是添加新联系人。(与内置iOS应用程序非常相似)如何在这三个页面之间创建流畅的流(不要忘记它们应该在一个选项卡栏中) 我已经添加了所有必需的按钮 第一页-显示所有联系人 第二页-显示一个特定联系人 第三页-添加新联系人 我嵌入了3个导航视图控制器(3个页面各一个) 第一页有“添加”按钮,该按钮应指向第三页。 第二页有
- 第一页-显示所有联系人
- 第二页-显示一个特定联系人
- 第三页-添加新联系人
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页的示例)
这些按钮工作正常,但它们会显示选项卡栏控制器之外的新页面。
我希望它们留在选项卡视图中。请帮忙!如果你对这个问题有任何疑问,一定要问。我尽量用简明扼要的话来解释它。
我在互联网上看过很多次,但我找不到任何三个视图控制器中每个都有一个导航控制器的实例(我认为这是必要的,因为它们每个都有到其他页面的传入和传出链接)
第二页-
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
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)
}
}
在导航控制器中嵌入第一页,并嵌入第二页和第三页。不要为第一页创建新实例。用于转到上一个视图控制器
可以使用pushViewController将数据传递到下一个视图控制器,并使用自定义委托或闭包将数据发送到上一个视图控制器。或者像这样在自定义导航控制器类中创建一个变量
UITabBarController_____ Another View Controller1
|
|____ Another View Controller2
|
|____ Contact - UINavigationController -- FirstPage
UINavigationController -- FirstPage --> SecondPage --> ThirdPage
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)
}
}
然后像这样从其他视图控制器读取和写入数据
UITabBarController_____ Another View Controller1
|
|____ Another View Controller2
|
|____ Contact - UINavigationController -- FirstPage
UINavigationController -- FirstPage --> SecondPage --> ThirdPage
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,因此隐藏了选项卡栏。等等,我将尝试共享图像。为什么要展示VC,尝试将它们推送到NavigationControlleri如果您正在代码中实例化一个新的视图控制器,那么它肯定是一个新的视图控制器。同时,您将演示该视图控制器。这也涵盖了这一观点。您需要查找选项卡栏项目的
selectItemAtIndex
。使用它,而不是每次都实例化视图控制器。此外,如果正在推送视图控制器,请将视图控制器的hidesBottomBarWhenPushed
属性设置为false,以便它不会隐藏选项卡栏。部分有效!但是我该怎么做才能从2变为3,反之亦然。实际上,我正在使用segue从2发送一些数据到3。早些时候它工作得很好,但由于我做了这些改变,它就不再工作了。另外,当我从3返回到2时,我希望单元格中包含名称和数字等值,但它会显示一个全新的空单元格。