trying to add done button to Numeric keyboard

前端 未结 4 909
星月不相逢
星月不相逢 2020-12-16 21:22

I\'m trying to add a \"done\" button to the UIKeyboadnumpad, but with no success. What\'s wrong in my code?

the keyboard don\'t have the done button

         


        
4条回答
  •  时光取名叫无心
    2020-12-16 21:59

    Another solution. Perfect if there are other non-number pad text fields on the screen.

    inputAccessoryView

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        numberToolbar.barStyle = UIBarStyleBlackTranslucent;
        numberToolbar.items = [NSArray arrayWithObjects:
                             [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                             [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                             [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                         nil];
        [numberToolbar sizeToFit];
        numberTextField.inputAccessoryView = numberToolbar;
    }
    
    -(void)cancelNumberPad{
        [numberTextField resignFirstResponder];
        numberTextField.text = @"";
    }
    
    -(void)doneWithNumberPad{
        NSString *numberFromTheKeyboard = numberTextField.text;
        [numberTextField resignFirstResponder];
    }
    

    I needed the phone pad (with the +*#) and not the number pad, do I didn't even had the empty button in the corner.

提交回复
热议问题