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 count]);

始终为 0,因此它根本不添加按钮……知道吗?

4

3 回答 3

0

真正的解决方案是创建一个包含所有按钮的视图 (12)。然后只需将其动画化到屏幕外即可。使用之前的海报方法,iOS 的任何更新都会破坏功能,如果你有任何严肃的应用程序,你不希望这样。您的客户可能会发疯,您的应用程序会被替代安装。

不利的一面是,您需要绘制一些像样的图形,但要保证在 Apple 有官方方法让这种情况发生之前一直工作。

于 2013-04-09T00:43:56.477 回答
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

于 2012-08-23T14:49:11.710 回答
0

this my working code for this, it has been used on real iPhone only, I've never tested on Simulator, I'm hoping it will help on you.

- (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 回答