2010-12-05 130 views
3

我正在使用下面的這個方便的代碼成功添加完成按鈕到我的數字鍵盤上。但我有一個電子郵件按鈕,啓動MFMailComposeViewController。我如何確保完成按鈕不會出現在電子郵件鍵盤上?將完成按鈕添加到iPhone上的數字鍵盤鍵盤上

// 
// 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所以它會自動執行此當我輸入這個子類,所以在我的應用程序中的布爾標誌可能不會工作。

回答

4

對於iOS 3.2+,無論如何,你都不應該再使用這個黑客技術。相反,將您的自定義視圖分配給您的控件的inputAccessoryView屬性。

+0

你能詳細說明一下示例代碼嗎?我在哪裏可以使用inputAccessoryView? – Bryan 2010-12-05 19:03:56

+0

在文檔中查找`inputAccessoryView`。您將自定義視圖分配給`myTextField.inputAccessoryView = ...`,並且當此文本字段成爲第一個響應者時它會自動顯示。 – 2010-12-05 19:20:10

+0

我無法定位按鈕。它保持鍵盤的頂部和中心,但工作。 – Bryan 2010-12-05 20:08:40

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

我一直在玩這一段時間,並從這個和其他幾個線程了一堆想法。我最終做了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 
相關問題