iPhone 上 UIAlertView 中的 UITextField - 如何使其响应? [英] UITextField in UIAlertView on iPhone - how to make it responsive?
问题描述
我将一个 UITextField 放入一个 UIAlertView 并将其向上移动,这样键盘就不会用以下代码覆盖它:
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
}}
我还有一个 UIAlertViewExtended.h 文件,其中包含:
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?
谢谢,本
推荐答案
对于那些可能关心的人,这里有一个可行的解决方案:
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];
确保你已经创建了 UITextField * nameField;在您的 .h 文件中,您可以通过执行以下操作获取用户输入的文本:inputText = [nameField text];
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];
在 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex 方法中.
in the - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex method.
重要说明:对于 iOS 4.0+,CGAffineTransform
是自动完成的,因此如果您只针对 4.0+,则可以忽略这一点.否则,您将需要检查您使用的是哪个操作系统并进行相应处理.
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屋!