今朝、私はどこかで見つけた (そしてアプリで使用されていた) 古いコードを調べていましたUIAlertView
。それは目的のためにやり過ぎで、かなりばかげているように見えますが、これより簡単な方法は見たことがありません。次に、うまくいくように見える次のアプローチを思いつきました(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