Xcode 4 Generic Application Questions

I am trying to create a universal application with Xcode 4. However, it is slightly different from previous versions.

My project uses a view-based application template. My problem is that I added a subclass of UIViewController which has one nib file for iPad. How to create another file with the same class instead of iPhone? Also, how can I ensure that the correct nib file is uploaded for the correct platform?

EDIT: here is my code:

 - (IBAction)BookView { if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { BookViewController *viewController = [[BookViewController alloc] initWithNibName:@"BookViewController" bundle:nil]; [self presentModalViewController:viewController animated:YES]; } else { BookViewController *viewController = [[BookViewController alloc] initWithNibName:@"BookViewController_iPad" bundle:nil]; [self presentModalViewController:viewController animated:YES]; } } 
+2
source share
2 answers

Step 1: Create for iPhone:

  • New file / ios / cocoa touch / UIViewController subclass
  • uncheck "Target for iPad"
  • check with xib

This step will create .mh and .xib files with the same name, for example: CustomView

Step 2: Create a New XIB for iPad:

  • New file / ios / user interface / view iPad device family
  • for convenience, select the same name with the suffix _iPad (for example, CustomView_iPad)
  • in this xib, go to File Owner, on the inspector tabs, select the ID card inspector, a custom class, and select the same class name created in step 1.
  • Connect IBOutlets.

In your code, use something like this:

 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { CustomView *viewController = [[CustomView alloc] initWithNibName:@"CustomView" bundle:nil]; } else { CustomView *viewController = [[CustomView alloc] initWithNibName:@"CustomView_iPad" bundle:nil]; } 

Good luck

+6
source

Name your xib file for iPad BookViewController ~ ipad.xib and iPhone one BookViewController ~ iphone.xib . Then download the nib file as usual:

 BookViewController *viewController = [[BookViewController alloc] initWithNibName:@"BookViewController" bundle:nil]; [self presentModalViewController:viewController animated:YES]; 

When the application launches on the iPad, ~ ipad xib will be automatically downloaded. If the application runs on iPhone, ~ iphone xib will be automatically downloaded.

Note that the suffixes ~ ipad and ~ iphone are case sensitive. If you name it ~ iPad, for example, you will get a runtime exception if the nib file is not found.

+3
source

Source: https://habr.com/ru/post/902154/


All Articles