I got it!
You have to add these methods:
Preparation:
Code:
<UITextFieldDelegate> //import the Delegate protocol
- (void) presentSheet; //declare method in header file
Show the alert:
Code:
- (void) presentSheet {
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"New Alert" message:@"your message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[myAlert addTextFieldWithValue:nil label:@"I'm the Placeholder"];
[[myAlert textField] setDelegate:self];
[[myAlert textField] setTextAlignment:UITextAlignmentCenter];
[[myAlert textField] becomeFirstResponder];
[myAlert show];
[myAlert release];
myAlert = nil;
}
Get clicked button and string:
Code:
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{ if ([alertView title] == @"New Alert") { //Only for one specific alert
NSString *myString = [[alertView textField]text]; //Get the string
NSLog(@"User Pressed Button %d and String is: %@", buttonIndex + 1, myString); //Put it on the debugger
if ([[[alertView textField]text]length] <= 0 || buttonIndex ==0)
return; //If cancel or 0 length string the string doesn't matter
if (buttonIndex == 1) {
//Setup an object or do sth. else here
}
}
}
Done button:
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"Dismiss Return");
[self alertView:(UIAlertView *)[textField superview] clickedButtonAtIndex:1];
[(UIAlertView *)[textField superview] dismissWithClickedButtonIndex:1 animated:NO];
return NO;
}