2012-08-23 48 views
0

我已阅读了一些有关此问题的问题,但找不到解决方案。将b完成按钮添加到数字键盘

我简单惯于一个完成按钮添加到我的数字小

- (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,所以它根本不会添加按钮...任何想法?

+0

我没有看到你在上面的代码中添加按钮到tempWindow的位置,你在别的地方做它吗? – Brian

+0

[keyboard addSubview:doneButton]; 这个,还是不行? –

回答

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

+0

他正在使用相同的教程伙计.. –

+0

但他没有检查它是键盘还是没有..这就是为什么我推杆链接到他可以只使用 – lukaswelte

+0

扩展我甚至没有得到NSLog(@找到“); ....多数民众赞成在问题 –

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]; 
     } 
    } 
} 
0

真正的解决方案是创建一个包含所有按钮(12)的视图。然后将它从屏幕上移动到视图中。使用之前发布的方法,iOS的任何更新都可能会破坏功能,如果您有任何严重的应用程序,则不需要这些更新。您的客户可能会发疯,并且您的应用会被替代方案安装。

这里的缺点是,你需要画出一些体面的图形,但保证工作,直到苹果有一个官方的方式让这种情况发生。

相关问题