我想创建一个允许用户单击导航栏上V1上的按钮的应用程序。这将选择V2,他们可以按V2导航栏上的另一个按钮,并将其带回到V1,V1的底部为标签栏控制器。我不希望V2上的标签栏,也不希望V2作为标签栏项目。当我尝试此操作时,当我从V2搜索回V1时,选项卡栏消失在V1上。

TAB BAR CONTROLLER-> TAB BAR ITEM(V1)-> V2(V1上的Via导航栏按钮项)->返回V1(V2上的Via导航栏按钮项)

我在按钮函数中添加了[self.navigationController, popViewControllerAnimated:YES];,但它出现了一个错误-容器文字中的预期表达式。

除了此代码外,我的应用程序中还没有任何其他代码。

我正在使用Xcode 8.0和Swift 3.0

最佳答案

首先创建UITabBarController的子类,然后将属性添加到AppDelegate

var navController: UINavigationController?
var tabController: MyTabController?


如果要在应用启动时显示标签栏控制器,则将这些代码放在AppDelegate中的didFinishLaunchingWithOptions

 self.window = UIWindow(frame: UIScreen.main.bounds)
 let myStoryboard = UIStoryboard(name: "Main", bundle: nil) as UIStoryboard
 self.tabController = myStoryboard.instantiateViewController(withIdentifier: "MyTabController") as? MyTabController
//self.navController = UINavigationController(rootViewController: self.tabController!)
//self.window?.rootViewController = self.navController
 self.window?.rootViewController = self.tabController
 self.window?.makeKeyAndVisible()
 return true


如果要在登录后或其他之后跳到选项卡栏,请向该控制器添加属性

 var appDelegate: AppDelegate!


viewDidLoad

 appDelegate = UIApplication.shared.delegate as? AppDelegate


和方法应该一样

 func logIntoApp() {

    appDelegate.tabController = self.storyboard?.instantiateViewController(withIdentifier: "MyTabController") as? MyTabController
    appDelegate.window?.rootViewController = appDelegate.tabController
 }


然后在选项卡项目视图控制器中,创建AppDelegate的属性,并如上所述分配委托。

方法应该像这样:

@IBAction func showWithTab(_sender: AnyObject) {

      let DefaultVC = self.storyboard?.instantiateViewController(withIdentifier: "DefaultViewController") as! DefaultViewController
      self.navigationController?.pushViewController(DefaultVC, animated: true)
}

@IBAction func showWithoutTab(_sender: AnyObject) {

      let DefaultVC = self.storyboard?.instantiateViewController(withIdentifier: "DefaultViewController") as! DefaultViewController

    // You can create your own animation
      UIView.transition(from: (appDelegate.tabController?.view)!, to: (appDelegate.navController?.view)!, duration: 0.3, options: UIViewAnimationOptions.curveEaseIn) { (finished) in

      self.appDelegate.window?.rootViewController = self.appDelegate.navController
    }

    // OR you can use like this way
      UIView.transition(from: self.view, to: DefaultVC.view, duration: 0.3, options: UIViewAnimationOptions.curveEaseIn) { (finished) in

      self.appDelegate.window?.rootViewController = self.appDelegate.navController
    }
}

关于ios - 返回到另一个 View 时,标签栏 Controller 消失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39952413/

10-14 05:37