今天早上,我正在查看我在某处(并在应用程序中使用)发现的一些旧代码,这些代码会破解 aUIAlertView
以便从用户那里获取一串输入。就目的而言,这有点矫枉过正,而且看起来很愚蠢,但我从未见过更简单的方法。然后我想出了以下方法,这似乎可行(iPad 5.1.1 和模拟器)。
我的问题有点开放,但从本质上讲,是否存在将其作为策略中断的条件:创建一个带有附件视图的屏幕外文本字段,在附件视图中放置一个代理文本字段,并转发各种属性设置给代理?
PMKeyboardTextField.h:
#import <UIKit/UIKit.h>
@interface PMKeyboardTextField : UITextField
- (id)initWithPrompt:(NSString *)prompt;
@end
PMKeyboardTextField.m:
#import "PMKeyboardTextField.h"
@interface PMKeyboardTextField ()
@property (nonatomic, strong) UITextField *inputField;
@end
@implementation PMKeyboardTextField
@synthesize inputField = _inputField;
- (id)initWithPrompt:(NSString *)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
selector:@selector(keyboardDidShow)
name:UIKeyboardDidShowNotification
object:nil];
}
return self;
}
- (void)keyboardDidShow {
[self.inputField becomeFirstResponder];
}
- (id<UITextFieldDelegate>)delegate {
return self.inputField.delegate;
}
- (void)setDelegate:(id<UITextFieldDelegate>)delegate {
self.inputField.delegate = delegate;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end