2012-01-06 15 views
9

Quiero diferenciar el controlador para iPhone y iPad.¿Cómo diferenciar iPhone y iPad en una aplicación universal?

 #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]; 
    } 

si el error diciendo #ifdef sin terminar

¿Es este enfoque correcto?

Respuesta

17

Usted puede hacer uso de las constantes que ya están presentes:

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

Naturalmente usted no necesitaría la declaración else if, es posible que utilices else sin embargo sólo estoy usando que ilustran las constantes de diferencia disponible.

Puede encontrar más here (vea la sección en UI_USER_INTERFACE_IDIOM).

+0

gracias Crazy Chimp! – user905582

+0

no hay problema - ¡me alegro de poder ayudar! –

+0

La aplicación debe ser universal para que se ejecute el bloque de iPad. –

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

Buenas respuestas acompañan ejemplos de código con una explicación para futuros lectores. Si bien la persona que hace esta pregunta puede comprender tu respuesta, explicar cómo llegaste a ella ayudará a muchos otros. –

Cuestiones relacionadas