Search code examples
objective-cios6viewcontroller

The UIButton not responding as it should in single-view app in iOS6


The selector 'go' with IBAction return type is not responding correctly. If the button is clicked with the text field being empty i.e its value being nil , the flow should return the 'if' part and not 'else'. But it works fine when I click the button second time. what can be the problem ?

Below is the code from implementation file i.e. ViewController.m where I have implemented the go selector.

@synthesize textField = _textField;


- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    //Clear the text field
    self.textField.text =nil;
}

NSString *s;

-(IBAction)go:(id)sender
{
    [self.textField resignFirstResponder];

    if (self.textField.text == nil)
    {
        s = [NSString stringWithFormat:@" Enter Your Name First " ];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Blank Field : "
                                                        message:s
                                                       delegate:self
                                              cancelButtonTitle:@"OKAY"
                                              otherButtonTitles:nil];
         [alert show];
    } 
    else
    {
        s = [NSString stringWithFormat:@"Hello %@ " , self.textField.text];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello "
                                                        message:s
                                                       delegate:self
                                              cancelButtonTitle:@"Thanks"
                                              otherButtonTitles:nil];

        [alert show];    
    }
}

Please help. Thanks in advance.


Solution

  • Instead of using nil as your comparator I would instead use something more concrete like

    if ([self.textfield.text length] > 0)
    {
    
    }
    else
    {
    
    }