我想在应用程序启动开始时显示一个视频,就像启动一样。我的整个应用都将在导航中。首先,我在splashViewController中添加视频,并在appDelegate中将其设置为rootView,然后再次将mainViewController设置为rootView。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
splashViewController = [[SplashViewController alloc] init];
splashViewController = (SplashViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"SplashViewController"];
self.window.rootViewController = splashViewController;
[NSTimer scheduledTimerWithTimeInterval:9.0 target:self selector:@selector(removeSplashScreen:) userInfo:nil repeats:NO];
return YES;
}
-(void)removeSplashScreen:(id)userInfo
{
[splashViewController removeFromParentViewController];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
mainViewController = [[MainViewController alloc] init];
mainViewController = (MainViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"MainViewController"];
self.window.rootViewController = mainViewController;
}
现在我想从这个mainViewController开始导航。我使用storyBoard&在mainViewController中添加Editor > Embed in > Navigation Controller
的导航。不要以编程方式执行此操作。但我知道使用Nib.xib实现它的方法。在这里,我还添加了arrow
(表示起始VC的箭头)标记。但是当我从mainVC点击一个按钮进入下一个VC时它会崩溃。我如何在这里设置导航?
如果有人有答案,请与我分享。非常感谢提前。
我的故事板的场景:
答案 0 :(得分:1)
确定尽管我的建议(在评论中),但您可以通过以下方式实现这一目标:
显示启动:
//StoryBoard
UIStoryboard *mainStoryboard = [UIStoryboard
storyboardWithName:@"Main" bundle: nil];
//Splash Controller
SplashViewController *splashViewController = [mainStoryboard
instantiateViewControllerWithIdentifier:@"SplashViewController"];
//Show it
[self.window.rootViewController presentViewController:splashViewController
animated:NO completion:nil];
删除它:
[self dismissViewControllerAnimated:YES completion:nil];
也许你叫它快。这样做:
<强> viewDidLoad中强>:
[NSTimer scheduledTimerWithTimeInterval:9.0
target:self
selector:@selector(removeSplashScreen:)
userInfo:nil repeats:NO];
删除功能:
-(void)removeSplashScreen:(id)userInfo
{
[self dismissViewControllerAnimated:YES completion:nil];
}
答案 1 :(得分:1)
很高兴通过两个步骤为您提供帮助
步骤1- 你的didFinishLaunchingWithOptions代码几乎是正确的,但它的工作原理并不重要。所以让我们专注于第二步。第2步 - (void)removeSplashScreen:(id)userInfo
-first line is good , but you even dont need it because you are going to switch the root controller (leave it for now)
- second line (no need to write it)
- third line is really bad , what you doing here is trying to alloc you mainviewcontroller. really you need it with storyboard? no
- you should load you main view controller from storyboard
MainViewController* mainViewController = (MainViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"MainViewController"];
“此行足以从故事板加载主视图控制器” - 下一行是正确的 self.window.rootViewController = mainViewController; 代码似乎现在正在工作,但你仍然缺少导航控制器,因为你已经从故事板加载了mainviewcontroller,而不是导航控制器。
- two solution for it
1- load navigation controller in place of mainviewcontroller(give a storyboard id to navigation controller , load it , and make it as root controller) --i will always go with it
2- make a navigation controller with allocating it & make its rootviewcontroller the mainviewcontroller
-----------------工作代码---------------
答案 2 :(得分:0)
所以解决方案,根据pawan给出的答案是:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
splashViewController = (SplashViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"SplashViewController"];
self.window.rootViewController = splashViewController;
[NSTimer scheduledTimerWithTimeInterval:9.0 target:self selector:@selector(removeSplashScreen:) userInfo:nil repeats:NO];
return YES;
}
-(void)removeSplashScreen:(id)userInfo
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
mainViewController = [[MainViewController alloc] init];
mainViewController = (MainViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"MainViewController"];
navigationContoller = [[UINavigationController alloc] initWithRootViewController:mainViewController];
self.window.rootViewController = navigationContoller;
}
:)