2011-11-24 17 views
10

我想通過一次點擊就解除UIAlertView的任何位置。我想顯示一個沒有任何按鈕的UIAlertView。iOS如何在任何地方點擊一次UIAlertView?

我在這裏有標準的UIAlertView代碼,但我需要輸入如何解除它,如果可能的話。

使用UITouch?用UITapGestureRecognizer?

謝謝。


編輯:

在viewDidLoad中

alertview初始化這裏有名爲 「警戒」

 if (alert) 
    { 
    emptyview = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)]; 
    emptyview.backgroundColor = [UIColor clearColor]; 
    [self.view addSubview:emptyview]; 
     [emptyview addSubview:alert]; 
    NSLog (@"emptyview is there!"); 

     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
     [emptyview addGestureRecognizer:singleTap]; 
     [singleTap release];    
    } 

然而,所有這些emptyview犯規沒有反應,它不以handleSingleTap選擇響應,我重寫了一下:

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{ 
    [alert dismissWithClickedButtonIndex:0 animated:YES]; 
    [emptyview removeFromSuperview]; 
} 

我需要這個空的視圖正在警報時顯示警報,然後我可以關閉一個水龍頭警報。

我想:

 if (alert) 
    { 
    emptyview = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)]; 
    emptyview.backgroundColor = [UIColor clearColor]; 
    [self.view addSubview:emptyview]; 
     [emptyview addSubview:alert]; 
    NSLog (@"emptyview is there!"); 

     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
     [alert addGestureRecognizer:singleTap]; 
     [singleTap release];    
    } 

當然,警惕並以handleSingleTap響應函數。空視圖有什麼問題?


第二個編輯:

我想在這種情況下,實現在Kindle應用程序中選擇一個詞,類似的功能後,以顯示與解釋一個小觀點,如果你有一個。 也許我應該創建一個UIView而不是UIAlertView?但Kindle應用程序中的小視圖對於低於它的陰影非常好,它怎麼可能?

回答

10

這聽起來像你基本上是試圖重新iOS上「吐司」刪除了這一觀點。好消息是,有人已經這樣做了。 See this project.

編輯:不想使用iToast。我喜歡你的風格,更少的代碼。這是我想出的。這似乎很明顯,因爲其他人已經表示,要克服UIAlertView的模態性質,唯一的方法是添加超級視圖來處理觸摸事件。但是您不必每次都手動執行此操作,請考慮子類UIAlertView。嘗試這樣的:

編輯: @wagashi,感謝您接受我的答案,並感謝有關setFrame:是一個調整大小的好地方。你的代碼確實做了一個非常敬酒的小警戒,但是當我嘗試它時,我發現如果消息太長,視圖似乎分崩離析。因此,我修改了setFrame:,只是將警報的大小縮小了一個按鈕的大小,並保持居中在屏幕上。所以班級準確地回答問題標題「iOS如何在任何地方輕輕一點地解除UIAlertView?」

NoButtonAlertView.h

#import <UIKit/UIKit.h> 
@interface _NoButtonAlertViewCover : UIView 
@property (nonatomic,assign) UIAlertView *delegate; 
@end 
@interface NoButtonAlertView : UIAlertView 
-(id)initWithTitle:(NSString *)title message:(NSString *)message; 
@end 

NoButtonAlertView.m

#import "NoButtonAlertView.h" 
@implementation _NoButtonAlertViewCover 
@synthesize delegate = _delegate; 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [self removeFromSuperview]; 
    [_delegate dismissWithClickedButtonIndex:0 animated:YES]; 
} 
@end 
@implementation NoButtonAlertView 
-(void)show{ 
    [super show]; 
    _NoButtonAlertViewCover *cover = [[_NoButtonAlertViewCover alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    cover.userInteractionEnabled = YES; 
    cover.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.01]; 
    cover.delegate = self; 
    [self.superview addSubview:cover]; 
} 
-(id)initWithTitle:(NSString *)title message:(NSString *)message{ 
    if ((self = [super initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil])){ 
    } 
    return self; 
} 
- (void)setFrame:(CGRect)rect { 
    // Called multiple times, 4 of those times count, so to reduce height by 40 
    rect.size.height -= 10; 
    self.center = self.superview.center; 
    [super setFrame:rect]; 
} 
@end 

有了這個簡單UIAlertView子及其UIView子類覆蓋,你可以簡單地,你會用它標準UIAlertView。像這樣:

NoButtonAlertView *alert = [[NoButtonAlertView alloc] initWithTitle:@"Hello" message:@"I am the very model of a modern major general; I'm information, vegitable, animal, and mineral."]; 
[alert show]; 

將產生:

Custom AlertView

+1

嗯,我的目標不是製作或使用由複雜而長的代碼組成的視圖。這個iToast真的很棒,但我想知道是否有可能使用更少的代碼來製作這樣的功能?感謝您與我們分享這個鏈接。 – wagashi

+0

這是無法幫助的!我添加了.m:' - (void)setFrame:(CGRect)rect {0} {0}};}} [超級setFrame:CGRectMake(0,0,rect.size.width,300)]; \t \t [super setFrame:CGRectMake(0,0,100,100)]; \t \t //下面的代碼使UIAlertView居中。 \t //self.center = CGPointMake(320/2,480/2); \t \t //我的自定義cgpointmake \t self.center = CGPointMake(200,400); }' – wagashi

+0

是的,你確實回答了我的問題。然而,我不會在我的「警戒」中提供很多信息,所以CGRectMake 0,0,100,100對我而言就非常完美。我想知道是否可以在UIAlertView中更改文本的字體大小? – wagashi

3

顯示UIAlertView添加到您的視圖控制器(甚至窗口)新的空白UIView全屏幕大小後。附加到這個wiew UITapGestureRecognizer

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
[view addGestureRecognizer:singleTap]; 
[singleTap release]; 

現在handleSingleTap方法,你可以辭退UIAlertView中,並從窗口

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{ 
    [myAlert dismissWithClickedButtonIndex:0 animated:YES]; 
    [view removeFromSuperView]; 
} 
+1

我想補充一個自定義的UIButton沒有文字或圖像 –

+0

沒錯,好主意。它可以節省多行代碼。無論如何,你需要處理自來水行動。 – beryllium

+1

出於好奇,爲什麼要使用'UIAlertView'呢?將全屏UIButton放在所有其他視圖之前,使用任何文本和背景顏色(你喜歡的字母)。 – XJones

相關問題