iPhone上的UIAlertView中的UITextField - 如何使其响应? [英] UITextField in UIAlertView on iPhone - how to make it responsive?

查看:74
本文介绍了iPhone上的UIAlertView中的UITextField - 如何使其响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个UITextField放到UIAlertView中并将其移动,所以键盘不会覆盖它:

  [dialog setDelegate:self]; 
[dialog setTitle:@输入名称];
[dialog addButtonWithTitle:@Cancel];
[dialog addButtonWithTitle:@OK];
UITextField * nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0,45.0,245.0,25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0,100.0);
[dialog setTransform:moveUp];
[dialog show];

我还有以下代码来处理警报视图:

   - (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@%d,(int)buttonIndex );
if(buttonIndex == 1){// OK push
NSLog([alert textField] .text); //不记录任何东西
} else {
//取消推送
}}


b $ b

我也有一个UIAlertViewExtended.h文件,其中包含:

  @class UITextField,UILabel; 

@interface UIAlertView(扩展)

- (UITextField *)textField;

@end

我的问题是如何获取文本

解决方案



/ div>

对于那些可能关心的人,这里有一个工作解决方案:

  UIAlertView * dialog = [[UIAlertView alloc]在里面]; 
[dialog setDelegate:self];
[dialog setTitle:@输入名称];
[dialog setMessage:@];
[dialog addButtonWithTitle:@Cancel];
[dialog addButtonWithTitle:@OK];

nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0,45.0,245.0,25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0,100.0);
[dialog setTransform:moveUp];
[dialog show];
[dialog release];
[nameField release];

确保您已创建UITextField * nameField;在您的.h文件中,然后您可以通过执行以下操作获取用户输入的文本:
inputText = [nameField text];



void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex method。



重要注意事项 c $ c> CGAffineTransform 是自动完成的,所以如果你只定位4.0+,你可以离开这一点。否则,您需要检查您所使用的操作系统,并相应地处理。


I placed a UITextField into a UIAlertView and moved it up so the keyboard wouldn't cover it up with the following code:

[dialog setDelegate:self];
[dialog setTitle:@"Enter Name"];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
UITextField * nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 100.0);
[dialog setTransform: moveUp];
[dialog show];

I also have the following code to deal with the alert view:

- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{    
    NSLog(@"%d", (int) buttonIndex);
    if (buttonIndex == 1) { // OK pushed
        NSLog([alert textField].text); // does not log anything
    } else {
        // Cancel pushed
}}

I also have a UIAlertViewExtended.h file that contains:

@class UITextField, UILabel;

@interface UIAlertView(Extended)

-(UITextField *)textField;

@end

My question is how do I get the text the user entered and how do I dismiss the keyboard?

Thanks, Ben

解决方案

For those who may care, here's a working solution:

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter Name"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];

nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 100.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];

Make sure you've created UITextField * nameField; in your .h file, then you can get at the text the user typed in by doing: inputText = [nameField text];

in the - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex method.

important note: for iOS 4.0+, the CGAffineTransform is done automatically so you can leave that bit out if you're only targeting 4.0+. Otherwise, you will need to check which OS you are on and handle it accordingly.

这篇关于iPhone上的UIAlertView中的UITextField - 如何使其响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆