主细节应用程序初始视图控制器更改

Posted

技术标签:

【中文标题】主细节应用程序初始视图控制器更改【英文标题】:Master detail application initial view controller change 【发布时间】:2017-07-17 12:20:54 【问题描述】:

我是 ios 世界的新手,对 Master detail 应用程序有疑问。 根视图是 UISplitViewController。我有 LoginViewController 并希望使其成为根视图,然后在按钮单击 segue UISplitViewController 之后。

我试过但收到错误“只有在源控制器由 UINavigationController 的实例管理时才能使用推送转场”

AppDelegate.swift


import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate 

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
        // Override point for customization after application launch.
//        let splitViewController = window!.rootViewController as! UISplitViewController
//        let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
//        navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
//        splitViewController.delegate = self
        return true
    

    func applicationWillResignActive(_ application: UIApplication) 
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    

    func applicationDidEnterBackground(_ application: UIApplication) 
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    

    func applicationWillEnterForeground(_ application: UIApplication) 
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    

    func applicationDidBecomeActive(_ application: UIApplication) 
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    

    func applicationWillTerminate(_ application: UIApplication) 
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    

    // MARK: - Split view

    func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool 
        guard let secondaryAsNavController = secondaryViewController as? UINavigationController else  return false 
        guard let topAsDetailController = secondaryAsNavController.topViewController as? DetailViewController else  return false 
        if topAsDetailController.detailItem == nil 
            // Return true to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
            return true
        
        return false
    


登录视图控制器

import UIKit

class LoginViewController: UIViewController 


    @IBOutlet weak var TryButton: UIButton!

    override func viewDidLoad() 
        super.viewDidLoad()
        TryButton.layer.cornerRadius = 10
    

    @IBAction func policiesButtonClick(_ sender: Any) 
        guard let url = URL(string: "https://google.com/#q=") else 
            return
        
        if #available(iOS 10.0, *) 
            UIApplication.shared.open(url)
         else 
            UIApplication.shared.openURL(url)
        
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    



【问题讨论】:

使用另一种segue ***.com/questions/25966215/… 【参考方案1】:

尝试使用如下导航控制器嵌入您的控制器:

选择导航控制器

【讨论】:

【参考方案2】:

你应该在 mainViewController 之后添加一个 UINavigationController,然后在那里设置 Root 和导航的其余部分。由于SplitViewController不是NavigationController,所以他不能做Push和Pop的。

【讨论】:

【参考方案3】:

您应该将根视图更改为 UINavigationController。错误显然是相同的。

"Push segues 只能在源控制器被管理时使用 UINavigationController 的一个实例”

SplitViewController 到 NavigationControllers 的 segue 是不同的。它应该嵌入 segue 而不是 pushpop。 Push 和 pop segues 只与导航控制器相关联

【讨论】:

以上是关于主细节应用程序初始视图控制器更改的主要内容,如果未能解决你的问题,请参考以下文章

异步请求后从 AppDelegate 更改初始视图控制器

使用主/详细应用程序更改默认视图

如何在成功登录后更改初始视图控制器?

iOS 初始化细节视图控制器

快速更改 AppDelegate 中的初始视图控制器

iOS - 每次显示的视图更改时回调