Check iphone for text box added in uialertview

I added a UITextField to the UIAlertView using the UIAlertViewStylePlainTextInput. I need to check the text box present in alertview, i.e. It should not be empty.

How should I do it?

+5
source share
5 answers

Suppose you have an OK button (or something similar) as the first of the other UIAlertView buttons. Next, suppose you want this button to turn on if and only if the length of the text in the text field is greater than 0. Then the solution for checking is simple. In the delegate of the UIAlertView implementation:

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    return [[alertView textFieldAtIndex:0].text length] > 0;
}

( clickedButtonAtIndex:), , .

Apple, . , "" .

+11

UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
0

" " File Owner . - , controller.m controller.h. .

, . , . , , , reset focus .

iOS, Ray Wnderlich. http://www.raywenderlich.com/

, " iOS" . , " iOS 5".

0

alertView viewController

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)return; //for cancel button

    UITextField *textField = [alertView textFieldAtIndex:0]; // since there is only one UITextField in alertView
    if ([textField.text length] > 0) // checking the length of the text in UITextField
    {
           // Your code goes here 
    }
}

, . BR,

0

1: UITextField alertView:

self.alertViewTextField = [alertView textFieldAtIndex:0];

2: Check the length of the text when changing the editing of the text field:

[self.alertViewTextField addTarget:self action:@selector(alertViewTextFieldDidChanged) forControlEvents:UIControlEventEditingChanged];

-(void)alertViewTextFieldDidChanged{
    if(self.alertViewTextField.text.length == 0){
        // ...
    }
}
0
source

All Articles