Alejandro L.
Alejandro L.

Reputation: 1076

UITextField.text property return nil

I have this code: (nomEquipo is a UITextField class variable)

nomEquipo = [[UITextField alloc] initWithFrame: CGRectMake(5, yPosition, modificar.frame.size.width - 10, 30)];
[nomEquipo setPlaceholder: @"Nombre del equipo"];
[nomEquipo setBorderStyle: UITextBorderStyleRoundedRect];
[nomEquipo setAutocorrectionType: UITextAutocorrectionTypeNo];
[nomEquipo setReturnKeyType: UIReturnKeyDone];
[nomEquipo addTarget: self action: @selector(ocultarTeclado:) forControlEvents: UIControlEventTouchUpInside];
[modificar addSubview: nomEquipo];

then in other method:

- (IBAction) crearEquipoReal:(id)sender
{
    NSLog(@"%@",nomEquipo.text);
    if ([nomEquipo.text length] == 0)
    {
        UIAlertView *alerta = [[UIAlertView alloc] initWithTitle: @"Aviso"
                                                         message: @"Debe introducir al menos el nombre del equipo"
                                                        delegate: self
                                               cancelButtonTitle:@"Aceptar"
                                               otherButtonTitles:nil, nil];
        [alerta show];
    }
}

This NSLog returns:

2013-04-11 15:40:10.184 GeoRuta_v1[7157:907] (null)

Code where I put text in my UITextFields:

-(void)didChangeComboBoxValue:(AJComboBox *)comboBox selectedIndex:(NSInteger)selectedIndex
{
    if (comboBox == self.combo)
    {
        equipoActual = (Equipo *) [listaEquipos objectAtIndex: selectedIndex];
        nomEquipoActual = equipoActual.nombreEquipo
        user.idEquipo = equipoActual.idEquipo;
    }
    else if (comboBox == self.equiposMod)
    {
        Equipo *aux = (Equipo *) [listaEquiposMod objectAtIndex: selectedIndex];
        user.idEquipo = aux.idEquipo;
        descEquipo2.text = aux.descEquipo;
        nomEquipo2.text = aux.nombreEquipo;
    }
}

My surprise comes when it happens suddenly, it has been working for 2 days I programmed it.

Thanks for your help.

EDIT1: * [FIX] I fixed with 2 UITextField, because I was generating that nomEquipo twice .... :(

Upvotes: 1

Views: 1887

Answers (1)

Ben M
Ben M

Reputation: 2475

It's likely that the nomEquipo pointer is referencing nil and that's why you are seeing (null). Nil objects can receive selectors, they will just return nil.

You should debug your code with a breakpoint and "po nomEquipo" in the console before that code executes.

Upvotes: 2

Related Questions