Adding a cancel button to UITextField keyboard

后端 未结 3 968
旧巷少年郎
旧巷少年郎 2021-01-11 20:29

Is there a way to add a cancel button to the keyboard displayed for UITextField? Looking over the UITextInputTraits Protocol Reference, I could not

3条回答
  •  礼貌的吻别
    2021-01-11 21:00

    I just dropped a UIToolbar into my view controller in Interface Builder, then:

    @property IBOutlet UIToolbar *keyboardAccessory;
    
    -(void) viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        // this is so I can edit it in Interface Builder, but it doesn't show in the view
        [keyboardAccessory removeFromSuperview];
    }
    
    -(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
        textField.inputAccessoryView = keyboardAccessory;
        return YES;
    }
    
    -(IBAction) pressedCancelButton {
        [self.view endEditing:YES];
    }
    

    Voila!

提交回复
热议问题