iphone - 在 iPhone 上仅将完成按钮添加到数字键盘

标签 iphone objective-c uikeyboard dismiss

我使用下面这个方便的代码成功地向我的数字键盘添加了一个完成按钮。但是我有一个启动 MFMailComposeViewController 的电子邮件按钮。我如何确保完成按钮不会出现在电子邮件键盘上?

//
//  UIViewController+NumPadReturn.m
//  iGenerateRandomNumbers
//
//  Created by  on 12/4/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "UIViewController+NumPadReturn.h"


@implementation UIViewController (NumPadReturn)

-(void) viewDidLoad{
    // add observer for the respective notifications (depending on the os version)
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardDidShow:) 
                                                     name:UIKeyboardDidShowNotification 
                                                   object:nil];     
    } else {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardWillShow:) 
                                                     name:UIKeyboardWillShowNotification 
                                                   object:nil];
    }

}


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

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

- (void)addButtonToKeyboard {
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
        [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
    } else {        
        [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;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];

        // keyboard found, add the button
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                [keyboard addSubview:doneButton];
        } else {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                [keyboard addSubview:doneButton];
        }
    }


}

- (void)doneButton:(id)sender {
    NSLog(@"doneButton");
    [self.view endEditing:TRUE];
}



@end

我正在尝试扩展 UIViewController,以便它在我导入该子类时自动执行此操作,因此我的应用程序中的 bool 标志可能不起作用。

最佳答案

对于 iOS 3.2+,无论如何你不应该再使用这个 hack。相反,将自定义 View 分配给控件的 inputAccessoryView 属性。

关于iphone - 在 iPhone 上仅将完成按钮添加到数字键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4360140/

相关文章:

ios - Swift HealthKit UpdateQuery 只被调用一次

iphone - 对内存管理的一些理解

ios - 意外的迭代性能下降

Objective-C int 桥接为 Int32 到 Swift

ios - 在滚动 UITableView 的同时拉起键盘并随着 tableView 一起滚动

iphone - 关于ui键盘的问题

iphone - 获取 UITableViewCell 的 subview (UITextField 或 UILabel)——两种方式,一种有效,一种无效——为什么?

ios - Swift: TableView 部分标题

objective-c - 添加 UITextview 时应用程序崩溃 "[UITextView length]: unrecognized selector sent to instance"

ios - 设置 UIKeyboard 附件 View 时遇到问题