有人能如何委託解釋到UIAlertView
作品?它是自動調用還是必須調用它?例如:UIAlertView中代表
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
有人能如何委託解釋到UIAlertView
作品?它是自動調用還是必須調用它?例如:UIAlertView中代表
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
只要你正確設置UIAlertView中的委託財產和執行協議,它會自動在用戶點擊您的警報按鈕調用。
看看「相關的示例代碼」中列出的項目在http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html看到它在行動。
看起來像我沒有調用委託:自我在初始化中,感謝您的快速回應 – Joe 2010-09-29 23:54:35
委託的alertView:clickedButtonAtIndex:
方法被自動通過UIAlertView
調用。 UIAlertView
的init方法將委託作爲參數之一。只要確保傳入響應alertView:clickedButtonAtIndex:
的對象。
比方說,你顯示一個警告,其中的代表是「自我」
- (void)showAlert {
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"My Alert"
message:@"Do you want to continue?"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"No", @"Yes", nil];
[myAlert show];
[myAlert release];
}
爲了下面在.m文件的工作:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
您的.h文件中需要在執行語句中引用UIAlertViewDelegate,如下所示:
@interface myViewController : UIViewController <UIAlertViewDelegate> {
}
這就是允許y我們的.m文件響應UIAlertViewDelegate方法調用。
有頭文件中列出的委託沒有必要,但是很好的做法 – braden 2014-03-06 14:51:13
爲什麼這不是說在文檔!? https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: – 2014-04-01 10:13:33
這是一個委託包裝,以便您可以使用塊代替。執行流程將是相同的,但代碼的流程將更容易遵循。所以,用法:
[YUYesNoListener yesNoWithTitle:@"My Title" message:@"My Message" yesBlock:^
{
NSLog(@"YES PRESSED!");
}
noBlock:^
{
NSLog(@"NO PRESSED!");
}];
...這裏是輔助類:
typedef void(^EmptyBlockType)();
@interface YUYesNoListener : NSObject <UIAlertViewDelegate>
@property (nonatomic, retain) EmptyBlockType yesBlock;
@property (nonatomic, retain) EmptyBlockType noBlock;
+ (void) yesNoWithTitle:(NSString*)title message:(NSString*)message yesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock;
@end
@implementation YUYesNoListener
@synthesize yesBlock = _yesBlock;
@synthesize noBlock = _noBlock;
- (id) initWithYesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock
{
self = [super init];
if (self)
{
self.yesBlock = [[yesBlock copy] autorelease];
self.noBlock = [[noBlock copy] autorelease];
}
return self;
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0 && self.noBlock)
self.noBlock();
else if (buttonIndex == 1 && self.yesBlock)
self.yesBlock();
[_yesBlock release];
[_noBlock release];
[alertView release];
[self release];
}
- (void) alertViewCancel:(UIAlertView *)alertView
{
if (self.noBlock)
self.noBlock();
[_yesBlock release];
[_noBlock release];
[alertView release];
[self release];
}
+ (void) yesNoWithTitle:(NSString*)title message:(NSString*)message yesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock
{
YUYesNoListener* yesNoListener = [[YUYesNoListener alloc] initWithYesBlock:yesBlock noBlock:noBlock];
[[[UIAlertView alloc] initWithTitle:title message:message delegate:yesNoListener cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil] show];
}
@end
我寫了一個通用類與塊回調更換UIAlertView中代表團。你可以查看[這裏](http://stavash.wordpress.com/2013/01/31/quick-tip-uialertview-with-a-block-callback/)。 – Stavash 2013-01-31 21:53:10