`UIActionSheet`子类,使用block和target/action调用,具有更简单的API。
以下是如何创建一个显示取消按钮、破坏按钮和其他两个按钮的动作表的两个示例。第一个是用`UIActionSheet`正常创建动作表的例子。第二个是等价的,但使用`MEActionSheet`。
使用`UIActionSheet`和代理
@property (nonatomic, strong) id someObject;
- (void)showActionSheet:(id)something {
self.someObject = something; // set property to access it later
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Button One", @"Button Two", nil];
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *clickedButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([clickedButtonTitle isEqualToString:@"Destructive Button"]) {
// do something when destructive button is tapped
} else if ([clickedButtonTitle isEqualToString:@"Button One"]) {
// do something when button one is tapped
} else if ([clickedButtonTitle isEqualToString:@"Button Two"]) {
// do something with self.someObject when button two is tapped
}
}
转换为使用`MEActionSheet`
- (void)showActionSheet:(id)something {
MEActionSheet *actionSheet = [[MEActionSheet alloc] initWithTitle:@"Title"];
// Example with block. This happens to be the destructive button.
[actionSheet setDestructiveButtonWithTitle:@"Destructive Button" onTapped:^{
// do something when "Destructive Button" is tapped
}];
// Example with target/action
[actionSheet addButtonWithTitle:@"Button One" target:self action:@selector(buttonOneTapped)];
// Example with target/action passing an object
[actionSheet addButtonWithTitle:@"Button Two" target:self action:@selector(buttonTwoTapped:) withObject:something];
// Add the cancel button to the end
[actionSheet addCancelButtonWithTitle:@"Cancel"];
[actionSheet showInView:self.view];
}
- (void)buttonOneTapped {
// do something when "Button One" is tapped
}
- (void)buttonTwoTapped:(id)someObject {
// do something with someObject when "Button Two" is tapped
}
版权(C)2012 Mike Enriquez
本许可证下,任何人免费获得此软件的副本及其关联文档文件(“软件”),在公司内部使用不需要付费。包括但不限于使用、复制、修改、合并、出版、分发、再许可和/或转售软件的副本,并允许被软件提供的人按照以下条件进行操作。
上述版权声明和本许可证声明应包含在软件的副本或主要部分中。
本软件按“原样”提供,不提供任何形式的质量保证,包括但不限于对适销性、特定目的适用性和非侵权的保证。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论其性质属于合同、侵权或其他,无论其源于、出于或与该软件或其使用或其他任何形式的对软件的处理有关。