Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以编程方式更改情节提要的rootViewController_Ios_Uiviewcontroller_Storyboard - Fatal编程技术网

Ios 以编程方式更改情节提要的rootViewController

Ios 以编程方式更改情节提要的rootViewController,ios,uiviewcontroller,storyboard,Ios,Uiviewcontroller,Storyboard,我已经使用故事板创建了我的项目。根ViewController位于情节提要中,我没有在appDelegate中编写任何代码 现在我想显示我的应用程序的浏览,所以我想将rootViewController从tabbar更改为我的TourVC,当应用程序浏览完成后,我想再次将我的rootViewController切换回tabbar 因此,我在网上查找并遵循以下几点 1) 从app.plist文件中删除故事板, 2) 取消选中情节提要中的选项“isInitialViewController”,该选项

我已经使用
故事板创建了我的项目。根
ViewController
位于
情节提要
中,我没有在
appDelegate
中编写任何代码

现在我想显示我的应用程序的浏览,所以我想将root
ViewController
tabbar
更改为我的TourVC,当应用程序浏览完成后,我想再次将我的root
ViewController
切换回
tabbar

因此,我在网上查找并遵循以下几点

1) 从app.plist文件中删除故事板, 2) 取消选中
情节提要
中的选项“isInitialViewController”,该选项在
选项卡栏
控制器的情况下被选中,因为它是根
视图控制器
, 3) 在appDelegate.m文件中添加此代码

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ProductTourViewController *PT = [[ProductTourViewController alloc] initWithNibName:@"ProductTourViewController" bundle:nil];
self.window.rootViewController = PT;
[self.window makeKeyAndVisible];
return YES;
但我的应用程序因此错误日志而崩溃

[ProductTourViewController selectedViewController]: unrecognized selector sent to instance 0x1766a9e0
我还得到了一个警告

Unsupported Configuration: Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier:.
目标C:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarcontroller"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
斯威夫特:

 let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
 let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("tabBarcontroller") as UITabBarController  
   UIApplication.sharedApplication().keyWindow?.rootViewController = viewController;
Swift 3:

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = viewController
Swift 5:

let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.windows.first?.rootViewController = viewController
UIApplication.shared.windows.first?.makeKeyAndVisible()
或者简单地说:

let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.windows.first?.rootViewController = viewController
UIApplication.shared.windows.first?.makeKeyAndVisible()
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()

两者都很好

在主情节提要中为类设置情节提要ID

UIStoryboard *MainStoryboard = [UIStoryboard storyboardWithName:@"Main"
                                                                     bundle: nil];

UINavigationController *controller = (UINavigationController*)[MainStoryboard
                                                                           instantiateViewControllerWithIdentifier: @"RootNavigationController"];


LoginViewController *login=[MainStoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
            [controller setViewControllers:[NSArray arrayWithObject:login] animated:YES];
self.window.rootViewController=controller;
我使用简单的方法:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"NameOfStoryBoard" bundle:nil];
UITabBarController *rootViewController = [sb instantiateViewControllerWithIdentifier:@"NameOfTabBarController"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];

在swift中,我们可以按如下方式实施

let storyboard = UIStoryboard(name: "StartingPage", bundle: NSBundle.mainBundle())      
let loginView: SignInVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInVC
UIApplication.sharedApplication().keyWindow?.rootViewController = loginView

为了补充Sunny Shah的答案,这是它的Swift 3版本:

        let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController: UIViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
        UIApplication.shared.keyWindow?.rootViewController = viewController
Swift 3代码:

在didFinishLaunchingWithOptions Appdelegate函数中使用以下内容。 将“HomeViewController”替换为要在应用程序启动时设置为根ViewController的ViewController

self.window = UIWindow(frame: UIScreen.main.bounds)

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let initialViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController")

self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()

return true
目标c

步骤1:从info.plist中删除主故事板

步骤2:在界面生成器中将情节提要id添加到视图控制器

步骤3:将以下代码添加到app delegate中的application did finish方法

self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];


//set main story board
if( condition){
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardName1" bundle:nil];
    UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
    [[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
    [self window].rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
}else{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardName2" bundle:nil];
    UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
    [[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
    [self window].rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
}

这是一篇旧文章,但我会回复。 我不建议使用以下代码

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = viewController
因为您正在创建两个实例。 我建议在适当的ViewController中编写以下代码

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = self

Swift 5+Xcode 11:

let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.windows.first?.rootViewController = viewController
UIApplication.shared.windows.first?.makeKeyAndVisible()
这样做:

let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.windows.first?.rootViewController = viewController
UIApplication.shared.windows.first?.makeKeyAndVisible()
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()
或者像这样:

let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.windows.first?.rootViewController = viewController
UIApplication.shared.windows.first?.makeKeyAndVisible()
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()

两者都很好

那么,从plist文件中删除情节提要并取消选中IntalViewController如何。另外,newRootVC不在故事板中。没有故事板将在那里您的根视图控制器将被更改Hello,现在在应用程序启动时,我将我的productTour VC作为rootVC,现在当用户退出productTour时,我想再次将我的根VC移到TabBar,如何操作我已经编辑了代码并提交了编辑。我正在我的应用程序中使用它。只想注意,在
使用选项完成启动期间,
应用程序还没有设置keyWindow,所以你应该使用
self.window.rootViewController=…
在这种情况下,与我的问题中的上述情况相同…请检查Hello@Mayank,你收到了吗。伙计们,对不起,应用程序BecomeActive中有一些代码导致了此问题,现在问题已经解决。谢谢大家,你说得对。您好,现在在应用程序启动时,我让我的productTour VC作为根VC,现在当用户退出productTour时,我想再次将我的根VC转移到TabBar,如何做到这一点?此代码段与公认的答案有何不同?谢谢您的想法。这对我很有用:
func应用程序(application:UIApplication,didFinishLaunchwithOptions launchOptions:[uiApplicationLaunchOptions:Any]?)->Bool{if ProcessInfo.ProcessInfo.arguments.contains(“xuitests”){window?.layer.speed=100.0//在模拟器中加速测试大约3x}返回true}