0

我创建了一个按钮框架设置为与图像框架大小相同的位置UIButtonUIImageView我愿意做的是一旦我触摸到它应该做的图像PopOver。按钮设置了动作。问题是,一旦我触摸它,它就只显示图像,而不是弹出。

我的代码有什么问题?

- (void)viewDidLoad {
    [super viewDidLoad];    
    UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10,100, 100)]; 
    imageView.image=[UIImage imageNamed:@"abc.jpg"];    
    [self.view addSubview:imageView];          
    imageView.userInteractionEnabled = YES;  
    UIButton *imageButton = [[UIButton alloc]init];
    [imageButton setFrame:CGRectMake(0, 0, 100, 100)];
    [imageButton addTarget:self action:@selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:imageButton];
    [imageButton release];
    [imageView release];     
}


-(void)imageTapped:(UIButton*)in_sender {
    UIViewController *popoverContent = [[UIViewController alloc] init];
    CGRect theScreenFrame = [[UIScreen mainScreen] bounds];
    popoverContent.view.frame = CGRectMake(theScreenFrame.origin.x, theScreenFrame.origin.y,100, 100);
    popoverContent.view.backgroundColor = [UIColor whiteColor];
    popoverContent.contentSizeForViewInPopover = CGSizeMake(300, 300); 
    [popoverContent.view addSubview:imageButton];

    if(m_DetailPopover) {
        [m_DetailPopover release];
        m_DetailPopover = nil;
    }
    UIPopoverController m_DetailPopover = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
    [popoverContent release];   

    [m_DetailPopover presentPopoverFromRect:imageButton.frame inView:self.view
     permittedArrowDirections:UIPopoverArrowDirectionAny  animated:YES];    
}
4

3 回答 3

1

问题可能是代码中的错字:

UIPopoverController *m_DetailPopover = [[UIPopoverController alloc] initWithContentViewController:popoverContent];

另请注意,您不能在 imageTapped 中引用 imageButton,因为您在 viewDidLoad 中释放它。

于 2013-03-05T12:08:59.503 回答
0

您的 UIImageView 位于 UIButton 的前面,不允许获取按钮操作。为什么不使用图像创建自定义按钮?

这样做:

UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setImage:[UIImage imageNamed:@"abc.jpg"]];
[imageButton setFrame:CGRectMake(0, 0, 100, 100)];
[imageButton addTarget:self action:@selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:imageButton];
[imageButton release];
[imageView release];

编辑

UIPopoverController *m_DetailPopover = [[UIPopoverController alloc] initWithContentViewController:popoverContent];


[m_DetailPopover presentPopoverFromRect:imageButton.frame inView:self.view
 permittedArrowDirections:UIPopoverArrowDirectionAny  animated:YES];  
[popoverContent release];
于 2013-03-05T12:04:34.227 回答
0

不要将图像视图作为子视图添加到按钮,反之亦然。只需将图像(不是图像视图)作为背景图像分配给按钮。

于 2013-03-05T12:05:20.213 回答