2010-12-05 9 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 

私はこのサブクラスをインポートするとき、それはこれを自動的に行いますので、私は自分のアプリケーションのbooleanフラグはおそらく動作しません、のUIViewControllerを拡張しようとしています。

答えて

4

iOS 3.2以降では、このハックをもう使用しないでください。代わりに、カスタムビューをコントロールのinputAccessoryViewプロパティに割り当てます。

+0

サンプルコードで具体的に説明できますか? inputAccessoryViewはどこで使用しますか? – Bryan

+0

ドキュメントの 'inputAccessoryView'を参照してください。 'myTextField.inputAccessoryView = ...'にカスタムビューを割り当てると、このテキストフィールドが最初のレスポンダになったときに自動的に表示されます。 –

+0

ボタンを配置できませんでした。それはキーボードの上と中央にとどまっていましたが、機能します。 – Bryan

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

こんにちは...あなたはどのように完了画像を取得しましたか?私はそれらを私のアプリに持っていて、参考文献を失ってしまった。あなたは私をウェブサイトに連れて行くことができますか? – CodeGuy

+0

この回答は、同じ親ビューの2種類のキーボードでもうまく動作する、より堅牢なバージョンを作成するよう促しました http://horseshoe7.wordpress.com/2012/08/17/tutorial-creating-a-完了ボタンの上にiphone番号パッドキーボード/ – horseshoe7

-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 
+0

どのようなスレッド?他のスレッドへの帰属を含めてください!それはあなたが言ったことで怠惰です。*私はずっと前に他のスレッドからそれらをダウンロードしてどこを忘れていましたが、来るのは難しくありません。私から-1! – t0mm13b

関連する問題