如何将动作发送到UIImage子视图,从UIAlertView委派

时间:2011-09-29 07:57:48

标签: ios xcode cocoa-touch uiimageview uialertview

基本上我弹出一个UIAlertView。当用户选择一个按钮时,它会自行调用,并根据按钮索引显示UIImageView子视图。我的问题是,因为UIImageView占据了屏幕的很大一部分,它位于UIAlertView之上,所以取消按钮是看不见的。我想知道我是否可以在UIImageView上创建动作,这样如果在内部点击或触摸它,UIAlertView / UIImageView会辞职并消失吗?

有人请求帮助,我已经在这里工作了几个小时。

以下是点击按钮的代码和按钮索引。

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex     {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
 //     exit(0);
}
else
{
    UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:@"molecule" message:molURL delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 260)];


    NSString *MyURL = molURL;

    NSString *apURL = [NSString stringWithFormat:@"%@",MyURL];


    NSURL * imageURL = [NSURL URLWithString:apURL];
    NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage * image1 = [UIImage imageWithData:imageData];


    //UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
    [imageView setImage:image1];


    [successAlert addSubview:imageView];

    [successAlert show];
  }
}

1 个答案:

答案 0 :(得分:1)

MyImageView.h

#import <UIKit/UIKit.h>

@interface MyImageView : UIImageView {



}

@end

MyImageView.m

#import "MyImageView.h"


@implementation MyImageView


- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

        self.userInteractionEnabled=YES;

    }
    return self;
}


-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [UIView animateWithDuration:2.0f animations:^(void) {


        self.alpha=0.0f;

        super.hidden =YES;

        UIAlertView *alert=(UIAlertView*)self.superview;


        alert.alpha=0.0f;

        [alert dismissWithClickedButtonIndex:0 animated:YES];

        //or

        //[alert removeFromSuperview];

    }];




}


- (void)dealloc {
    [super dealloc];
}


@end

MyViewController.h

    @class MyImageView;

MyViewController.m

    #import "MyImageView.h"

然后创建imageView

    MyImageView *specialImageView =[[MyImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 260)];