【问题标题】:Adding Done Button to Only Number Pad Keyboard on iPhone将完成按钮添加到 iPhone 上的数字键盘键盘
【发布时间】:2011-05-20 14:18:42
【问题描述】:

我使用下面这个方便的代码成功地在我的数字键盘上添加了一个完成按钮。但我有一个启动 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,以便在我导入此子类时自动执行此操作,因此我的应用程序中的布尔标志可能不起作用。

【问题讨论】:

    标签: iphone objective-c uikeyboard dismiss


    【解决方案1】:

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

    【讨论】:

    • 你能用示例代码详细说明一下吗?我将在哪里使用 inputAccessoryView?
    • 在文档中查找inputAccessoryView。您将自定义视图分配给 myTextField.inputAccessoryView = ...,当此文本字段成为第一响应者时,它会自动显示。
    • 我无法定位按钮。它位于键盘的顶部和中心,但可以正常工作。
    • 然后将其放置在跨越整个屏幕宽度的自定义视图中(呈现工具栏的外观)。
    • 谢谢奥莱。我就是这么做的。
    【解决方案2】:
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
            UIButton *myDoneButton = [self GetKeyboardDoneButton];
            myMinText.inputAccessoryView = myDoneButton;
            myMaxText.inputAccessoryView = myDoneButton;
        }
    
    
    - (UIButton *)GetKeyboardDoneButton {
        // create custom button
        UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        doneButton.frame = CGRectMake(-100, 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];
    
    
        return doneButton;
    
    }
    
    - (void)doneButton:(id)sender {
        NSLog(@"doneButton");
        [self.view endEditing:TRUE];
    }
    

    【讨论】:

    • 嗨...您是如何获得完成图像的?我曾经将它们放在我的应用程序中,并且丢失了参考。你能引导我到网站吗?
    • 这个答案启发我制作了一个更强大的版本,即使在同一父视图中使用 2 种不同类型的键盘也能很好地工作horseshoe7.wordpress.com/2012/08/17/…
    【解决方案3】:

    我一直在玩这个,并从这个和其他几个线程中获得了很多想法。我最终创建了一个 UIViewController 的子类来处理数字键盘问题,但后来我认为这不够通用,因为我可能需要从 UITableViewController 继承并仍然实现它。所以我将它重构为一个辅助类,它可以完成所有工作并且实现起来相当简单。所以这就是我最终的结果。 .h 文件包含在您要使用它的 UIViewController 类中实现此功能所需的步骤。

    我唯一想不通的是为什么我无法摆脱这条警告消息“方法实现的属性及其声明必须匹配”。我认为这与变量参数列表和 NS_REQUIRES_NIL_TERMINATION 有关

    我希望其他人发现此代码有帮助,如果有人知道如何摆脱警告消息,我很想知道。

    哦,我差点忘了您需要为按钮添加图像文件。我很久以前从其他线程下载了它们,忘记了在哪里,但它们应该不难找到。

    //
    //  NumericKeyboardHelper.h
    //
    //  Created by Joseph Gagliardo on 7/6/12.
    //  Copyright (c) 2012 Joseph Gagliardo. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    /*
    1. Import this header file
    
    2. Add the NumericKeyboardHelperProtocol to the UIViewController<NumericKeyboardHelperProtocol>
    
    3. Add a property to create this helper class 
    
     @property (strong, nonatomic) NumericKeyboardHelper *numericKeyboardHelper;
    
    4. synthesize it and clean it up when done in the viewDidUnload
    
     @synthesize numericKeyboardHelper=_numericKeyboardHelper;
    
     [self setNumericKeyboardHelper:nil];
    
    5. Insert the following line in the viewDidLoad of the controller 
    
     self.numericKeyboardHelper = [[NumericKeyboardHelper alloc] initWithObserver:self andSelector:@selector(numericDoneButtonPressed:) andFields: self.TextField1, self.TextField2, nil];
    
     where self.TextField1, ... are the textField Outlets that have a numeric keyboard
    
    6. Provide a numericDoneButtonPressed: method as required by the protocol to receive the message when the done button is pressed
    
    The helper class does all the rest of the work 
    */
    @protocol NumericKeyboardHelperProtocol
    - (void)numericDoneButtonPressed:(id)sender;
    @end
    
    @interface NumericKeyboardHelper : NSObject
    @property (strong, nonatomic) UIButton *numericDoneButton;
    @property (strong, nonatomic) NSArray *numericFields;
    @property (weak, nonatomic) UIViewController *viewController;
    
    - (void)showNumericKeyboard:(id)sender;
    - (void)hideNumericKeyboard:(id)sender;
    - (id) initWithObserver: (id) observer andSelector:(SEL)selector andFields:(UIControl *)argList, ... NS_REQUIRES_NIL_TERMINATION;
    
    @end
    
    
    //
    //  NumericKeyboardHelper.m
    //
    //  Created by Joseph Gagliardo on 7/6/12.
    //  Copyright (c) 2012 Joseph Gagliardo. All rights reserved.
    //
    
    #import "NumericKeyboardHelper.h"
    
    @implementation NumericKeyboardHelper
    @synthesize numericDoneButton=_numericDoneButton;
    @synthesize viewController=_viewController;
    @synthesize numericFields=_numericFields;
    
    - (id) initWithObserver: (id) observer andSelector:(SEL)selector andFields:(UIControl *)argList, ... NS_REQUIRES_NIL_TERMINATION
    {
        if (self = [super init])
        {
            [[NSNotificationCenter defaultCenter] addObserver:observer 
                                                 selector:selector
                                                     name:@"numericDoneButtonPressed" 
                                                       object:nil];
    
            NSMutableArray *a = [[NSMutableArray alloc]init];
            va_list args;
            va_start(args, argList);
            for (UIControl *arg = argList; arg != nil; arg = va_arg(args, UIControl*))
            {
                [a addObject:arg];
            }
            va_end(args);
            self.numericFields = [NSArray arrayWithArray:a];
            NSLog(@"Array count %i", [a count]);
            self.viewController = observer;
            [self setAllTextFields:self.viewController.view];
        }
        return self;
    }
    
    - (void) setAllTextFields: (UIView *) view
    {
        for (UIView *v in view.subviews)
        {
            if ([v isKindOfClass:[UITextField class]])
            {
                UITextField *t = (UITextField *)v;
                if ([self.numericFields containsObject:v])
                    [t addTarget:self action:@selector(showNumericKeyboard:) forControlEvents:UIControlEventTouchDown];
                else 
                    [t addTarget:self action:@selector(hideNumericKeyboard:) forControlEvents:UIControlEventTouchDown];
            }
            else if ([v.subviews count] > 0) 
            {
                [self setAllTextFields:v];
            }
        }
    }
    
    - (void)addNumericDoneButtonToKeyboard
    {
        if (self.numericDoneButton == nil)
        {
            self.numericDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
            self.numericDoneButton.frame = CGRectMake(0, 163, 106, 53);
            self.numericDoneButton.adjustsImageWhenHighlighted = NO;
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) 
            {
                [self.numericDoneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
                [self.numericDoneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
            } 
            else 
            {        
                [self.numericDoneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
                [self.numericDoneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
            }
            [self.numericDoneButton addTarget:self action:@selector(numericDoneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        }
        [[self keyboardView] addSubview:self.numericDoneButton];
    }
    
    - (void)showNumericKeyboard:(id)sender
    {
        [self addNumericDoneButtonToKeyboard];
        self.numericDoneButton.hidden = NO;
    }
    
    - (void)hideNumericKeyboard:(id)sender
    {
        self.numericDoneButton.hidden = YES;
    }
    
    - (UIView *)keyboardView
    {
        UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
        UIView* keyboard;
        for(int i=0; i<[tempWindow.subviews count]; i++) 
        {
            keyboard = [tempWindow.subviews objectAtIndex:i];
            if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2 && [[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) || [[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                return keyboard;
        }
        return nil;
    }
    
    - (void)numericDoneButtonPressed:(id)sender 
    {
        for (UIControl *c in self.numericFields)
            [c resignFirstResponder];
    
        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"numericDoneButtonPressed"
         object:sender ];
    }
    @end
    

    【讨论】:

    • 什么线程?请包括归属于其他线程!那是你说的懒惰,我很久以前从其他线程下载了它们,忘记了在哪里,但它们应该不难找到。 -1 来自我!
    猜你喜欢
    • 2012-02-23
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2013-09-21
    • 2011-02-19
    • 1970-01-01
    相关资源
    最近更新 更多