ios - 移回另一个 View 时选项卡栏 Controller 消失

标签 ios swift xcode uinavigationbar segue

我想创建一个应用程序,让用户单击导航栏上 V1 上的按钮。这将转到 V2,他们可以在 V2 上的导航栏上按另一个按钮,然后将他们带回 V1,其中底部有选项卡栏 Controller 。我不希望 V2 上有标签栏,也不希望 V2 作为标签栏项目。当我尝试此操作时,当我从 V2 切换回 V1 时,标签栏会在 V1 上消失。

标签栏 Controller -> 标签栏项目 (V1) -> V2(通过 V1 上的导航栏按钮项目) -> 返回 V1(通过 V2 上的导航栏按钮项目)

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

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

我使用的是 Xcode 8.0 和 Swift 3.0

最佳答案

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

var navController: UINavigationController?
var tabController: MyTabController?

如果您想在应用程序启动时显示选项卡栏 Controller ,请将这些代码放入 didFinishLaunchingWithOptions 中的 AppDelegate

 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

如果您想在登录后跳转到选项卡栏或其他内容,请向该 Controller 添加属性

 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
 }

然后在选项卡项 View Controller 中,创建 AppDelegate 属性并按上述方式分配委托(delegate)。

方法应该是这样的:

@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/

相关文章:

ios - GameCenter - 每个级别都有一个排行榜

ios - xcode - Assets 目录 - 图像类型 - 相同比例 - 不同分辨率

iphone - 如何在 AVAudioPlayer 发布时调试此死锁?

iphone - iPhone 资源的单独目录

ios - 我可以安全地删除 Xcode 派生数据文件夹的内容吗?

iphone - 重新配置动态库

ios - 通过 sendMessage 发送一个巨大的字典 - WatchKit

ios - "if"语句的问题 : "trying to style message. 内容根据 message.sender 有所不同

xcode - SWIFT + 解析 signUpInBackgroundWithBlock 不再有效 : Xcode 6. 3.1

swift - 一个 collectionView 中的不同类型的单元格