2010-09-29 16 views
19

क्या कोई बता सकता है कि प्रतिनिधि UIAlertView पर कैसे काम करता है? क्या इसे स्वचालित रूप से बुलाया जाता है या क्या मुझे इसे कॉल करना है? उदाहरण के लिए:UIAlertView प्रतिनिधि

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

+0

मैं ब्लॉक कॉलबैक साथ UIAlertView प्रतिनिधिमंडल की जगह के लिए एक सामान्य वर्ग में लिखा था। आप इसे यहां देख सकते हैं [http://stavash.wordpress.com/2013/01/31/quick-tip-uialertview-with-a-block-callback/)। – Stavash

उत्तर

11

तो जब तक आप सही तरीके से UIAlertView के प्रतिनिधि संपत्ति स्थापित कर रहे हैं और प्रोटोकॉल को लागू करने, यह स्वतः ही जब कोई उपयोगकर्ता आपके चेतावनी में एक बटन पर क्लिक करता है बुलाया जाएगा।

कार्रवाई में देखने के लिए http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html पर "संबंधित नमूना कोड" के अंतर्गत सूचीबद्ध परियोजनाओं पर नज़र डालें।

+0

ऐसा लगता है कि मैं प्रतिनिधि को फोन नहीं कर रहा था: in in in init, te त्वरित प्रतिक्रिया – Joe

2

alertView:clickedButtonAtIndex: प्रतिनिधि का तरीका स्वचालित रूप से UIAlertView द्वारा बुलाया जाता है। UIAlertView के लिए init विधि पैरामीटर में से एक के रूप में एक प्रतिनिधि लेता है। बस उस ऑब्जेक्ट को पास करना सुनिश्चित करें जो alertView:clickedButtonAtIndex: पर प्रतिक्रिया देता है।

32

मान लीजिए कि आप एक चेतावनी से पता चला है जहां प्रतिनिधि के लिए था "स्व"

- (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 

आपका ज फ़ाइल की आवश्यकता होगी कार्यान्वयन वक्तव्य में UIAlertViewDelegate को संदर्भित करने के लिए:

@interface myViewController : UIViewController <UIAlertViewDelegate> { 
} 

यह वही है जो y UIAlertViewDelegate विधि कॉल का जवाब देने के लिए हमारी .m फ़ाइल।

+2

के लिए धन्यवाद हेडर में सूचीबद्ध प्रतिनिधि होना जरूरी नहीं है, लेकिन अच्छा अभ्यास – braden

+0

यह दस्तावेज़ में क्यों नहीं कहा गया है ?? https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/initWithTitle:message:delegate:cancelButtonTitle :otherButtonTitles: –

10

यहां प्रतिनिधि के लिए एक रैपर है ताकि आप इसके बजाय ब्लॉक का उपयोग कर सकें। निष्पादन का प्रवाह वही होगा लेकिन कोड का प्रवाह पालन करना आसान होगा। तो, उपयोग:

[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 
संबंधित मुद्दे