2009-08-11 14 views
9

मैं UIActionSheet subclassed गया है में बटन जोड़ने नहीं है, और -init विधि में, मैं व्यक्तिगत रूप से फोन करने के बाद बटन जोड़ने के लिए सुपर init (एक var_args पारित नहीं कर सकते हैं)।UIActionSheet addButtonWithTitle: सही क्रम

अभी, यह इस तरह दिखता है:

if (self = [super initWithTitle:title delegate:self cancelButtonTitle:cancel destructiveButtonTile:destroy otherButtonTitles:firstButton,nil]) { 
    if (firstButton) { 
    id buttonTitle; 
    va_list argList; 
    va_start(argList, firstButtton); 
    while (buttonTitle = va_arg(argList, id)) { 
     [self addButtonWithTitle:buttonTitle] 
    } 
    va_end(argList); 
    } 
} 
return self; 

हालांकि, इस मामले में मेरी विशिष्ट उपयोग नहीं विनाशकारी बटन, एक बटन को रद्द, और चार अन्य बटन होते हैं। जब यह पता चलता है, आदेश सभी बंद है, के रूप में

दिखा Button1
रद्द
Button2
Button3

की तरह वे बस सूची है, जो समझ में आता है के अंत में जोड़ा गया था; हालांकि, मैं ऐसा नहीं दिखाना चाहता हूं; तो मैं क्या करूं? क्या वास्तव में, UIActionSheet को सही तरीके से उपclass करने और यह काम करने का कोई तरीका है?

उत्तर

21

आप उन्हें अपने सही क्रम में जोड़ सकते हैं, और उसके बाद cancelButtonIndex और destructiveButtonIndex मैन्युअल रूप से सेट कर सकते हैं।

अपने कोड उदाहरण के लिए:

if (self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTile:nil otherButtonTitles:nil]) { 
    if (firstButton) { 
    id buttonTitle; 
    int idx = 0; 
    va_list argList; 
    va_start(argList, firstButtton); 
    while (buttonTitle = va_arg(argList, id)) { 
     [self addButtonWithTitle:buttonTitle] 
     idx++; 
    } 
    va_end(argList); 
    [self addButtonWithTitle:cancel]; 
    [self addButtonWithTitle:destroy]; 
    self.cancelButtonIndex = idx++; 
    self.destructiveButtonIndex = idx++; 
    } 
} 
return self; 
+1

आह, यह आसान बनाता है। मैंने सोचा कि वे केवल पढ़ने के लिए –

+4

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

8

Aviad बेन डोव का जवाब सही है, तथापि बटन सूचकांक काउंटर को नष्ट करने और रद्द सूचकांक सूचकांक सेट करने के लिए की जरूरत नहीं है है। addButtonWithTitle: विधि देता नव इस्तेमाल किया बटन के सूचकांक इसलिए हम तो जैसे अभी उस मान का उपयोग कर सकते हैं:

if (self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTile:nil otherButtonTitles:nil]) { 
    if (firstButton) { 
    id buttonTitle; 
    va_list argList; 
    va_start(argList, firstButtton); 
    while (buttonTitle = va_arg(argList, id)) { 
     [self addButtonWithTitle:buttonTitle] 
    } 
    va_end(argList); 
    self.cancelButtonIndex = [self addButtonWithTitle:cancel]; 
    self.destructiveButtonIndex = [self addButtonWithTitle:destroy]; 
    } 
} 
return self; 
+0

मुझे लगता है कि आपका विनाश बटन सही स्थान पर नहीं है। यह शीर्ष पर होना चाहिए। – lhunath

3

पहले जवाब के कारण विनाशकारी बटन नीचे है, जो के अनुसार नहीं है पर रखा जा करने के लिए एचआईजी, और जो उपयोगकर्ता के लिए भी बहुत भ्रमित है। विनाशकारी बटन शीर्ष पर होना चाहिए, नीचे रद्द करना, और बीच में अन्य।

निम्नलिखित आदेश उन्हें सही ढंग से:

sheetView   = [[UIActionSheet alloc] initWithTitle:title delegate:self 
             cancelButtonTitle:nil destructiveButtonTitle:destructiveTitle otherButtonTitles:firstOtherTitle, nil]; 
if (otherTitlesList) { 
    for (NSString *otherTitle; (otherTitle = va_arg(otherTitlesList, id));) 
     [sheetView addButtonWithTitle:otherTitle]; 
    va_end(otherTitlesList); 
} 
if (cancelTitle) 
    sheetView.cancelButtonIndex  = [sheetView addButtonWithTitle:cancelTitle]; 

एक कार्यान्वयन (एक ब्लॉक आधारित एपीआई के साथ एक UIActionSheet आवरण) के लिए https://github.com/Lyndir/Pearl/blob/master/Pearl-UIKit/PearlSheet.m देखें।

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