2011-10-15 11 views
7

मेरे पास विकल्पों के साथ एक एक्शन शीट है जो परिस्थितियों के आधार पर भिन्न होती है। पर्याप्त बटन शीर्षक हैं जो मैं पहले उन बटन शीर्षकों की एक सरणी बनाना चाहता हूं, लेकिन मैं यह नहीं समझ सकता कि इसे varargs प्रारूप में कैसे परिवर्तित किया जाए। अब जाहिर हैमैं एक UIActionSheet varargs init विधि में तारों की एक सरणी कैसे भेज सकता हूं?

NSMutableArray *buttonTitles = [NSMutableArray array]; 
if (condition1) { 
    [buttonTitles addObject: @"Do action 1"]; 
} 
if (condition2) { 
    [buttonTitles addObject: @"Do action 2"]; 
} 
if (condition3) { 
    [buttonTitles addObject: @"Do action 3"]; 
} 
if (condition4) { 
    [buttonTitles addObject: @"Do action 4"]; 
} 
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: buttonTitles] autorelease]; 

अगर मैं था मैं इस बजाय की तरह कुछ कर सकता करने के लिए:

मैं इस तरह कुछ करना चाहता हूँ

UIActionSheet *actionSheet = nil; 
if (condition1 && condition2 && condition3 && condition4) { 
    actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", @"Do action 4", nil] autorelease];  
} else if (condition1 && condition2 && condition3 && !condition4) { 
    actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", nil] autorelease];  
} 
// else ... 
// about 14 other cases! 

लेकिन उस भयानक होगा। किसी को भी मेरी मदद करने के लिए कुछ अच्छी वाक्य रचनात्मक चीनी पता है?

संपादित करें: यह सुझाव दिया गया है कि मैं addButtonWithTitle है, जो इसे चेहरे पर बहुत अच्छा लगता है का उपयोग, दुर्भाग्य से यह इस अतिरिक्त बटन को रद्द बटन, जो वांछनीय नहीं है के बाद डालता है।

मेरा मानना ​​है कि इस addButtonWithTitle राज्यों पर अपनी दस्तावेज़ीकरण के बाद से एप्पल के कोड के साथ बग है:

// adds a button with the title. returns the index (0 based) of where it was added. buttons are displayed in the order added except for the 
// destructive and cancel button which will be positioned based on HI requirements. buttons cannot be customized. 

HI आवश्यकताओं (एप्पल के स्वयं के ह्यूमन इंटरफ़ेस दिशा-निर्देश) के पक्ष में अन्य सभी विकल्पों को नीचे दिए गए बटन को रद्द, तो मैं एप्पल कहेंगे बँधा हुआ। बेशक, यह वास्तव में मेरी मदद नहीं करता है, इसलिए मैं एक एनएसएआरएआरई और एक वैरगास के बीच रूपांतरित करने की कोशिश करने के लिए वापस आ गया हूं, जिसे मैं अभी भी नहीं जानता कि कैसे करना है।

for (Nstring *button in buttonTitles) 
    [actionSheet addButtonWithTitle:button]; 

उत्तर

18

आप इस

NSMutableArray *buttonTitles = [NSMutableArray array]; 
if (condition1) { 
    [buttonTitles addObject: @"Do action 1"]; 
} 
if (condition2) { 
    [buttonTitles addObject: @"Do action 2"]; 
} 
if (condition3) { 
    [buttonTitles addObject: @"Do action 3"]; 
} 
if (condition4) { 
    [buttonTitles addObject: @"Do action 4"]; 
} 
[buttonTitles addObject: @"Cancel"]; 
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: nil destructiveButtonTitle: nil otherButtonTitles: nil] autorelease]; 

for (NSString *title in buttonTitles) { 
    [actionSheet addButtonWithTitle: title]; 
} 

[actionSheet setCancelButtonIndex: [buttonTitles count] - 1]; 
+0

मैंने सोचा कि यह काम करता है, लेकिन मुझे इसके साथ कुछ समस्याएं मिलीं, संपादन देखें। –

+0

कृपया संपादन की जांच करें। –

+0

यह काम किया। हालांकि किसी भी अतिरिक्त खिताब के साथ गड़बड़ करने की कोई ज़रूरत नहीं थी। –

4

क्यों सिर्फ ऐसा नहीं कुछ इस तरह की कोशिश कर सकते बटन इंडेक्स के बजाय बटन शीर्षक के खिलाफ:

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kSMSButtonText]) { 
    //share via sms 
    }else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kEmailButtonText]) { 
    //share via mail 
    } 
} 

मुझे लगता है कि लोगों की याद आती है कि महत्वपूर्ण विवरण, अन्य सभी बटनों के बाद इसे जोड़ने और सेटकैंकबटन इंडेक्स को कॉल करके रद्द बटन के रूप को निर्दिष्ट करने के बजाय initWithTitle चयनकर्ता में रद्द करना है।

+0

मैंने सोचा कि यह काम करता है, लेकिन मुझे इसके साथ कुछ समस्याएं मिलीं, संपादन देखें। –

5

हिमांशु के जवाब पर एक बदलाव:

UIActionSheet * as = [[UIActionSheet alloc] initWithTitle:@"Share" 
               delegate:self 
             cancelButtonTitle:nil /* don't set Cancel title here! */ 
            destructiveButtonTitle:nil 
             otherButtonTitles:nil]; 
int cancelButtonIndex = 0; // will remain 0 if no other buttons besides "Cancel" 
if (canShareBySMS) 
{ 
    [as addButtonWithTitle:kSMSButtonText]; 
    cancelButtonIndex++; 
} 
if (canShareByMail) 
{ 
    [as addButtonWithTitle:kEmailButtonText]; 
    cancelButtonIndex++; 
} 
/* etc. */ 

// after all other buttons have been added, include Cancel 
[as addButtonWithTitle:@"Cancel"]; 
[as setCancelButtonIndex:cancelButtonIndex]; 

[as showInView:self.sharingViewController.view]; 
[as release]; 

नोट: यदि आपका UIActionSheetDelegate विधि की जाँच करनी चाहिए

0

यहां एक विधि है जिसे मैंने कोशिश की है जो ठीक काम करने लगता है। यह काम करता है अगर आप "रद्द करें" बटन से पहले बटन के अंत में एक अतिरिक्त बटन जोड़ना चाहते हैं।

NSString * optionalButton = nil; 
if (condition4) { 
    optionalButton = @"Some Additional Option"; 
} 

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"Some Title" 
                  delegate:self 
               cancelButtonTitle:@"Cancel" 
              destructiveButtonTitle:nil 
               otherButtonTitles:@"Button 1",@"Button 2",@"Button 3",optionalButton, nil]; 


[actionSheet showInView:self.view]; 

यह बटन सूची के अंत में एक अतिरिक्त 'शून्य' को जोड़ने के लिए है, तो अपनी हालत (condition4) पूरा नहीं होता है ठीक प्रतीत होता है।

मैंने सफलतापूर्वक आईओएस 7 पर इसका परीक्षण किया है।

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