UIBUTTON and picker view

前端 未结 3 1205
一整个雨季
一整个雨季 2021-01-20 17:28

I want to display a picker when the user clicks on a button I subclassed the UIButton and changed its inputview so that it is writable.

I followed this link http://

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-20 17:47

    1) Create a subclass of a UIButton

    .h

    #import 
    
    @interface UIButtonAsFirstResponder : UIButton
    
    @property (strong, nonatomic) UIView *inputView;
    @property (strong, nonatomic) UIView *inputAccessoryView;
    
    @end
    

    .m

    #import "UIButtonAsFirstResponder.h"
    
    @implementation UIButtonAsFirstResponder
    
    - (BOOL) canBecomeFirstResponder
    {
        return YES;
    }
    
    @end
    

    2) Set inputView (if needed also inputAccessoryView) in your view controller (to your picker or any other custom view)

    3) Create IBAction for your button in your view controller

    - (IBAction)becom:(id)sender {
        [sender becomeFirstResponder];
    }
    

    If you want to remove keyboard, call on your button:

    [button resignFirstResponder];
    

    or in view controller:

    [self.view endEditing:YES];
    

提交回复
热议问题