استخدام NSArray لتحديد otherButtonTitles?
-
05-07-2019 - |
سؤال
UIAlertSheet منشئ يأخذ otherButtonTitles المعلمة كما varg قائمة.أود أن يحدد الأخرى زر العناوين من NSArray بدلا من ذلك.هل هذا ممكن ؟
أييجب أن أفعل هذا:
id alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: button1Title, button2Title, nil];
ولكن منذ أنا توليد قائمة من الأزرار المتوفرة في وقت التشغيل, أنا حقا أريد شيئا من هذا القبيل:
id alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: otherButtonTitles];
الآن أنا أفكر أنني بحاجة إلى دعوة منفصلة initWithTitle:
1 البند البنود 2 و 3 بنود.مثل هذا:
if ( [titles count] == 1 ) {
alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: [titles objectAtIndex: 0], nil];
} else if ( [titles count] == 2) {
alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: [titles objectAtIndex: 0], [titles objectAtIndex: 1], nil];
} else {
// and so on
}
أن الكثير من التعليمات البرمجية مكررة ، ولكن في الواقع قد يكون من المعقول بما لدي في معظم ثلاثة أزرار.كيف يمكنني تجنب هذا ؟
المحلول
وهذا هو سنة من العمر ولكن الحل بسيط جدا ... تفعل كما اقترحSimon ولكن لم تحدد عنوان زر إلغاء، لذلك:
UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: nil];
ولكن بعد إضافة أزرار العادية، إضافة زر إلغاء، مثل:
for( NSString *title in titles) {
[alert addButtonWithTitle:title];
}
[alert addButtonWithTitle:cancelString];
والآن الخطوة الأساسية هي تحديد الزر الذي هو الزر، مثل إلغاء:
alert.cancelButtonIndex = [titles count];
ونحن نفعل [titles count]
وليس [titles count] - 1
لأننا نقوم بإضافة زر إلغاء كما اضافية من قائمة الأزرار في titles
.
ويمكنك الآن أيضا تحديد الزر الذي كنت تريد أن تكون على زر المدمرة (أي على الزر الأحمر) لتحديد destructiveButtonIndex (عادة والتي ستكون زر [titles count] - 1
). أيضا، إذا كنت حفاظ على زر إلغاء ليكون زر آخر، ودائرة الرقابة الداخلية أضيف أن تباعد لطيفة بين الأزرار الأخرى وزر إلغاء الأمر.
وكل هذه هي دائرة الرقابة الداخلية 2.0 متوافقة حتى ينعم.
نصائح أخرى
وبدلا من إضافة أزرار عند تهيئة UIActionSheet، حاول إضافة لهم طريقة addButtonWithTitle باستخدام لحلقة التي يمر NSArray الخاص بك.
UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: nil];
for( NSString *title in titles)
[alert addButtonWithTitle:title];
وaddButtonWithTitle: إرجاع مؤشر على الزر المضافة. تعيين cancelButtonTitle مقابل لا شيء في طريقة الحرف الأول وبعد إضافة أزرار إضافية تشغيل هذا:
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
- (void)showActionSheetWithButtons:(NSArray *)buttons withTitle:(NSString *)title {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: title
delegate: self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: nil];
for (NSString *title in buttons) {
[actionSheet addButtonWithTitle: title];
}
[actionSheet addButtonWithTitle: @"Cancel"];
[actionSheet setCancelButtonIndex: [buttons count]];
[actionSheet showInView:self.view];
}
ويمكنك إضافة زر إلغاء وضعه مثل هذا:
[actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle: @"Cancel"]];
أعرف أن هذا هو آخر العمر, ولكن في حالة شخص آخر مثلي ، هي محاولة لمعرفة ذلك.
(كان هذا الرد من قبل @kokemomuke.وهذا هو الغالب على شرح أكثر تفصيلا.أيضا بناء على @افرايم و @سيمون)
اتضح آخر دخول addButtonWithTitle:يجب أن يكون Cancel
زر.كنت استخدم:
// All titles EXCLUDING Cancel button
for( NSString *title in titles)
[sheet addButtonWithTitle:title];
// The next two line MUST be set correctly:
// 1. Cancel button must be added as the last entry
// 2. Index of the Cancel button must be set to the last entry
[sheet addButtonWithTitle:@"Cancel"];
sheet.cancelButtonIndex = titles.count - 1;