0

1つのテキストフィールドが電話番号テキストフィールドである5つのテキストフィールドを持つ1つの登録フォームがあり、そのuikeyboardtypenumberpadのキーボードを使用しますが、numberpadkeyboardの完了ボタンはありません。完了ボタンを追加していますが、ボタンはすべてのテキストフィールドに追加されていますが、電話番号フィールドのみが必要です。

私のコードは次のとおりです。

- (void)viewDidLoad
{
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonAction)];
    self.navigationItem.leftBarButtonItem = leftButton;
//    UIBarButtonItem *rightbarButton = [[UIBarButtonItem alloc]initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(AboutButtonAction)];
//    self.navigationItem.rightBarButtonItem = rightbarButton;
    [super viewDidLoad];
    NSLog(@"Events Id is : %@", particularEventId);
    phoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(56, 223, 220, 31)];
    phoneNumber.borderStyle = UITextBorderStyleRoundedRect;
    phoneNumber.keyboardType = UIKeyboardTypeNumberPad;
    phoneNumber.returnKeyType = UIReturnKeyDone;
    phoneNumber.textAlignment = UITextAlignmentLeft;

    [self.view addSubview:phoneNumber];
    // 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)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(keyboard == phoneNumber)
        {
        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)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)doneButton:(id)sender {
    NSLog(@"doneButton");
    NSLog(@"Input: %@", phoneNumber.text);
    [phoneNumber resignFirstResponder];
}
4

1 に答える 1