プロパティにUIActionSheet値が含まれているかどうかに応じてプログラムで追加されるボタンを含む があります。
これは私が現在使用しているコードです:
- (IBAction)infoButtonTap:(id)sender {
MyObject *obj = (MyObject *)[self.dataSource objectAtIndex:self.pageControl.currentPage];
if (obj != nil) {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Details"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Programatically add Other button titles if they exist.
if (obj.firstProperty) {
[actionSheet addButtonWithTitle:@"Dosomething"];
}
if (obj.secondProperty) {
[actionSheet addButtonWithTitle:@"Dosomethingelse"];
}
[actionSheet addButtonWithTitle:@"Static button"];
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
[actionSheet showInView:self.view.window];
[actionSheet release];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
MyObject *obj = (MyObject *)[self.dataSource objectAtIndex:self.pageControl.currentPage];
switch (buttonIndex) {
case 0: // Dosomething
break;
case 1: // Dosomethingelse
break;
case 2: // Static button
break;
default:
break;
}
}
問題は、プロパティの 1 つが null である可能性があるという事実をどのように説明し、それを .xml 内で処理するclickedButtonAtIndex:かです。指標値が変わります。プロパティに何らかの値が含まれているかどうかに応じて、それぞれが表示される約 4 つまたは 5 つの異なるボタンがあります。