简体   繁体   English

带有Storyboard的通用应用程序上的UISplitViewController

[英]UISplitViewController on universal app with Storyboard

I'm want to make an app that uses a UISplitViewControler on the iPad (as far as I understand it's only available on the iPad) but I want the app to be universal. 我想制作一个在iPad上使用UISplitViewControler的应用程序(据我所知,它仅适用于iPad)但我希望该应用程序具有通用性。

The setup is like this: 设置如下:

I have a UITableView (as a master view) and when I select a row it should display a detail view of that cell. 我有一个UITableView (作为主视图),当我选择一行时,它应该显示该单元格的详细视图。 I am using a Storyboard and I can't figure out how to implement the split view only for the iPad. 我正在使用故事板,我无法弄清楚如何仅为iPad实现拆分视图。

What would be the easiest way to achieve that? 最简单的方法是什么? Thanks. 谢谢。

You don't need two storyboards to do this.You can use them both in a single storyboard.For iphone ,we normally use a class SWRevealViewController (if you are new to iOS coding ..:)) for side menu and splitviewcontroller for ipad.We can also use SWRevealViewController for ipad as well.It depends on your requirement. 你不需要两个故事板来做到这一点。你可以在一个故事板中使用它们。对于iphone,我们通常使用类SWRevealViewController (如果你是iOS编码的新手:)。)用于侧面菜单和splitviewcontroller for ipad我们也可以使用SWRevealViewController for ipad。这取决于你的要求。

For universal apps,create viewcontrollers using size Classes (usually we use any height any width for universal apps ). 对于通用应用程序,使用大小类创建viewcontrollers(通常我们使用任何高度任何宽度的通用应用程序)。

change these size classes and create different viewcontrollers for ipad and iphones as required. 更改这些大小类,并根据需要为ipad和iphone创建不同的viewcontrollers。 In most cases any height any width will do the job. 在大多数情况下,任何宽度都可以完成任务。

After creating the viewcontrollers,in the appdelegate ,using the instantiateViewcontrollerWithIdentifier method, load the required viewcontroller. 创建viewcontrollers后,在appdelegate中,使用instantiateViewcontrollerWithIdentifier方法,加载所需的viewcontroller。

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  // The device is an iPad running ios 3.2 or later.

}
else {
  // The device is an iPhone or iPod touch.
}

For ipad load the splitviewcontroller. 对于ipad加载splitviewcontroller。 and swrevealviewcontroller for iPhone. 和iPhone的swrevealviewcontroller。

This is the core basics.If you need any more information,let me know. 这是核心基础。如果您需要更多信息,请告诉我。

EDIT 编辑

Have you seen an arrowmark ath the initial VC(viewcontroller) in the storyboard?This vc is loaded first after the launch screen.In my app,I have a home screen which is common to both iphone and ipad(using size classes as mentioned above).So I can set this vc as the initial VC.In this case I don't have to do anything in the appdelegate.But if I have a different home screen for ipad,then I can make a condition check in the appdelegate didFinishLaunchingWithOptions 您是否在故事板中看到了初始VC(viewcontroller)的箭头标记?此vc在启动屏幕后首先加载。在我的应用程序中,我有一个主屏幕,这对于iphone和ipad都是常见的(使用上面提到的大小类因此,我可以将此vc设置为初始VC。在这种情况下,我不需要在appdelegate中执行任何操作。但是如果我有一个不同的ipad主屏幕,那么我可以在appdelegate中进行条件检查didFinishLaunchingWithOptions

You can load the First screen like this.You should follow through splitVC tutorilal and swrevealcontroller tutorial to set the side menu.You should load the SWrevealVC or splitViewcontroller only if the first screen contains the side menu. 您可以像这样加载First屏幕。您应该通过splitVC tutorilal和swrevealcontroller教程来设置侧面菜单。只有在第一个屏幕包含侧边菜单时才应加载SWrevealVC或splitViewcontroller。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UISplitViewController *split = [storyboard instantiateViewControllerWithIdentifier:@"SplitViewController"];
        [AppDelegate setRootController:split storyboard:storyboard actiontype:0];
    }
    else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *split = [storyboard instantiateViewControllerWithIdentifier:@"SWrevealVC"];
        [AppDelegate setRootController:split storyboard:storyboard actiontype:-1];
    }
return YES;
}

+(void)setRootController:(UIViewController*)controller
              storyboard:(UIStoryboard*)storyboard actiontype:(int) actiontype;
{
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && actiontype == 0)
    {
        UISplitViewController *splitViewController = (UISplitViewController *)controller;
        //splitViewController.presentsWithGesture = false;

        UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];
        SideMenuViewController *controller = (SideMenuViewController *)masterNavigationController.topViewController;
        controller.splitViewController = splitViewController;
        splitViewController.delegate = (id)controller;
    }

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    [UIView
     transitionWithView:appDelegate.window
     duration:0.5
     options:UIViewAnimationOptionAllowAnimatedContent
     animations:^(void) {
         BOOL oldState = [UIView areAnimationsEnabled];

         [UIView setAnimationsEnabled:NO];

         appDelegate.window.rootViewController = controller;

         [UIView setAnimationsEnabled:oldState];
     }
     completion:nil];
}

The code may look lengthy,but take it simple.You can only understand the logic if u do things. 代码可能看起来很冗长,但要简单一点。如果你做的话,你只能理解逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM