2012-01-06 20 views
9

Voglio differenziare il controller per iPhone e iPad.Come differenziare iPhone e iPad nell'applicazione universale?

 #ifdef __IPHONE_NA 
      { 

      UINavigationBar *ipadNavBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 768.0f, 50.0f)]; 
      [[self view] addSubview: ipadNavBar]; 

      UINavigationItem *ipadNavItem = [[UINavigationItem alloc] initWithTitle: @"EMPLOYEE"]; 
      [ipadNavBar pushNavigationItem:ipadNavItem animated:NO]; 
      } 
    else 
     { 

     UINavigationBar *ipadNavBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 360.0f, 45.0f)]; 
     [[self view] addSubview: ipadNavBar]; 



UINavigationItem *ipadNavItem = [[UINavigationItem alloc] initWithTitle: @"EMPLOYEE"]; 
    [ipadNavBar pushNavigationItem:ipadNavItem animated:NO]; 
    } 

se dicendo errore #ifdef terminato

È questo approccio corretto?

risposta

17

Si può fare uso delle costanti già presenti:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
{ 
    // Some code for iPhone 
} 
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
{ 
    // Some code for iPad 
} 

Naturalmente non avrebbe bisogno la dichiarazione else if, si potrebbe utilizzare else però sto solo usando illustrare le costanti di differenza a disposizione.

È possibile trovare ulteriori informazioni here (vedere la sezione UI_USER_INTERFACE_IDIOM).

+0

grazie Crazy Chimp! – user905582

+0

nessun problema - felice di poterti aiutare! –

+0

L'app deve essere universale per il blocco di iPad da eseguire. –

2
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
     NSLog(@"iPad Idiom"); 
    else 
     NSLog(@"iPhone Idiom"); 
0
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone) { 

    Console.WriteLine("Phone"); 

} else if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) { 

    Console.WriteLine("Pad"); 

} 
+3

Le buone risposte accompagnano i campioni di codice con una spiegazione per i futuri lettori. Mentre la persona che fa questa domanda può capire la tua risposta, spiegare come sei arrivato aiuterà molti altri. –

Problemi correlati