网站:
http://www.cnblogs.com/top5/archive/2012/05/17/2506623.html
核心代码:
- (void) enterNickName:(NSString*)msg
{
prompt = [[UIAlertViewalloc] initWithTitle:@"设置昵称"
message:msg
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
UITextField *textField = [[UITextFieldalloc] initWithFrame:CGRectMake(27.0,70.0, 230.0, 30.0)];
[textField setBackgroundColor:[UIColorwhiteColor]];
[textField setPlaceholder:@"输入昵称"];
textField.layer.cornerRadius=6.0;
textField.delegate=self;
textField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
[prompt addSubview:textField];
[textField becomeFirstResponder];
textField.keyboardType=UIKeyboardTypeDefault;
textField.keyboardAppearance=UIKeyboardAppearanceDefault;
textField.returnKeyType=UIReturnKeyDone;
[textField release];
[promptsetTransform:CGAffineTransformMakeTranslation(0.0, -40.0)]; //可以调整弹出框在屏幕上的位置
[prompt show];
}
#pragma textField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"nickname=%@",textField.text);
if (nil==textField.text ||0==[[textField.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] length])
{
prompt.message=@"\n\n昵称不能为空!";
return YES;
}
if ([[textField.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] length]<2
||[[textField.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] length]>15)
{
prompt.message=@"\n\n昵称只支持2-15个字,请重新设置";
return YES;
}
[prompt dismissWithClickedButtonIndex:0animated:NO];//触发dismiss
[prompt release];
}