development

varlist가 아닌 배열을 전달하여 UIActionSheet 'otherButtons'를 작성하십시오.

big-blog 2020. 7. 30. 09:29
반응형

varlist가 아닌 배열을 전달하여 UIActionSheet 'otherButtons'를 작성하십시오.


UIActionSheet의 버튼 제목에 사용하려는 문자열 배열이 있습니다. 불행히도, 메소드 호출의 otherButtonTitles : 인수는 배열이 아닌 가변 길이 문자열 목록을 사용합니다.

그렇다면이 타이틀을 UIActionSheet에 어떻게 전달할 수 있습니까? 내가 본 해결책은 nil을 otherButtonTitles :에 전달한 다음 addButtonWithTitle :을 사용하여 버튼 제목을 개별적으로 지정하는 것입니다. 그러나 이것은 "취소"버튼을 마지막이 아닌 UIActionSheet의 첫 번째 위치로 옮기는 문제가 있습니다. 나는 그것이 마지막이되기를 원합니다.

1) 가변 문자열 목록 대신 배열을 전달하거나 2) 취소 단추를 UIActionSheet의 맨 아래로 이동하는 방법이 있습니까?

감사.


나는 이것을 작동시켰다. (단순한 버튼으로 확인하고 다음에 추가하면된다.

NSArray *array = @[@"1st Button",@"2nd Button",@"3rd Button",@"4th Button"];

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title Here"
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    // ObjC Fast Enumeration
    for (NSString *title in array) {
        [actionSheet addButtonWithTitle:title];
    }

    actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];

    [actionSheet showInView:self.view];

한 가지 작은 참고 사항 : [actionSheet addButtonWithTitle :]은 해당 버튼의 인덱스를 반환하므로 안전하고 "깨끗한"작업을 수행 할 수 있습니다.

actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];

Jaba와 Nick의 답변을 받아 조금 더 확장했습니다. 이 솔루션에 파괴 버튼을 포함 시키려면 :

// Create action sheet
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];
// Action Buttons
for (NSString *actionName in actionNames){
    [actionSheet addButtonWithTitle: actionName];
}

// Destruction Button
if (destructiveName.length > 0){
    [actionSheet setDestructiveButtonIndex:[actionSheet addButtonWithTitle: destructiveName]];
}

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

// Present Action Sheet
[actionSheet showInView: self.view];

응답에 대한 신속한 버전이 있습니다.

//array with button titles
private var values = ["Value 1", "Value 2", "Value 3"]

//create action sheet
let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil)
//for each value in array
for value in values{
    //add a button
    actionSheet.addButtonWithTitle(value as String)
}
//display action sheet
actionSheet.showInView(self.view)

값을 선택하려면 ViewController에 대리자를 추가하십시오.

class MyViewController: UIViewController, UIActionSheetDelegate

그리고 "clickedButtonAtIndex"메소드를 구현하십시오.

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
    let selectedValue : String = values[buttonIndex]
}

참고 URL : https://stackoverflow.com/questions/2384044/create-uiactionsheet-otherbuttons-by-passing-in-array-not-varlist

반응형