iphone - 我想在接受用户输入的屏幕上弹出一个小 View (200x200)

标签 iphone objective-c ios xcode

<分区>

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"作为参数传递,以便为我要显示的文本字段腾出空间..所以你必须根据以下内容修改消息参数您希望在提示中显示的文本字段数。

希望对你有帮助

关于iphone - 我想在接受用户输入的屏幕上弹出一个小 View (200x200),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8512528/

相关文章:

objective-c - NSStream : Is there any airtight defense against blocking?

objective-c - 从 Swift 错误调用 objective-c 方法

ios - 如何检测 iPhone 5c 的颜色?

ios - 在 Appstore 上更改卖家名称

iPhone bundle 显示名称与长名称?

javascript - Youtube 检测视频何时开始播放

ios - 根据标签文本长度动态自定义 UITableViewCell 的高度

iOS::导航栏不出现

iphone - shouldStartLoadWithRequest 附加链接与 applewebdata

iphone - 出于测试目的在应用程序中设置 iPhone 日期/时间?