iOS: UIAlertView as an Input Dialog
Problem:
You want a simple input dialog to have the user enter a text in iOS.
Solution:
You can use an UIAlertView:
UIAlertView * inputAlert = [[UIAlertView alloc] initWithTitle:@"New Event" message:@"Enter a title for the event" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
inputAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
[inputAlert show];
You also need to add the UIAlertViewDelegate protocol to the view controller:
@interface ViewController : UIViewController
And the delegate method when the user taps the Cancel (index 0) or OK (index 1) buttons:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
NSLog(@"Entered: %@",[[alertView textFieldAtIndex:0] text]);
}
}
No comments yet.