在 uipopover 中时,UIButton 不响应 touchupinside

Posted

技术标签:

【中文标题】在 uipopover 中时,UIButton 不响应 touchupinside【英文标题】:UIButton does not respond to touchupinside when in a uipopover 【发布时间】:2012-04-20 13:03:11 【问题描述】:

我在 UIPopoverController 中添加了一个按钮,但它似乎对触摸没有响应。我不知道我是否应该在 UIPopoverController 上设置一些属性或什么。这是呈现弹出视图和按钮的代码。

- (void)topicImageButtonPressed

    CGRect aFrame = CGRectMake(0.0, 0.0, 1000.0, 600.0);
    UIViewController *aView = [[UIViewController alloc] init];
    aView.view.frame = aFrame;
    UIImageView *iView = [[UIImageView alloc] init];
    [iView setContentMode:UIViewContentModeScaleAspectFit];
    [iView setImage:self.topicImageView1.image];
    aView.view = iView;

    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [nextButton addTarget:self 
                   action:@selector(quizButtonPressed:)
     forControlEvents:UIControlEventTouchUpInside];
    [nextButton setTitle:@"Next" forState:UIControlStateNormal];

    nextButton.frame = CGRectMake(700.0, 550.0, 160.0, 40.0);

    [aView.view addSubview:nextButton];

    //aView.view.backgroundColor = [UIColor colorWithPatternImage:self.topicImageView1.image];

    imagePopoverController = [[UIPopoverController alloc] 
                         initWithContentViewController:aView];
    imagePopoverController.popoverContentSize = CGSizeMake(1000, 600);

    imagePopoverController.passthroughViews = [NSArray arrayWithObject:nextButton];

    [imagePopoverController presentPopoverFromRect:CGRectMake(212, 10, 1000, 600) inView:self.view 
                     permittedArrowDirections:0 animated:YES];

【问题讨论】:

是这个问题吗,你的按钮正在触地但没有可见的视图? 按钮未获得触摸事件。我向它添加了一个 NSLog 语句,以查看它是否被触及。 【参考方案1】:

添加它使其工作。 iView.userInteractionEnabled = YES;

【讨论】:

嘿,很高兴看到你自己找到它。我赞成它..也许你会得到“自学”徽章。! :)【参考方案2】:
UIViewController *popoverContent = [[UIViewController alloc]init];
    UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];

    datepiker= [[UIDatePicker alloc]init];
    [datepiker setFrame:CGRectMake(0, 0, 320, 216)];
    datepiker.datePickerMode=UIDatePickerModeDateAndTime;
    datepiker.hidden=NO;
    datepiker.minimumDate=[NSDate date];

    [self.view addSubview:datepiker];

    [datepiker release];
    // [datepiker addTarget:self action:@selector(changedDate:) forControlEvents:UIControlEventValueChanged];

    btn_add=[[UIButton alloc]initWithFrame:CGRectMake(115, 250, 100, 30)];
    [btn_add setTitle:@"Add" forState:UIControlStateNormal];
    [btn_add setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
    [btn_add setBackgroundColor:[UIColor redColor]];
    [btn_add addTarget:self action:@selector(AddDate:) forControlEvents:UIControlEventTouchUpInside];
    [popoverView addSubview:btn_add];

    btn_cancel=[[UIButton alloc]initWithFrame:CGRectMake(115, 300, 100, 30)];
    [btn_cancel setTitle:@"Cancel" forState:UIControlStateNormal];
    [btn_cancel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
    [btn_cancel setBackgroundColor:[UIColor redColor]];
    [btn_cancel addTarget:self action:@selector(CancelDate:) forControlEvents:UIControlEventTouchUpInside];
    [popoverView addSubview:btn_cancel];

    [popoverView addSubview:datepiker];
    [popoverView addSubview:btn_add];
    popoverContent.view = popoverView;
    popoverContent.contentSizeForViewInPopover = CGSizeMake(320,350);


    self.popoverController = [[UIPopoverController alloc]
                                  initWithContentViewController:popoverContent];
    [self.popoverController  presentPopoverFromRect:CGRectMake(230,250, 320,220) 
                                                 inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];


    [popoverView release];
    [popoverContent release];


-(void)AddDate:(id)sender


-(void)CancelDate:(id)sender

[popoverController dismissPopoverAnimated:YES];


试试这个..它成功地为我工作。

【讨论】:

并根据您的需要进行更改...和属性并合成popover和datepiker。并且还给了代表

以上是关于在 uipopover 中时,UIButton 不响应 touchupinside的主要内容,如果未能解决你的问题,请参考以下文章

如何从 UIPopover 中的 UIButton 在主 UIView 中创建 UITextView?

iPad键盘分裂时调整UIPopover

UIPopover 中的导航栏

UIPopover 中显示时的 UINavigationController 内容偏移

Objective C UIButton with divider并在选中时更改textcolor

Objective C - UIButton 保持突出显示/选中,背景颜色和字体颜色在突出显示/选中时发生变化