Mi sugerencia sería no implementar la funcionalidad Agregar como un botón en la barra de navegación. Supongo que está tratando con una vista de tabla de los elementos a continuación, por lo que una forma de manejar esta interacción del usuario es mostrar una opción "Agregar nuevo elemento" como la última entrada en su vista de tabla. Esto podría ser desvaneció mediante programación en cuando el usuario pulsa en el botón Editar en la barra de navegación mediante la aplicación del método siguiente delegado:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView beginUpdates];
[self.tableView setEditing:editing animated:YES];
if (editing)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView endUpdates];
}
A continuación, tendría que asegurarse de que la fila adicional se explica por el aumento del recuento de filas utilizando el siguiente:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.editing)
return ([objects count] + 1);
else
return [objects count];
}
y luego mostrando el signo más verde a su izquierda, en contraposición con el estilo normal de edición de eliminación:
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
if (indexPath.row >= [objects count])
return UITableViewCellEditingStyleInsert;
else
return UITableViewCellEditingStyleDelete;
return UITableViewCellEditingStyleNone;
}
Por supuesto, deberá proporcionarle un nombre en su cellForRowAtIndexPath: implementación y manejar su selección de filas, también.
significa esto realmente funciona? He intentado esto sin luc _viewTypeButton = [[UIButton alloc] initWithFrame: CGRectMake (0,0,20,20)]; _viewTypeButton.titleLabel.text = @ "R"; [self.navigationController.navigationBar addSubview: _viewTypeButton]; k. – roocell