2010-01-12 21 views
27

的UIAlertviewDelegate協議有幾個可選的方法包括:是否有可能不排除一個UIAlertView中

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; 

這似乎表明,並非所有的按鈕點擊真正解除警報視圖。但是我看不到配置警報視圖的方法,不能通過任何按鈕按鈕自動關閉。

我需要創建一個子類來完成這個嗎?

的UIAlertViewDelegate協議爲什麼會有:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex; 
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; 

如果它不支持任意解僱不與每個按鈕警報視圖點擊?

旁白: 我意識到UIAlertView的設計目的。但我的目的是爲了讓用戶在應用程序退出之前一些文本到粘貼板複製(當警報視圖駁回其自動發生。

回答

27

是的子類UIAlertView然後超載-dismissWithClickedButtonIndex:animated:,如

@implementation MyAlertView 
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated { 
    if (buttonIndex should not dismiss the alert) 
     return; 
    [super dismissWithClickedButtonIndex:buttonIndex animated:animated]; 
} 
@end 

非官方可以定義

-(void)alertSheet:(UIAlertSheet*)sheet buttonClicked:(id)button; 

方法德爾egate,它將繞過-dismissWithClickedButtonIndex:animated:,但它的無證,所以我不知道它是否適合你。

+0

那就是我現在正在做的。但由於可用的委託方法,似乎我不應該這樣做。哦,以及... – 2010-01-12 20:12:24

+0

有一個未記錄的方法(未測試),請參閱編輯。 – kennytm 2010-01-12 20:30:51

+0

嗯......有趣。太糟糕了,他們已經使用未記錄的API。 – 2010-01-13 15:43:53

3

willPresentAlertView:didPresentAlertView:alertView:willDismissWithButtonIndex:alertView:didDismissWithButtonIndex:用於跟蹤UIAlertView動畫的開始和結束。

不需要跟蹤UIAlertView動畫的應用程序可以簡單地使用alertView:clickedButtonAtIndex:。該方法的文檔說「在調用此方法後,接收器會自動解除。」

+0

謝謝,我錯過了文檔中的最後一段文字。仍然看起來有點額外的方法,因爲它基本上與willPresentAlertView相同... – 2010-01-12 23:13:58

+0

@Darren:謝謝,很多在ios7 alertView:willDismissWithButtonIndex:此方法調用兩次。但你的解決方案節省了我的一天.. – TamilKing 2014-02-20 08:40:52

1

警告

從一些消息來源聽說一些應用已經得到否決 下面這個過程。我在iOS6中很幸運,因此我在這裏顯示代碼 。使用您自己的風險: -/

子類化是最好的方法。創建一個bool警報標誌應該保留與否。

這是UIAlertView

// 
// UICustomAlertView.h 
// 

#import <UIKit/UIKit.h> 

@interface UICustomAlertView : UIAlertView 
{ 

} 
@property(nonatomic, assign) BOOL dontDisppear; 
@end 

// 
// UICustomAlertView.m 
// 

#import "UICustomAlertView.h" 

@implementation UICustomAlertView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated { 

    if(self.dontDisppear) 
     return; 
    [super dismissWithClickedButtonIndex:buttonIndex animated:animated]; 
} 
@end 

的子類,這是我用它爲我的代碼

if(![txtUsername.text isEqualToString:@"admin"] && ![txtPassword.text isEqualToString:@"admin"]) 
{ 
    alertLogin.dontDisppear = YES; 
    alertLogin.message = NSLocalizedString(@"my_alert", nil); 
} 
else 
{ 
    alertLogin.dontDisppear = NO; 
    // proceed 
} 
+0

我已經給出了-1,原因是因爲'「UIAlertView類旨在按原樣使用,並且不支持子類化。此類的視圖層次結構是私有的,必須不要在https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html中修改'',所以實施你的方式將違背Apple文檔和審覈流程,所以會得到他們的應用被拒絕這是幾年以來的方式,所以在你回答這個問題時,這將是一個不正確的答案。 – Popeye 2014-05-29 07:49:58

+0

可能只是我的應用程序去年批准(iOS 6)。 – 2014-05-30 05:33:13

+0

對不起,但這並不意味着它是正確的。我提交了一個應用程序,然後才知道這一點,並通過了初步審覈流程,然後在我提交更新時被拒絕。蘋果審查小組只是人類可以錯過的事情。我知道盡管他們對iOS 7的要求比較嚴格。然而,我的-1完全是因爲這個答案違背了蘋果的文檔,你沒有警告它。如果你警告這一點,我會很高興地刪除我的-1 – Popeye 2014-05-30 06:53:26

1
#import "MLAlertView.h" 

@implementation MLAlertView 


-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated { 
} 

-(void)dismissNow:(NSInteger)buttonIndex { 
    [super dismissWithClickedButtonIndex:buttonIndex animated:YES]; 
} 
2

在我看來:沒有理由繼續alertView。即使你想保留它,只要考慮「重新展示」它,通過保留一個參考,然後調用[alertView show] ==>不需要任何東西。好消息,是吧?

+0

這個工程。雖然在重新顯示彈出窗口時會有輕微的閃爍。 – RajV 2015-12-17 20:11:47

相關問題