programing

NSArray를 사용하여 otherButtonTitles를 지정 하시겠습니까?

nasanasas 2021. 1. 10. 17:25
반응형

NSArray를 사용하여 otherButtonTitles를 지정 하시겠습니까?


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
}

중복 코드가 많지만 버튼이 최대 3 개이므로 실제로 합리적 일 수 있습니다. 이것을 어떻게 피할 수 있습니까?


이것은 1 년이 지났지 만 솔루션은 매우 간단합니다 ... @ 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];

Now the key step is to specify which button is the cancel button, like:

alert.cancelButtonIndex = [titles count];

We do [titles count] and not [titles count] - 1 because we are adding the cancel button as extra from the list of buttons in titles.

You now also specify which button you want to be the destructive button (ie the red button) by specifying the destructiveButtonIndex (typically that will be the [titles count] - 1 button). Also, if you keep the cancel button to be the last button, iOS will add that nice spacing between the other buttons and the cancel button.

All of these is iOS 2.0 compatible so enjoy.


Instead of adding the buttons when you initialize the UIActionSheet, try adding them with the addButtonWithTitle method using a for loop that goes through your NSArray.

UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                              delegate: self
                              cancelButtonTitle: cancelString
                              destructiveButtonTitle: nil
                              otherButtonTitles: nil];

for( NSString *title in titles)  
    [alert addButtonWithTitle:title]; 

addButtonWithTitle: returns the index of the added button. Set cancelButtonTitle to nil in the init method and after adding additional buttons run this:

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];
}

You can add the cancel button and set it like this:

[actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle: @"Cancel"]];

I know this is an old post, but in case someone else, like me, is trying to figure this out.

(This WAS answered by @kokemomuke. This is mostly a more detailed explanation. Also building on @Ephraim and @Simon)

It turns out the LAST entry of addButtonWithTitle: needs to be the Cancel button. I'd use:

// 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;

ReferenceURL : https://stackoverflow.com/questions/1602214/use-nsarray-to-specify-otherbuttontitles

반응형