0

これに関する多くの質問を読みましたが、解決策が見つかりません。

NumberPad に [完了] ボタンを追加したくない

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)addButtonToKeyboard {
    NSLog(@"call");
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;

    [doneButton setImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted];

    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;

    NSLog(@"%d", [tempWindow.subviews count]);

    for(int i=0; i<[tempWindow.subviews count]; i++) {
        NSLog(@"found");

        keyboard = [tempWindow.subviews objectAtIndex:i];
        [keyboard addSubview:doneButton];
    }
}

- (void)keyboardDidShow:(NSNotification *)note {
    // if clause is just an additional precaution, you could also dismiss it
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        [self addButtonToKeyboard];
    }
}

このコード:

NSLog(@"%d", [tempWindow.subviews カウント]);

常に 0 であるため、単にボタンを追加しません...何か考えはありますか?

4

3 に答える 3

0

これは私の作業コードです。実際のiPhoneでのみ使用されています。シミュレータでテストしたことはありません。お役に立てば幸いです。

- (void)keyboardWillShow:(NSNotification *)notification {
    UIWindow *_keyboardWindow = nil;
    for (UIWindow *_testWindow in [[UIApplication sharedApplication] windows]) {
        if (![[_testWindow class] isEqual:[UIWindow class]]) {
            _keyboardWindow = _testWindow;
            break;
        }
    }

    UIView *_foundKeyboard = nil;
    for (UIView *_possibleKeyboard in [_keyboardWindow subviews]) {
        if ([[_possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
            if ([[[[_possibleKeyboard subviews] objectAtIndex:0] description] hasPrefix:@"<UIKeyboard"]) {
                _foundKeyboard = _possibleKeyboard;
                break;
            }
        } else if ([[_possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
            _foundKeyboard = _possibleKeyboard;
            break;
        }
    }

    if (_foundKeyboard) {
        if (_doneButton == nil) {
            _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _doneButton.frame = CGRectMake(0, 163, 106, 53);
            _doneButton.adjustsImageWhenHighlighted = false;
            [_doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:19.f]];
            [_doneButton setTitle:@"DONE" forState:UIControlStateNormal];
            [_doneButton setTitleColor:[UIColor colorWithRed:77.f/256.f green:84.f/256.f blue:98.f/256.f alpha:1.f] forState:UIControlStateNormal];
            [_doneButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_doneButton.titleLabel setShadowOffset:CGSizeMake(1.f, 1.f)];
            [_doneButton setTitle:@"DONE" forState:UIControlStateHighlighted];      
            [_doneButton addTarget:self action:@selector(buttonKeyBoardDoneTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside];
        }
        if (_doneButton.superview == nil) {
            [_foundKeyboard addSubview:_doneButton];
        }
    }
}
于 2012-08-23T15:54:49.640 に答える
0

あなたが必要とすることを正確に行う素晴らしいチュートリアルがあり ます。

追加してみる

 if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)
        [keyboard addSubview:doneButton];

あなたの

for(int i=0; i<[tempWindow.subviews count]; i++) {
    NSLog(@"found");

    keyboard = [tempWindow.subviews objectAtIndex:i];
    [keyboard addSubview:doneButton];
}

これはあなたの結果になるはずです:

for(int i=0; i<[tempWindow.subviews count]; i++) {
    NSLog(@"found");

    keyboard = [tempWindow.subviews objectAtIndex:i];
    if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)
        [keyboard addSubview:doneButton];   
}

または、この投稿にある拡張機能を使用することもできます https://stackoverflow.com/a/4355795/1578508

于 2012-08-23T14:49:11.710 に答える