2

今朝、私はどこかで見つけた (そしてアプリで使用されていた) 古いコードを調べていました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
4

2 に答える 2

2

アラートビューの過度の乱雑さや画面上のビューの再配置の必要性を回避するこのアイデアが気に入っているのと同じくらい、別のフォーラムで致命的な欠陥が指摘されました。一部の人々は外部キーボードを使用しています。

于 2012-08-16T11:44:18.463 に答える
2

iOS 5.0 以降をターゲットにしている場合は、アラート ビューをハックしてテキスト フィールドを追加する必要はありません。アラート ビューに 1 つのプレーンテキスト入力フィールドを与えるようにアラートビューalertViewStyleを設定するだけで、アラート ビューに送信することでアクセスできます。UIAlertViewStylePlainTextInputtextFieldAtIndex:

それを除けば、あなたの回避策は問題ないようです。通常のアプリ ウィンドウではなく、アラート ビューのウィンドウにオフスクリーン テキスト フィールドを追加する必要があると思います。

于 2012-08-15T17:21:42.697 に答える