Pregunta

He visto muchas de las aplicaciones que tienen un botón de información (la letra "i" con un círculo alrededor de él) en el UINavigationBar. ¿Cómo agrego este tipo de botón?

¿Fue útil?

Solución

La respuesta anterior estaba cerca, no acababa de compilar. Esto es lo que realmente quiere:

// Info button
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];

Otros consejos

No hay pérdida de memoria en la respuesta anterior. Debe utilizar "autorelease" para evitarlo. He tratado de editar la respuesta, pero no tuvo éxito hasta el momento.

// Info button
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];

ESTO ES LA CORRECTA manera de hacerlo.

Usted debe ser capaz de añadir un botón de tipo "Información de Luz" a la navigationItem del controlador de vista que el UINavigationController está mostrando actualmente. Algo como esto podría ir en su código de controlador de vista:

- (void) viewDidLoad {
  UIButton * info = [UIButton buttonWithType:UIButtonTypeInfoLight];
  // set your button's target and selector for whatever action here.
  self.navigationItem.rightBarButtonItem.customView = info;
}

También es posible usar selector de objetivo y el elemento actual de botón de la barra para su botón de información.

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self.barButtonItem.target action:self.barButtonItem.action forControlEvents:UIControlEventTouchUpInside];
self.barButtonItem.customView = infoButton;

De esta manera, cualquier acción o segues acoplados a su artículo botón de la barra será transferido a su botón de información.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top