2011-09-29 48 views
1

本質上,我彈出UIAlertView。當用戶選擇一個它自己調用的按鈕時,它會根據按鈕索引調出一個子視圖。我的問題是,因爲UIImageView佔據了屏幕的很大一部分,所以它放在UIAlertView之上,因此取消按鈕是不可見的。我想知道是否可以在UIImageView上創建操作,以便在點擊或觸摸內部時,UIAlertView/UIImageView會辭職並消失?如何將操作發送到UIAmage子視圖,從UIAlertView委派的代理

有人請幫忙,我一直在這工作幾個小時。

這裏是被點擊的按鈕和按鈕索引的代碼。

- (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]; 
    } 
} 
+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)]; 
相關問題