不同的初始viewcontroller

时间:2014-05-20 08:00:18

标签: ios objective-c uiviewcontroller uisplitviewcontroller

我正在构建一个应用,用户可以从另一个ViewController中选择一个项目,然后打开一个分割ViewController。我发现我无法将另一个ViewController设置为初始,但我已经找到了辅助解决方案。

我将拆分视图设置为初始值,然后导航到ViewController方法上的假初始viewDidAppear,就好像它是初始的一样。然后在全局变量中设置所选项目并关闭项目页面,如下所示:

[self dismissModalViewControllerAnimated:YES]

打开splitview

我的问题: 我可以使用这种方法吗?如果应用程序按预期工作,苹果会批准它吗?

谢谢!

修改

这段代码在我的AppDelegate.m中,它应该改成什么?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UISplitViewController *splitviewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitviewController.viewControllers lastObject];
splitviewController.delegate = (id)navigationController.topViewController;
return YES;
}

编辑2:

// Override point for customization after application launch.
UISplitViewController *splitviewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitviewController.viewControllers lastObject];
splitviewController.delegate = (id)navigationController.topViewController;

如何将self.window.rootViewController引用到SplitViewController?因此,它并不认为这个视图控制器将会主持" splitview?

编辑3:

// Close the ProjectsViewController and open the selected project
[self presentViewController:_detailViewController animated:YES completion:^{
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController:_detailViewController];
}];

编辑4:

* 由于未捕获的异常终止应用' NSInvalidArgumentException',原因:'应用程序尝试以模态方式呈现拆分视图控制器

方法与按钮链接:

-(void)selectedProject {
// The user opened a project

// Override point for customization after application launch.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; // assuming your split view controller in storyboard with name "Main" in project's main bundle
UIViewController *splitViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"splitVC"];

UISplitViewController *splitviewController = (UISplitViewController *)splitViewController;
UINavigationController *navigationController = [splitviewController.viewControllers lastObject];
splitviewController.delegate = (id)navigationController.topViewController;

// Close the ProjectsViewController and open the selected project
[self presentViewController:splitViewController animated:YES completion:^{
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController:splitViewController];
}];
}

显示projectsPage:

- (IBAction)closeProject:(id)sender {
// Show the selectSubjectBanner
_selectSubjectBanner.hidden = NO;

// Save the current subject in case the user edited it
[self saveCurrentSubject];

// Present the ProjectsViewController
[self performSegueWithIdentifier:@"projectsSegue" sender:nil];
}
}

关闭项目页面:

- (void)selectedProject {
// The user opened a project

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; // assuming your split view controller in storyboard with name "Main" in project's main bundle
UISplitViewController *splitViewController = (UISplitViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"splitVC"];

UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;

// Close the ProjectsViewController and open the selected project
[UIView transitionFromView:[[[[[UIApplication sharedApplication] delegate] window] rootViewController] view]
                    toView:splitViewController.view
                  duration:0.5
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                completion:^(BOOL finished) {
                    [[[[UIApplication sharedApplication] delegate] window] setRootViewController:splitViewController];
                }];
}

2 个答案:

答案 0 :(得分:1)

最好设置窗口rootViewController

[[[[UIApplication sharedApplication] delegate] window] 
                     setRootViewController:yourViewController];

编辑:以下是如何从故事板中实例化视图控制器的代码,

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; // assuming your split view controller in storyboard with name "Main" in project's main bundle
UIViewController *splitViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"splitVC"];

确保在故事板中设置了视图控制器的Storyboard ID,Identity Inspector选项卡,splitVC(例如)

编辑:因此,UISplitViewController本身就是一个容器视图控制器,因此我们无法在UINavigationController,UITabBarController中添加它,添加为childViewController,..等等我们也无法呈现它在模态上,因此我们不会得到动画演示。 UISplitViewController应该是窗口的rootViewController。在rootViewController和splitViewController之间获得动画过渡的最简单方法是使用UIView的transitionFromView类方法,并在改变rootViewController属性时可视地交换它们的视图。这是您的代码编辑。

- (void)selectedProject {
    // The user opened a project

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; // assuming your split view controller in storyboard with name "Main" in project's main bundle
    UISplitViewController *splitViewController = (UISplitViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"splitVC"];

    UINavigationController *navigationController = [splitviewController.viewControllers lastObject];
    splitViewController.delegate = (id)navigationController.topViewController;

    // Close the ProjectsViewController and open the selected project
    [UIView transitionFromView:[[[[[UIApplication sharedApplication] delegate] window] rootViewController] view]
                        toView:splitViewController.view
                      duration:0.5
                       options:UIViewAnimationOptionTransitionFlipFromLeft
                    completion:^(BOOL finished) {
                        [[[[UIApplication sharedApplication] delegate] window] setRootViewController:splitViewController];
                    }];
}

答案 1 :(得分:1)

您可以将任何viewcontroller设置为初始视图控制器

您无法在viewDidAppear方法中启动viewcontroller。太糟糕了。

Apple在考虑应用转换时不会拒绝应用。这取决于开发者。

您可以通过重置窗口

的rootviewcontroller来设置任何视图控制器

而不是故事板检查您的逻辑并从appdelegate启动必要的viewcontroller。如Seryozha

所述,分别替换窗口rootViewController