将b完成按钮添加到number键盘

时间:2012-08-23 14:40:03

标签: ios xcode ios5

我已经阅读过有关此问题,但无法找到解决方案。

我根本不会在我的号码簿上添加一个完成按钮

- (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(@&#34;%d&#34;,[tempWindow.subviews count]);

始终为0,所以它根本不添加按钮......任何想法?

3 个答案:

答案 0 :(得分:0)

有一个很棒的教程可以完全满足您的需求 所以访问: http://www.neoos.ch/blog/37-uikeyboardtypenumberpad-and-the-missing-return-key

尝试添加

 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

答案 1 :(得分: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];
        }
    }
}

答案 2 :(得分:0)

真正的解决方案是使用所有按钮(12)创建视图。然后从屏幕上将其动画显示为视图。使用以前的海报方法,对iOS的任何更新都可能会破坏功能,如果您有任何严肃的应用程序,则不要这样做。您的客户可能会疯狂,您的应用程序可以替代安装。

这方面的缺点是你需要绘制一些不错的图形,但保证工作直到Apple有正式的方式让这种情况发生。

相关问题