2010-06-10 6 views
6

मैं बुनियादी UIAlertView के लेखन के बीमार हूँ, अर्थात्:UIAlertView के लिए एक फ़ंक्शन लिख रहा है?

UIAlertView *alert = [[UIAlertView alloc] initWith...]] //etc 

इसके बजाय ऐसा करने का एक "सहायक" समारोह है, जहां मैं buttonIndex, या जो कुछ भी एक चेतावनी लौट सकते में यह सब डाल करने के लिए यह संभव है आमतौर पर रिटर्न?

एक साधारण सहायक समारोह के लिए मुझे लगता है कि आप शीर्षक, संदेश के लिए पैरामीटर खिला सकते हैं, मुझे यकीन नहीं है कि आप पैरामीटर में प्रतिनिधियों को पास कर सकते हैं या बंडल जानकारी।

छद्म कोड में, यह इस तरह से हो सकता है:

someValueOrObject = Print_Alert(Title="", Message="", Delegate="", Bundle="") // etc 

किसी भी मदद के लिए इस पर बहुत अच्छा होगा।

धन्यवाद

+0

यह बेहतर टैग किया जा सकता है। 'उद्देश्य-सी', 'iphone', आदि जोड़ने पर विचार करें –

+0

क्षमा करें। आपकी सहायता के लिए धन्यवाद. – zardon

उत्तर

2

यह मैं क्या लिखा है, जब मैं एक ही करने का बीमार हो गया है:

-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName { 
    [self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: nil]; 
} 

-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName informing:(id)delegate { 
    [self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: delegate]; 
} 

-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName withExtraButtons:(NSArray *)otherButtonTitles informing:(id)delegate { 
    UIAlertView *alert = [[UIAlertView alloc] 
       initWithTitle: title 
       message: message 
       delegate: delegate 
       cancelButtonTitle: firstButtonName 
       otherButtonTitles: nil]; 
    if (otherButtonTitles != nil) { 
    for (int i = 0; i < [otherButtonTitles count]; i++) { 
     [alert addButtonWithTitle: (NSString *)[otherButtonTitles objectAtIndex: i]]; 
    } 
    } 
    [alert show]; 
    [alert release]; 
} 

आप एक समारोह है कि एक चेतावनी प्रदर्शित करेगा और फिर एक तरह कोई मान नहीं लिख सकते हैं बटन इंडेक्स हालांकि, क्योंकि वह मान-वापसी केवल तब होती है जब उपयोगकर्ता बटन दबाता है और आपका प्रतिनिधि कुछ करता है।

दूसरे शब्दों में, UIAlertView के साथ एक प्रश्न पूछने की प्रक्रिया एक असीमित है।

+0

ओह, ठीक है। आपकी सहायताके लिए धन्यवाद! – zardon

14

में 4.0+ आप इस तरह एक सा, ब्लॉक का उपयोग चेतावनी कोड को आसान बनाने में कर सकते हैं:

CCAlertView *alert = [[CCAlertView alloc] 
    initWithTitle:@"Test Alert" 
    message:@"See if the thing works."]; 
[alert addButtonWithTitle:@"Foo" block:^{ NSLog(@"Foo"); }]; 
[alert addButtonWithTitle:@"Bar" block:^{ NSLog(@"Bar"); }]; 
[alert addButtonWithTitle:@"Cancel" block:NULL]; 
[alert show]; 

Lambda Alert on GitHub देखें।

+0

धन्यवाद, मुझे इसकी ज़रूरत है! –

+0

बहुत हल्का समाधान। मेरे लिए बहुत अच्छा काम किया। – Kongress

+0

क्या 'LambdaAlert' कक्षा का उपयोग किए बिना ऐसा करने के लिए वैसे भी है? – Chris