UIAlertView
的子类,使用块和目标/操作调用提供更简单的 API。
以下是具有两个按钮(“取消”和“确定”)的警报视图的两个示例。第一个示例是您通常会使用 UIAlertView
的方式。第二个是等效的,但是使用 MEAlertView
。
使用 UIAlertView
和代理
- (void)showAlert {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"This is a message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *clickedButtonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([clickedButtonTitle isEqualToString:@"Ok"]) {
// do something when "Ok" is selected
}
}
转换为使用 MEAlertView
- (void)showAlert {
MEAlertView *alertView = [[MEAlertView alloc] initWithTitle:@"Alert!" message:@"This is a message"];
[alertView setCancelButtonTitle:@"Cancel"];
// Example with block. addOtherButtonWithTitle:target:action:withObject: and its variants are also available.
[alertView addOtherButtonWithTitle:@"Ok" onTapped:^{
// do something when "Ok" is selected
}];
[alertView show];
}
UIAlertView
的直接替代品。MEAlertView
是 UIAlertView
的子类,原始 API 工作如预期。同样,UIAlertViewDelegate
也一样工作。alertView:clickedButtonAtIndex:
中发现的冗长的 if...else if... 或 switch 语句。不再需要尝试匹配字符串或确定哪个按钮被点击。addButtonWithTitle:target:action:withObject
传递它。alertViewCancel:
代理,则系统会尝试它。它没有特殊样式或位置。UIAlertViewDelegate
代理,您可以在构造函数 initWithTitle:message:delegate:
中设置它。或者通过 delegate
属性设置它。MEAlertView
方法与 UIAlertView
方法。坚持使用一个或另一个。版权所有 (C) 2012 Mike Enriquez
本协议在此授予任何获得本软件及相关文档副本(统称“软件”)的个人免费使用该软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或将软件副本出售给他人的权利,以及允许软件提供者从事上述活动,前提是遵守以下条件:
上述版权声明和本授权声明应包含在软件的所有副本或实质部分的副本中。
软件按“现状”提供,不提供任何明确或隐含的担保,包括但不限于适销性、特定用途适用性和非侵权性。在任何情况下,无论是由于合同违约、侵权或其他原因导致的,作者或版权所有者均不对任何索赔、损害或其它责任承担责任,包括但不限于与软件或其他因其使用或其它方式处置软件而产生的索赔、损害或其它责任。