Uipickerview项目工作背后

问题描述:

我的视图控制器中有pickerview.我通过添加到子视图中,然后更改子视图的框架来显示它.当我显示pickerview时,后面有一个按钮.当我在使pickerview无效之后单击那些区域时,仍会调用该按钮动作.如何正确设置pickerview?

I have pickerview in my view controller. I am displaying it by adding into subview and then changing frame of subview. When my pickerview displayed I have one button behind it. When I click on those area after dispalying pickerview still that button action is called. How to set pickerview proper?

U子类如下所示的PickerView将帮助您解决此问题.

U subclass the PickerView like below will help u to fix this issue.

//
//  WPCustomPickerView.h
//  test
//
//  Created by VASANTH K on 08/01/14.
//  
//

#import <UIKit/UIKit.h>

@interface WPCustomPickerView : UIDatePicker

@end

实施文件

//
//  WPCustomPickerView.m
//  test
//
//  Created by VASANTH K on 08/01/14.
//  
//

#import "WPCustomPickerView.h"

@implementation WPCustomPickerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    id hitview=[super hitTest:point withEvent:event];
     if(!hitview)
     {
     if(point.y<=self.frame.size.height&&point.y>=0)
     return self;
      }
return hitview;
}


@end

在这里,我将覆盖hitTest以对用户交互做出UIPickerView响应.当用户直接触摸主视图而不是选择器内容时,苹果通过这种方式通过返回nil来使主选择器视图对于用户触摸透明.

Here i override the hitTest to make UIPickerView response for the user interaction. This is the way how apple make main picker view transparent for user touch by return nil when user directly touch on the main view instead of picker content.