Possible Duplicate:
How to display temporary popup message on iPhone/iPad/iOS
我希望能够在我当前 View 的顶部弹出一个小屏幕/ View ,上面有一个 UITextField 和两个按钮。我想允许用户输入一些数据或取消。
我不想使用全新的屏幕来执行此操作。有什么好的方法可以实现这一目标吗? iOS API 中是否提供了执行此操作的功能?
任何指向教程的链接将不胜感激。一直在谷歌上四处阅读,但我能找到的只是全屏模态视图的示例。
谢谢,
-代码
您可以子类化 UIAlertView 类以拥有一个文本字段(或多个文本字段)
您的自定义类如下所示:(AlertPrompt.h)
@interface AlertPrompt : UIAlertView
{
UITextField *emailTextField;
UITextField *nameTextField;
}
@property (nonatomic, retain) UITextField *emailTextField;
@property (nonatomic, retain) UITextField *nameTextField;
- (id)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;
@end
AlertPrompt.m
#import "AlertPrompt.h"
@implementation AlertPrompt
@synthesize emailTextField, nameTextField;
- (id)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{
if (self = [super initWithTitle:title message:@" \n\n\n" delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
{
self.frame = CGRectMake(0, 0, 300, 400);
UITextField *nameTF = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
UITextField *emailTF = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80, 260.0, 25.0)];
[nameTF setBackgroundColor:[UIColor whiteColor]];
[nameTF setAutocorrectionType:UITextAutocorrectionTypeNo];
[nameTF setPlaceholder:@"name"];
[emailTF setBackgroundColor:[UIColor whiteColor]];
[emailTF setAutocorrectionType:UITextAutocorrectionTypeNo];
[emailTF setPlaceholder:@"email"];
[self addSubview:nameTF];
[self addSubview:emailTF];
self.nameTextField = nameTF;
self.emailTextField = emailTF;
if (delegate) {
self.emailTextField.delegate = delegate;
self.nameTextField.delegate = delegate;
}
[nameTF release];
[emailTF release];
}
return self;
}
- (void)show
{
[nameTextField becomeFirstResponder];
[super show];
}
- (void)dealloc
{
[nameTextField release];
[emailTextField release];
[super dealloc];
}
@end
你会注意到我在 init 方法中将 @"\n\n\n"作为参数传递,以便为我要显示的文本字段腾出空间..所以你必须根据以下内容修改消息参数您希望在提示中显示的文本字段数。
希望对你有帮助