UISplitViewController programmatically without nib / xib

I usually create my projects without IB materials. The first thing I do is to remove all xibs links, update updated feeds, etc. Etc. No problem, works great (in my world)!

Now I just installed 3.2 and tried to develop the first iPad app. Following the same procedure as before, I created an application project based on UISplitView and deleted all IB elements. In addition, I followed the section in Apple reference documents: Creating a controller with a split view programmatically , but nevertheless, the master view is never displayed, only the Detailed view (regardless of orientation). I really tried to study this carefully, but I can’t understand what I missed.

Is there a working example of a UISplitViewController without buns floating around somewhere? I have googled, but I could not find. Or do you know what I might have missed?

+46
objective-c ipad uisplitviewcontroller
May 03 '10 at 10:47 a.m.
source share
4 answers

Declare your splitview controller in your delegate header, use something like this in your dofinishlaunching

make sure you add the UISplitViewControllerDelegate to the detailViewController header file and that you also have delegate methods. don't forget to import the corresponding header files

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { splitViewController = [[UISplitViewController alloc] init]; rootViewController *root = [[rootViewController alloc] init]; detailedViewController *detail = [[detailedViewController alloc] init]; UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:root]; UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detail]; splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, detailNav, nil]; splitViewController.delegate = detail; [window addSubview:splitViewController.view]; 

EDIT - as per Scott's recommendations below, don't add a subview to the window, instead

  [self.window setRootViewController:(UIViewController*)splitViewController]; // that the ticket [window makeKeyAndVisible]; return YES; } //detailedView delegate methods - (void)splitViewController:(UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController:(UIPopoverController*)pc { [barButtonItem setTitle:@"your title"]; self.navigationItem.leftBarButtonItem = barButtonItem; } - (void)splitViewController:(UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem { self.navigationItem.leftBarButtonItem = nil; } 

I also prefer code for IB; -)

+58
Jun 04 2018-10-06T00:
source share

A lone thread, but I thought that I would get rid of the reader’s time + grief when the above method could not create a UISplitViewController that correctly responded to device orientation changes. You will need:

  • Ensure that all subviews respond properly shouldAutorotateToInterfaceOrientation. Nothing new here.
  • Instead of adding the UISplitViewController view to the main window

     [window addSubview:splitViewController.view]; // don't do this 

    instead, install the root controller of the main window in the UISplitViewController:

     [self.window setRootViewController:(UIViewController*)splitViewController]; // that the ticket 

Adding the splitviewcontroller view as a subtask of the main window (barely) allows it to be present together with the siblings' views, but it does not fly with the supposed use case of the UISplitViewController. UISplitViewController is a view of highlanders; there can be only one.

+23
Jan 17 '12 at 1:35
source share

I just met the same problem. make sure your child viewController splitview can authorize the orientation of the interface.

you can change the function in childViewController as follows:

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } 

then the main view will be shown.

+4
Feb 15 '12 at 7:32
source share

Swift version 5.0 (both primary and detailed view controllers are integrated into navigation controllers)

  let splitViewController = UISplitViewController() splitViewController.delegate = self let masterVC = MasterViewController() let detailVC = DetailViewController() let masterNavController = UINavigationController(rootViewController: masterVC) let detailNavController = UINavigationController(rootViewController: detailVC) splitViewController.viewControllers = [masterNavController,detailNavController] 

You can put this code in the didFinishLaunchingWithOptions didFinishLaunchingWithOptions function. Just remember to make splitViewController your rootViewController as follows

  self.window!.rootViewController = splitViewController 
0
May 02 '19 at 21:44
source share



All Articles