如何将操作发送到 UIImage 子视图(从 UIAlertView 委托)

发布于 2024-12-06 20:29:38 字数 1255 浏览 1 评论 0原文

本质上我有一个弹出的 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];
  }
}

Essentially I have a UIAlertView that pops up. When a user selects a button it calls itself, and based on the button index it brings up a UIImageView subview. My question is, because the UIImageView takes up a good portion of the screen it lays on top of the UIAlertView, so the cancel button is unseen. I was wondering if I could create actions on the UIImageView so that if it is clicked or touched up inside, the UIAlertView/UIImageView would resign and disappear?

Someone please help, I've been tooling around this for hours.

Here is the code from the button being clicked, and the button index.

- (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];
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为一人 2024-12-13 20:29:38

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

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"

then while create imageView

    MyImageView *specialImageView =[[MyImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 260)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文