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

+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- done-on-the-iphone-number-pad-keyboard/ – 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