ios - 如何将操作发送到从 UIAlertView 委托(delegate)的 UIImage subview

标签 ios xcode cocoa-touch uiimageview uialertview

基本上我有一个弹出的UIAlertView。当用户选择一个按钮时,它会调用自己,并根据按钮索引调出一个 UIImageView subview 。我的问题是,因为 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];
  }
}

最佳答案

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)];

关于ios - 如何将操作发送到从 UIAlertView 委托(delegate)的 UIImage subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7594168/

相关文章:

iphone - 如何将 NSArray 中的对象放入 NSSet 中?

ios - Swift中的关联对象,全局键是否实际产生特定实例?

ios - 调试 Xcode 异常断点;未记录异常 - redux

ios - 将训练好的 Keras 图像分类模型转换为 coreml 并集成到 iOS11

ios - "Missing retina 4-inch launch image"错误,但我有那个图像

iphone - 在模拟器中使用真实定位服务

ios - 如何更改核心数据数据库 : baby steps?

objective-c - 从静态库手动创建框架

xcode - install_name_tool -change 和 -id 之间的区别

iphone - 是否有可能为 UILocalNotification 设置条件 `firedate`?