OStack程序员社区-中国程序员成长平台

标题: ios - touchesBegin 没有被调用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:45
标题: ios - touchesBegin 没有被调用

我使用 UIPicker 来填充 UITextField 而不是键盘。这很好用,但我现在无法关闭 UIPicker。我希望能够点击屏幕上的任意位置并关闭 UIPicker。我已经尝试了每种触摸方法,但都不会触发。

setUserInteractionEnabled:是的。 不确定它是否有区别,但我使用的是 StoryBoard

我应该在我的 AppDelegate 中设置一些东西来监听触摸吗?

这是我的 .h

 #import <UIKit/UIKit.h>

@interface RNMemberTableViewController : UITableViewController<UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIPickerView *behaviorPicker;
@property (nonatomic, weak) IBOutlet UIDatePicker *dateOfRecordPicker;
@property (nonatomic, strong) NSArray *behaviorLevels;
@property (weak, nonatomic) IBOutlet UITextField *behaviorTextField;

@end

这里是一些实现...

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setUserInteractionEnabled:YES];
    [self buildBehaviorPicker];
    NSLog(@"Member View");
}

- (void)touchesBeganNSSet *)touches withEventUIEvent *)event
{
    NSLog(@"test");
    [self.view touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    int tCount = touch.tapCount;
    NSLog(@"Touched %d", tCount);
}

- (void) buildBehaviorPicker
{
    behaviorLevels = [[NSArray alloc] initWithObjects"Unsatisfactory", @"Needs Improvement", @"Satisfactory", @"Outstanding", nil];

    UIPickerView *pickerView = [[UIPickerView alloc] init];
    pickerView.dataSource = self;
    pickerView.delegate = self;
    [pickerView selectRow:2 inComponent:0 animated:NO];
    self.behaviorTextField.inputView = pickerView;


}

提前致谢 -鲍勃



Best Answer-推荐答案


您可以随时尝试以下方法:

-(void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self actionselector(tapReceived];
    [tapGestureRecognizer setDelegate:self];
    [self.view addGestureRecognizer:tapGestureRecognizer];
}

-(void)tapReceivedUITapGestureRecognizer *)tapGestureRecognizer
{
    // do something, like dismiss your view controller, picker, etc., etc.
}

希望这会有所帮助...

关于ios - touchesBegin 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24621402/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4