0

동적으로 단추를 사용자 시트에 추가하고 있습니다. "Cancel"단추를 추가하는 것으로 끝나지 만, setCancelButtonIndex 메서드를 사용하거나 수동으로 인덱스를 설정하면 "Cancel"이 표시되지 않습니다. setCancelButtonIndex 호출을 제외하면 취소가 표시됩니다 (물론 강조 표시없이). 제가 놓친 각주가 있습니까?사용자 인터페이스의 취소 버튼을 설정하면 마지막 항목이 사라집니다.

UIActionSheet *actionSheet; 
actionSheet = [[UIActionSheet alloc] initWithTitle:@"What do you want to do?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 
for (NSString *title in arrayButtons) { 
    [actionSheet addButtonWithTitle:title]; 
} 
[actionSheet addButtonWithTitle:@"Cancel"]; 
[actionSheet setCancelButtonIndex:[arrayButtons count]-1]; 
[actionSheet setDestructiveButtonIndex:0]; 
[actionSheet showInView:self.view]; 

답변

1

메서드 -addButtonWithTitle:은 단추 인덱스를 반환합니다. 그래서이 방법으로 수행해야합니다

UIActionSheet *actionSheet = [[UIActionSheet alloc] init]; 
[actionSheet setTitle:@"What do you want to do?"]; 
[actionSheet setDelegate:self]; 
for (NSString *title in arrayButtons) { 
    [actionSheet addButtonWithTitle:title]; 
} 
[actionSheet setCancelButtonIndex:[actionSheet addButtonWithTitle:@"Cancel"]];  
[actionSheet setDestructiveButtonIndex:0]; 
[actionSheet showInView:self.view]; 
+0

나는 약간 다르게 그것을 시도 : –

+0

actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle : @ "취소"]]; 하지만 성공하지 못했습니다. 방금 ​​정확한 코드를 시도했지만 동일한 문제가 있습니다. –

+0

@NelsonKo 방금 샘플 코드로 답을 편집했습니다. – leoformaggio

관련 문제