2010-02-16 42 views

Respuesta

9
  1. Ir al iPhone Interface Guidelines Page.

  2. En "Botones estándar para su uso en filas de tabla y otros elementos de la interfaz de usuario" Copie el botón ContactAdd (lo guardo como ContactAdd.png aquí). Agrégalo a tu proyecto.

  3. En el cellForRowAtIndexPath: (NSIndexPath *) Método indexPath complemento:

    UIImage *image = [UIImage imageNamed:@"ContactAdd.png"]; 
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    //You can also Use: 
    //UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
    
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height); 
    
    //match the button's size with the image size 
    button.frame = frame; 
    
    [button setBackgroundImage:image forState:UIControlStateNormal]; 
    
    // set the button's target to this table view controller so you can open the next view 
    [button addTarget:self action:@selector(yourFunctionToNextView:) forControlEvents:UIControlEventTouchUpInside]; 
    
    button.backgroundColor = [UIColor clearColor]; 
    
    cell.accessoryView = button; 
    
+12

* NOTA: En lugar de copiar y agregar, también puede usar: [UIButton buttonWithType: UIButtonTypeContactAdd]; en lugar de UIButtonTypeCustom. – erastusnjuki

+1

La parte difícil de hacer es pasar parámetros a yourFunctionToNextView: para saber qué fila se presionó. esto se puede hacer fácilmente con button.tag = row; pero si tiene secciones en su tabla, una sola int no es lo suficientemente buena. Necesitas pasar indexPath. Encontré este método aquí para hacer tal cosa. http://stackoverflow.com/questions/5500327/subclass-uibutton-to-add-a-property – roocell

+0

Sí, pasar el parámetro es la parte difícil. Su enlace está casi completo, excepto que no le indica cómo pasar la propiedad que agrega a la clase UIButton. Solo para completar ese pensamiento, el parámetro se pasa solo si incluye el ":" después de "yourFunctionToNextView:" en el selector. Por lo tanto, el ejemplo anterior se extendería con button.property = indexPath. Entonces, la definición del selector sería: - (void) yourFunctionToNextView: (UIButton *) sender {}. Luego, dentro de este método, puede usar algo como NSIndexPath * indexPath = sender.property; donde propiedad es el indexPath. – JeffB6688

27

Si usted tiene una barra de navegación se debe añadir un UIBarButtonItem así:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]  
    initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
    target:self action:@selector(addButtonPressed:)]; 
self.navigationItem.rightBarButtonItem = addButton; 
+0

Gracias. Me ayudó. Votación para tí. – DrinkJavaCodeJava

2

para SWIFT

let addButton = UIBarButtonItem.init(barButtonSystemItem: .Add, 
            target: self, 
            action: #selector(yourFunction)) 
self.navigationItem.rightBarButtonItem = addButton 
Cuestiones relacionadas