UIAlertView dismiss on return

15,776

Your class needs to implement the UITextFieldDelegate to get the keyboard return callback. As suggested by borrden, this can be done as follows

[message textFieldAtIndex:0].delegate = self;

Then implement this method,

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // Dismiss the alert view 
    // Initiate the search
    return YES;
}

Use this UIAlertView method. Refer Apple docs

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated

Call this on your alert view instance when required to dismiss it programmatically. You would require to keep message as an ivar to access it when required.

Hope that helps!

Share:
15,776
Joran Den Houting
Author by

Joran Den Houting

Updated on June 04, 2022

Comments

  • Joran Den Houting
    Joran Den Houting almost 2 years

    I'm having a problem with my search alert when using the search-tab. When someone is typing their search-string and hit return it will only dismiss the keyboard and you still have to tab the "Search" (Zoek) button do actually start a websearch.

    Here is my code:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    NSString *inputText = [[alertView textFieldAtIndex:0] text];
    if( [inputText length] >= 1 )
    {
        [searchview addSubview:indicator];
        NSString *urlAddress= @"http://www.sportdirect.com/articles?search=";
        urlAddress =[urlAddress stringByAppendingString:inputText];
        NSURL *url= [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj= [NSURLRequest requestWithURL:url];
        [searchview loadRequest:requestObj];
        [[searchview scrollView] setBounces: NO];
        timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
        [TestFlight passCheckpoint:@"Voerde zoekopdracht uit"];
    
    }
    }
    
    -(BOOL)textFieldShouldReturn:(UITextField*)textField;
    {
    [_message dismissWithClickedButtonIndex:(0) animated:YES];
    NSString *inputText = [[_message textFieldAtIndex:0] text];
    if( [inputText length] >= 1 )
    {
        [searchview addSubview:indicator];
        NSString *urlAddress= @"http://www.sportdirect.com/articles?search=";
        urlAddress =[urlAddress stringByAppendingString:inputText];
        NSURL *url= [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj= [NSURLRequest requestWithURL:url];
        [searchview loadRequest:requestObj];
        [[searchview scrollView] setBounces: NO];
        timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
        [TestFlight passCheckpoint:@"Voerde zoekopdracht uit"];
    
    }
    
    [textField resignFirstResponder];
    return NO; // We do not want UITextField to insert line-breaks.
    }
    

    Does anyone know how to actually do a search as soon as the return button on the keyboard is tabbed?

    Thanks in advance!