sha*_*jem 21 iphone cocoa-touch objective-c ios
我需要一个补充TextField
到UIAlertView
.我知道苹果不鼓励这种做法.那么,有没有,我可以利用的添加任何图书馆TextField
的UIAlertView
外观相似框架?
Shm*_*idt 69
对于iOS5:
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Please enter someth" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].delegate = self;
[av show];
Run Code Online (Sandbox Code Playgroud)
此外,您将需要实现UITextFieldDelegate
,UIAlertViewDelegate
协议.
小智 14
不幸的是,唯一的官方API是iOS 5及更高版本,它是一个名为的属性alertViewStyle
,可以设置为以下参数:
UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput
Run Code Online (Sandbox Code Playgroud)
UIAlertViewStylePlainTextInput
成为你想要的人.
Apple强烈反对与上述视图层次结构混淆.
我正在使用BlockAlertsAndActionSheets而不是AlertViews和ActionSheets的Apple组件,因为我更喜欢块方法.还包含一个BlockTextPromptAlertView
源代码,可能是你想要的.您可以替换该控件的图像以获得Apple风格的背面.
例:
- (IBAction)newFolder:(id)sender {
id selfDelegate = self;
UITextField *textField;
BlockTextPromptAlertView *alert = [BlockTextPromptAlertView promptWithTitle :@"New Folder"
message :@"Please enter the name of the new folder!"
textField :&textField];
[alert setCancelButtonWithTitle:@"Cancel" block:nil];
[alert addButtonWithTitle:@"Okay" block:^{
[selfDelegate createFolder:textField.text];
}];
[alert show];
}
- (void)createFolder:(NSString*)folderName {
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
尝试这样的事情:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title"
message:@"\n\n"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Save", nil] autorelease];
CGRect rect = {12, 60, 260, 25};
UITextField *dirField = [[[UITextField alloc] initWithFrame:rect] autorelease];
dirField.backgroundColor = [UIColor whiteColor];
[dirField becomeFirstResponder];
[alert addSubview:dirField];
[alert show];
Run Code Online (Sandbox Code Playgroud)