【发布时间】:2012-04-01 06:58:53
【问题描述】:
我需要将TextField 添加到UIAlertView。我知道苹果不鼓励这种方法。那么有没有我可以用来将TextField 添加到UIAlertView 相似框架的库?
【问题讨论】:
标签: iphone objective-c ios cocoa-touch
我需要将TextField 添加到UIAlertView。我知道苹果不鼓励这种方法。那么有没有我可以用来将TextField 添加到UIAlertView 相似框架的库?
【问题讨论】:
标签: iphone objective-c ios cocoa-touch
对于 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];
此外,您还需要实现UITextFieldDelegate、UIAlertViewDelegate 协议。
【讨论】:
不幸的是,唯一的官方 API 是 iOS 5 及更高版本,它是一个名为 alertViewStyle 的属性,可以设置为以下参数:
UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput
UIAlertViewStylePlainTextInput 是你想要的。
Apple 强烈不鼓励像上面描述的那样混淆视图层次结构。
【讨论】:
我使用BlockAlertsAndActionSheets 而不是 Apple 组件用于 AlertViews 和 ActionSheets,因为我更喜欢块方法。源中还包含BlockTextPromptAlertView,这可能是您想要的。您可以替换该控件的图像以恢复 Apple 风格。
Tutorial which gets you started
例子:
- (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
}
【讨论】:
BlockTextPromptAlertView 的例子吗?我的印象是不允许开发人员向 UIAlertView 添加任何文本字段等。
animateWithDuration
试试这样的:
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];
【讨论】:
从 iOS 8 开始,UIAlertView 已被弃用,取而代之的是 UIAlertController,这增加了对使用以下方法添加 UITextFields 的支持:
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
有关示例,请参阅 this answer。
【讨论】:
你可以试试:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Your title here!" message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:testTextField];
[myAlertView show];
[myAlertView release];
关注link了解详情。
【讨论】:
首先将 UIAlertViewDelegate 添加到 ViewController.h 文件中,如
#import <UIKit/UIKit.h>
@interface UIViewController : UITableViewController<UIAlertViewDelegate>
@end
然后在你想提醒显示的地方添加下面的代码,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
它是一个委托方法,它返回 UItextField 的输入内容
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%@", [alertView textFieldAtIndex:0].text);
}
【讨论】:
添加到来自“Shmidt”的答案,捕获在 UIAlertView 中输入的文本的代码粘贴在下面(感谢“Wayne Hartman”Getting text from UIAlertView)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
self.userNumber = [alertView textFieldAtIndex:0].text;
if (self.userNumber) {
// user enetered value
NSLog(@"self.userNumber: %@",self.userNumber);
} else {
NSLog(@"null");
}
}
}
【讨论】:
参考这个...http://iosdevelopertips.com/undocumented/alert-with-textfields.html 这是私有 api,如果你将它用于 appstore 中的应用程序,它可能会被拒绝,但它可以用于企业开发。
【讨论】: