0

我是新的目标c我有一张图片,当我点击它时我想显示一些警报消息添加了这样的图片-

AHolder = [[UIImageView alloc] initWithFrame:CGRectMake(5, 80, 40, 40)];
    UIImage *imageA = [UIImage imageNamed:@"A.png"];
    AHolder.image = imageA;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aPressed:) name:@"aPressed" object:nil];
    [view addSubview:AHolder]

它的事件是这样的——

 -(IBAction)aPressed:(id)sender
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"hello" message:@"a pressed" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"ok",nil];
    [alert show];
    [alert release];
}

它没有给我任何错误,但是当我单击图像时也没有任何反应。请建议我任何解决方案。

4

5 回答 5

2

将点击手势添加到图像视图。

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture)];
tapGesture.numberOfTapsRequired=1;
[AHolder setUserInteractionEnabled:YES];
[AHolder addGestureRecognizer:tapGesture];
[tapGesture release];

-(void)handleTapGesture{
//do what ever you want here
}

否则,只需使用带有背景图像的按钮。

于 2013-04-17T12:58:01.813 回答
0

对于您的问题,非常简单且最受欢迎的解决方案是在您的图像上使用自定义 UIButton。然后只需将选择器设置为您想要在单击按钮时调用的方法。确保 UIButton 和 UIImage 的框架相同。

于 2013-04-17T13:21:50.310 回答
0

在 xib 文件中添加“圆形矩形按钮”对象。将“Type”属性设置为“custom”,将“Background”属性设置为“nameofyourimage.extension”。

将 aPressed: 方法连接到圆形矩形按钮的 touch up inside 事件。

于 2013-04-17T15:28:41.580 回答
0

不需要把UIButton你的形象。只需使用UIButton而不是UIImageView. 您可以设置UIButton backgroundimageimage属性为您想要的图像。

除此之外,您可以UITapGestureRecognizer像这样使用:

将此代码放入viewDidLoad

UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[yourImage addGestureRecognizer:rec];
[yourImage setUserInteractionEnabled:YES]; //!! this is important

并实现这个委托方法

#define unused_related_to_gesture
- (void)tapRecognized:(UITapGestureRecognizer *)recognizer
{
    if(recognizer.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"\nUIGestureRecognizerStateBegan\n");
    }
    if(recognizer.state == UIGestureRecognizerStateCancelled)
    {
        NSLog(@"\nUIGestureRecognizerStateCancelled\n");
    }
    if(recognizer.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"\nUIGestureRecognizerStateChanged\n");
    }
    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"\nUIGestureRecognizerStateEnded\n");
    }
    if(recognizer.state == UIGestureRecognizerStateFailed)
    {
        NSLog(@"\nUIGestureRecognizerStateFailed\n");
    }
    if(recognizer.state == UIGestureRecognizerStatePossible)
    {
        NSLog(@"\nUIGestureRecognizerStatePossible\n");
    }
    if(recognizer.state == UIGestureRecognizerStateRecognized)
    {
        NSLog(@"\nUIGestureRecognizerStateRecognized\n");
        // this section will contain your code of clicking the image.
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"hello" message:@"a pressed" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"ok",nil];
        [alert show];
        [alert release];
    }
}
于 2013-04-18T08:03:00.680 回答
0

使用 tapGestureRecognizer。因此,每当用户点击 iamgeview 时,您都可以随心所欲

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(aPressed)];
tapGesture.numberOfTapsRequired=1;
[aHolder setUserInteractionEnabled:YES];
[aHolder addGestureRecognizer:tapGesture];

-(void)aPressed{
//do what ever you want here
}
于 2013-04-17T12:59:33.820 回答