iPhone/Objective-C UIActionSheet,带有以编程方式添加的按钮
我有一个 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 个不同的按钮,每个按钮的显示取决于属性是否包含某种值。
I have a UIActionSheet
that contains buttons that are programmatically added depending on whether a property contains a value or not.
This is the code I'm currently using:
- (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;
}
}
The problem is, how do I account for the fact that one of the properties may be null and handle that within the clickedButtonAtIndex:
? The index values will change. I have around 4 or 5 different buttons, each displayed depending on whether a property contains a value of some sort.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法可能是使用
UIActionSheet
的buttonTitleAtIndex:
方法来获取按钮标题并使用它来确定要执行的操作。或者,您可以使用类似于添加按钮的逻辑来弄清楚索引的确切含义。
The simplest way is probably to use the
buttonTitleAtIndex:
method ofUIActionSheet
to get the button title and use this to determine what action to take.Alternatively you could use logic similar to where you add the buttons to figure out exactly what the index means.