Frage

Currently i am working in iPhone application, Using UITextField to enter phone number, user enter the phone number, then how to hide the keyboard? because no return button here, Is it possible to add done button inside the keyboard, please help me

Thanks in Advance

Below mentioned image for your reference, empty space in left corner to add done button, Is it possible?

War es hilfreich?

Lösung

Here is to be a good tutorial about this.

Andere Tipps

Refer this link. It shows how to add "Done" button in key board.

But I would suggest you to use inputAccessoryView instead. Refer this SO post.

And Must read this SO answer.

You can add inputAccessoryView with 'Apply' and 'Cancel' buttons, and dismiss the number pad with then.

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

Without using done button you can also hide numeric keyboard this way by touching anywhere on the view

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [yourtextfieldname resignFirstResponder];
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top