今天早上,我正在查看我在某处(并在应用程序中使用)发现的一些旧代码,这些代码会破解 UIAlertView 以便从用户那里获取一串输入。就目的而言,这有点矫枉过正,而且看起来很愚蠢,但我从未见过更简单的方法。然后我想出了以下方法,似乎可以工作(iPad 5.1.1 和模拟器)。
我的问题有点开放,但从本质上讲,是否存在将其作为策略中断的条件:创建带有附件 View 的屏幕外文本字段,在附件 View 中放置代理文本字段,然后转发代理的各种属性设置?
PMKeyboardTextField.h:
#import <UIKit/UIKit.h>
@interface PMKeyboardTextField : UITextField
- (id)initWithPromptNSString *)prompt;
@end
PMKeyboardTextField.m:
#import "MKeyboardTextField.h"
@interface PMKeyboardTextField ()
@property (nonatomic, strong) UITextField *inputField;
@end
@implementation PMKeyboardTextField
@synthesize inputField = _inputField;
- (id)initWithPromptNSString *)prompt {
self = [super initWithFrame:CGRectMake(-1, -1, 1, 1)];
if (self) {
UIView *accessory = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 72)];
[accessory setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.8]];
UILabel *getLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, 284, 21)];
[getLabel setBackgroundColor:[UIColor clearColor]];
getLabel.text = prompt;
[accessory addSubview:getLabel];
self.inputField = [[UITextField alloc] initWithFrame:CGRectMake(18, 37, 264, 31)];
[accessory addSubview:self.inputField];
self.inputAccessoryView = accessory;
[[NSNotificationCenter defaultCenter] addObserver:self
selectorselector(keyboardDidShow)
name:UIKeyboardDidShowNotification
object:nil];
}
return self;
}
- (void)keyboardDidShow {
[self.inputField becomeFirstResponder];
}
- (id<UITextFieldDelegate>)delegate {
return self.inputField.delegate;
}
- (void)setDelegateid<UITextFieldDelegate>)delegate {
self.inputField.delegate = delegate;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
Best Answer-推荐答案 strong>
如果您的目标是 iOS 5.0 或更高版本,则无需破解警报 View 即可添加文本字段。您只需将警报 View 的 alertViewStyle 设置为 UIAlertViewStylePlainTextInput 即可为警报 View 提供一个纯文本输入字段,然后您可以通过发送 textFieldAtIndex: 访问该字段到警报 View 。
除此之外,您的解决方法似乎还可以。我猜您必须将屏幕外文本字段添加到警报 View 的窗口,而不是普通的应用程序窗口。
关于ios - inputAccessoryView 可以用于实际输入吗,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/11973634/
|