Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS UIAlertview clear text and disable OK
    text
    copied!<p>I'm a bit new to iOS development, and right now am working on some simple UI-related stuff. I have a <code>UIAlertView</code> that I'm using at one point to allow the user to enter some text, with simple Cancel and OK buttons. The OK button should be disabled if the text field is blank.</p> <p>I added to my <code>UIAlertViewDelegate</code> an alertViewShouldEnableFirstOtherButton function, so the OK button would disable when there's no text, and I also set the <code>UIAlertView</code>'s <code>UITextField</code> to have <code>clearOnBeginEditing</code> true, so the previous text would be gone every time I displayed the alert. Each of these things works perfectly on their own. Unfortunately, it seems like the AlertView is checking whether or not to enable the OK button before the text field is cleared, so when they're put together it comes up enabled. Below should be about the minimal code needed to reproduce.</p> <pre><code>-(void)viewDidLoad { textEntryBox = [[UIAlertView alloc] initWithTitle:@"Name" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; [textEntryBox setAlertViewStyle:UIAlertViewStylePlainTextInput]; [textEntryBox textFieldAtIndex:0].clearsOnBeginEditing = YES; } -(IBAction)functionTriggeredByOtherLogic { [textEntryBox show]; } -(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { if(alertView == textEntryBox) { if([[alertView textFieldAtIndex:0].text length] &gt; 0) { return YES; } else { return NO; } } return YES; } </code></pre> <p>So, ultimately, my question is this: am I doing something completely against the natural iOS way of doing things here? Is there a better way to do this? Should I just ignore the <code>clearsOnBeginEditing</code> property of the <code>UITextField</code>, and manually clear the Text property before showing the <code>UIAlertView</code>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload