MEActionSheet 1.0.0

MEActionSheet 1.0.0

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2014年12月

Mike Enriquez维护。



  • 作者:
  • Mike Enriquez

`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
}

特性

  • 作为`UIActionSheet`的替代项。《MEActionSheet》是`UIActionSheet`的子类,并且原始API按预期工作。《UIActionSheetDelegate》也按预期工作。
  • 消除了在`actionSheet:clickedButtonAtIndex:`代理方法中找到的长if...else if...或switch语句。不再尝试匹配字符串或确定哪个按钮索引被点击。
  • 通过使用target/action实现更小且可重用的方法。
  • 简化了在每个视图控制器中使用多个动作表。无需为每个视图控制器中的所有动作表尝试处理的代理方法。
  • 简化了传递对象到按钮动作。你通常需要在视图控制器中存储一个局部实例并在代理方法中访问它。现在,你只需在block中使用该对象,或者通过`addButtonWithTitle:target:action:withObject`传递即可。请参见上面的例子,传递了"Button Two"的`something`对象。

坑点

  • 顺序很重要。添加的按钮将出现在靠近顶部的地方。
  • 不要忘了添加取消按钮。由于顺序很重要,你可能希望将它添加到最后,以便它出现在底部。
  • 破坏性按钮通常出现在顶部。这不是必须的,但最好先添加它。
  • 如果你仍然需要`UIActionSheetDelegate`委托,可以将它设置为构造函数的`initWithTitle:delegate:`或者通过`delegate`属性设置。
  • 不要混合使用`MEActionSheet`方法和`UIActionSheet`方法。坚持使用一种或另一种。

MIT许可证

版权(C)2012 Mike Enriquez

本许可证下,任何人免费获得此软件的副本及其关联文档文件(“软件”),在公司内部使用不需要付费。包括但不限于使用、复制、修改、合并、出版、分发、再许可和/或转售软件的副本,并允许被软件提供的人按照以下条件进行操作。

上述版权声明和本许可证声明应包含在软件的副本或主要部分中。

本软件按“原样”提供,不提供任何形式的质量保证,包括但不限于对适销性、特定目的适用性和非侵权的保证。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论其性质属于合同、侵权或其他,无论其源于、出于或与该软件或其使用或其他任何形式的对软件的处理有关。