触摸事件不能以编程方式创建的视图

时间:2014-03-26 09:28:44

标签: ios

我想在viewdidload中以编程方式使用按钮,文本框,标签创建视图 但是触摸事件不适用于视图及其子视图,即使我已将userinteractionenabled设置为YES。 这是我的代码:

    CGRect bounds = [[UIScreen mainScreen] bounds];

    //creating a view
    UIView* mainView = [[UIView alloc] initWithFrame: bounds];
    [mainView setUserInteractionEnabled:YES];
    [mainView setBackgroundColor: [UIColor yellowColor]];    
    CGRect buttonFrame = CGRectMake( 100, 80, 100, 50 );
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
    myButton.frame = buttonFrame;
    [myButton setBackgroundColor:[UIColor greenColor]];
    myButton.userInteractionEnabled=YES;
    [myButton addTarget: self
               action: @selector(buttonClicked:)
     forControlEvents: UIControlEventAllTouchEvents];    
    [myButton setTitle: @"My Button" forState: UIControlStateNormal];

 //adding button to myview

    [mainView addSubview: myButton];

    //adding label to myview

    UILabel* myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
    myLabel.text=@"my text";
    [mainView addSubview:myLabel];

    //adding textbox to myview   

    UITextField* textbox=[[UITextField alloc]initWithFrame:CGRectMake(100, 210, 200, 50)];
    textbox.text=@"my text in textfield";
    textbox.userInteractionEnabled=YES;
    [mainView addSubview:textbox];    

    //finally adding my view to self.view

    [self.view addSubview:mainView];

2 个答案:

答案 0 :(得分:0)

试试这个

//creating a view
UIView* mainView = [[UIView alloc] initWithFrame: bounds];
[mainView setUserInteractionEnabled:YES];
[mainView setBackgroundColor: [UIColor yellowColor]];

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tappedOnView:)]; //add the gesture to get the touch event
tapGesture.numberOfTapsRequired = 1;
[mainView addGestureRecognizer:tapGesture];

CGRect buttonFrame = CGRectMake( 100, 80, 100, 50 );
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
myButton.frame = buttonFrame;
[myButton setBackgroundColor:[UIColor greenColor]];
myButton.userInteractionEnabled=YES;
[myButton addTarget: self
             action: @selector(buttonClicked:)
   forControlEvents: UIControlEventTouchUpInside];
[myButton setTitle: @"My Button" forState: UIControlStateNormal];

//adding button to myview

[mainView addSubview: myButton];

//adding label to myview

UILabel* myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
myLabel.text=@"my text";
[mainView addSubview:myLabel];

//adding textbox to myview

UITextField* textbox=[[UITextField alloc]initWithFrame:CGRectMake(100, 210, 200, 50)];
textbox.text=@"my text in textfield";
textbox.userInteractionEnabled=YES;
[mainView addSubview:textbox];

//finally adding my view to self.view

[self.view addSubview:mainView];

答案 1 :(得分:0)

修改行

//UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

并在代码中添加方法

-(void)buttonClicked:(UIButton *)button
{  
    NSLog(@"hai");

}

并在界面构建器中选中复选框enable user interaction,其工作正常。