我有一个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;
}
}
问题是,我如何解释其中一个属性可能为 null 并在clickedButtonAtIndex:
? 索引值会发生变化。我有大约 4 或 5 个不同的按钮,每个按钮的显示取决于属性是否包含某种值。