NSARRAY를 사용하여 기타 버전을 지정 하시겠습니까?
-
05-07-2019 - |
문제
UialertsHeet의 생성자는 다른 ButTontitles 매개 변수를 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];
이제 핵심 단계는 어떤 버튼이 취소 버튼인지 지정하는 것입니다.
alert.cancelButtonIndex = [titles count];
우리는하다 [titles count]
그리고 아닙니다 [titles count] - 1
취소 버튼을 버튼 목록에서 추가로 추가하기 때문에 titles
.
이제 파괴적 버튼 (예 : 빨간색 버튼)이 될 버튼 (일반적으로 [titles count] - 1
단추). 또한 취소 버튼을 마지막 버튼으로 유지하면 iOS가 다른 버튼과 취소 버튼 사이에 멋진 간격을 추가합니다.
이 모든 것은 iOS 2.0 호환이므로 즐기십시오.
다른 팁
UIACTION 시트를 초기화 할 때 버튼을 추가하는 대신 NSARRAY를 통과하는 루프를 사용하여 AddButtonWithTitle 메소드와 함께 추가하십시오.
UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: nil];
for( NSString *title in titles)
[alert addButtonWithTitle:title];
AddButtonWithTitle : 추가 된 버튼의 색인을 반환합니다. init 메소드에서 nil에 취소를 설정하고 추가 버튼을 추가 한 후 다음을 실행합니다.
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에 의해 답변되었습니다. 이것은 대부분 더 자세한 설명입니다. 또한 @ephraim과 @simon을 구축합니다)
그것은 밝혀졌다 마지막 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;