2012-03-12 8 views
8

मेरे पास एक ऐसा फ़ंक्शन है जो YES/NO बटन के साथ UIAlertView दिखाता है, और इसका उपयोग केवल फ़ंक्शन के दायरे के अंदर किया जाता है, इसलिए मैं उपयोगकर्ता प्रतिक्रिया को पकड़ने के लिए एक प्रतिनिधिमंडल को लागू नहीं करना चाहता हूं।प्रतिनिधिमंडल के बिना UIAlertView परिणाम को संभालने का कोई आसान तरीका है?

[alert show]; 
if([alert indexOfClickedButton] == indexOfYes) 
{ 
.... 
} 

या लैम्ब्डा अभिव्यक्ति के रूप में एनीमेशन

में
+1

UIAlertView सिर्फ एक ही रास्ता है कि यू एक कस्टम UIAlertView बनाने होती है। – ios

+2

यहां एक ऐसी साइट है जो ब्लॉक-आधारित यूआईएलर्टव्यूव (अन्य चीजों के साथ) लागू करती है: http://blog.mugunthkumar.com/coding/ios-code-block-based-uialertview-and-uiactionsheet/ – lnafziger

+0

UIAlertView * aletView = [[UIAlertView alloc] initWithTitle: @ "कोई शीर्षक" संदेश: @ "Hiii" प्रतिनिधि: स्वयं रद्द करें बटनटन: @ "रद्द करें" अन्य बटन बटन: शून्य, शून्य]; [चेतावनी शो]; यदि आप चाहें तो किसी भी प्रतिनिधि का उपयोग न करें। –

उत्तर

23

प्रतिनिधिमंडल से पूरी तरह से बचने का कोई तरीका नहीं है, लेकिन आप इन पंक्तियों के साथ उस प्रभाव के लिए एक रैपर बना सकते हैं:

@interface MyAlertViewDelegate : NSObject<UIAlertViewDelegate> 

typedef void (^AlertViewCompletionBlock)(NSInteger buttonIndex); 
@property (strong,nonatomic) AlertViewCompletionBlock callback; 

+ (void)showAlertView:(UIAlertView *)alertView withCallback:(AlertViewCompletionBlock)callback; 

@end 


@implementation MyAlertViewDelegate 
@synthesize callback; 

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

+ (void)showAlertView:(UIAlertView *)alertView 
     withCallback:(AlertViewCompletionBlock)callback { 
    __block MyAlertViewDelegate *delegate = [[MyAlertViewDelegate alloc] init]; 
    alertView.delegate = delegate; 
    delegate.callback = ^(NSInteger buttonIndex) { 
     callback(buttonIndex); 
     alertView.delegate = nil; 
     delegate = nil; 
    }; 
    [alertView show]; 
} 

@end 

(एआरसी माना जाता है, यदि आप इसे उपयोग नहीं कर रहे बदलने [delegate release] करने के लिए delegate = nil।)

प्रयोग की तरह कुछ होगा:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirm" message:@"Yes or No?" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes",@"No", nil]; 
[MyAlertViewDelegate showAlertView:alert withCallback:^(NSInteger buttonIndex) { 
    // code to take action depending on the value of buttonIndex 
}]; 
+0

धन्यवाद, तो आपका समाधान काम करता है। – jAckOdE

+1

मुझे उस लाइन पर एक कंपाइलर चेतावनी मिल रही है जहां आप 'प्रतिनिधि = शून्य' कह रहे हैं: इस ब्लॉक में दृढ़ता से प्रतिनिधि को पकड़ना एक चक्र को बनाए रखने की संभावना है। आईओएस 5.1 एआरसी के तहत यह। –

+1

'प्रतिनिधि = nil' आवश्यक है क्योंकि 'alertView.delegate' एक गैर-बनाए रखने वाली संपत्ति है और हमें ऑब्जेक्ट को" प्रतिनिधि "रखने की आवश्यकता है जब तक कि अलर्ट व्यू बटन कॉलबैक समाप्त न हो जाए। चेतावनी (झूठी सकारात्मक) को #pragma clang diagnostic push' और 'pragma clang diagnostic अनदेखा "-वर्स-रीटेन-चक्र" से पहले अनदेखा किया जा सकता है, और उस पंक्ति के बाद '#pragma clang diagnostic pop'। – AndiDog

1

यह बहुत आसान है:

वहाँ किसी भी तरह से पता करने के लिए क्लिक किया क्या बटन उपयोगकर्ताओं के बिना UIAlertViewDelegate, की तरह कुछ को लागू है।

//Alert 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirm" message:@"Yes or No?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes",@"No", nil]; 
[alert show]; 

आप इस विधि को जोड़ने की आवश्यकता के लिए जा रहे: आप एक चेतावनी, कुछ इस तरह है कहो

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

इस पद्धति की एक संभव कार्यान्वयन इस प्रकार दिखाई देगा:

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

//Checks For Approval 
    if (buttonIndex == 1) { 
     //do something because they selected button one, yes 
    } else { 
     //do nothing because they selected no 
    } 
} 
+13

लेकिन उन्होंने विशेष रूप से पूछा कि यह कैसे करें _ प्रतिनिधिमंडल के बिना ... – Arkku

+0

बहुत सावधानी से नहीं पढ़ा। अगर इससे मदद मिलती है तो – JTApps

+0

धन्यवाद! और हाँ, यह मुझे कोड की कुछ पंक्तियों को बचाएगा, लेकिन मुझे अभी भी फ़ंक्शन के दायरे से बाहर कुछ कोड लिखना होगा और शायद, अलर्टव्यू का संदर्भ सहेजना होगा, यही वह नहीं है जिसे मैं नहीं चाहता हूं। मैं एक ब्लॉक-आधारित समाधान की तलाश में हूं, और यदि ऐसा करने का कोई आसान तरीका नहीं है, तो मुझे लगता है कि आपका समाधान सबसे अच्छा होगा। – jAckOdE

0

आप जो छिपा हो सकता है इस का उपयोग करते हुए कस्टम दृश्य क्या कर सकते हैं और एक्शनशीट्स

UIView *AlertVw=[UIView alloc]initWithFrame:CGRect(x,y,w,h)]]; 

UIButton *SaveButton=[UIButton alloc]initWithFrame:CGRect(x,y,w,h)]]; 
[CustomButton setTitle:@"Ok" forState:UIControlStateNormal]; 
[SaveButton addTarget:self action:@selector(SaveClicked)   forControlEvents:UIControlEventTouchUpInside]; 

UIButton *CancelButton=[UIButton alloc]initWithFrame:CGRect(x,y,w,h)]]; 
[CustomButton setTitle:@"Cancel" forState:UIControlStateNormal]; 
[CancelButton addTarget:self action:@selector(CancelClicked)   forControlEvents:UIControlEventTouchUpInside]; 

[AlertVw addSubview:SaveButton]; 
[AlertVw addSubview:CancelButton]; 

[self.view addSubview:AlertVw]; 

-(void)SaveButton 
{ 
    //Code to apply on Save clicked 
    [AlertVw removeFromSuperView]; //Also you can use AlertView.hidden=YES; 

} 
-(void)CancelButton 
{ 
    //Code to apply on cancel clicked 
    [AlertVw removeFromSuperView]; //Also you can use AlertView.hidden=YES; 

} 
0

कक्षा को प्राप्त करने की कोई आवश्यकता नहीं है। ब्लॉक के साथ, उपयोगकर्ता चयनित बटन इंडेक्स प्राप्त करना आसान है।

http://blog.innovattic.com/uikitblocks/

आप सिर्फ इस की एक काम कार्यान्वयन चाहते हैं:

typedef void(^AlertViewCallBackBlock)(NSInteger selectedIndex); 

@interface ABC() 
    @property (nonatomic, copy) AlertViewCallBackBlock alertViewBlock; 
@end 

@implementation 

- (void)showAlert { 
    self.alertViewBlock = ^(NSInteger selectedIndex) { 
     if (selectedIndex == 1) { 

     } 
    }; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirm" message:@"Yes or No?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes",@"No", nil]; 
    [alert show]; 
} 

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    self.alertViewBlock(buttonIndex); 
} 
@end 
3

मैं कैसे करने के लिए (और क्यों) दृश्य, कार्रवाई चादरें और एनिमेशन सचेत करने के लिए ब्लॉक कॉलबैक जोड़ने के बारे में एक ब्लॉग पोस्ट में लिखा है आप GitHub से सूत्रों फ़ाइलों को डाउनलोड कर सकते हैं:

https://github.com/Innovattic/UIKit-Blocks

उपयोग:

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"My easy alert" 
               message:@"Would you like to perform some kind of action?" 
             cancelButtonTitle:@"No" 
             otherButtonTitles:@"Yes", nil]; 
[alert setHandler:^(UIAlertView* alert, NSInteger buttonIndex) { 
    NSLog(@"Perform some kind of action"); 
} forButtonAtIndex:[alert firstOtherButtonIndex]]; 
[alert show]; 
+0

को बहुत समय पहले जवाब मिला है, लेकिन फिर भी, यह अच्छा काम है – jAckOdE

0

UIAlertView आईओएस 8.0, से हटा दिया गया है एक बेहतर समाधान UIAlertController का उपयोग किया जाएगा: प्रतिनिधिमंडल के बिना

let alert = UIAlertController(title: "message", message: "Title", preferredStyle: .Alert) 

alert.addAction(UIAlertAction(title: "YES", style: .Default, handler: { (action) -> Void in 
    // Action for YES 
})) 
alert.addAction(UIAlertAction(title: "NO", style: .Default, handler: { (action) -> Void in 
    // Action for NO 
})) 

self.view.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil) 
संबंधित मुद्दे

 संबंधित मुद्दे