此组件的目的是提高在原生UIAlertView中使用时的可读性。UIKit警报视图与代理协同工作。当你有两个或多个警报在同一个控制器中时,它会成为一个问题,因为您不得不使用标签来识别哪个UIAlertView发出了用户的操作。
使用STAlertView,您将能够在声明警报视图的同一位置定义'确定'和'取消'按钮的行为。那么,让我们看看一些代码示例
[[STAlertView alloc] initWithTitle:@"Title"
message:@"Message"
cancelButtonTitle:@"Cancel"
otherButtonTitle:@"Ok"
cancelButtonBlock:^{
NSLog(@"do something at cancel");
} otherButtonBlock:^{
NSLog(@"do something at ok");
}];
就是这样,没有片段代码到处都是,只有几行,所有相关的代码都在一起。由于这是一个原生的UIAlertView,所以使用STAlertView的结果就像原生一样。
此组件是在StackOverflow的答案的启发下制作的,该答案由Ricky Helgesson提供。
要运行示例项目,首先克隆仓库,然后从示例目录运行pod install
。
与ARC和非ARC兼容。
然后在要显示警报视图的视图控制器中添加到.h文件中
#import <STAlertView/STAlertView.h>
...
@property (nonatomic, strong) STAlertView *alertView;
...
@end
并在.m文件中
...
self.alertView = [[STAlertView alloc] initWithTitle:@"Title of the alert"
message:@"Message you want to show"
cancelButtonTitle:@"No"
otherButtonTitle:@"Yes"
cancelButtonBlock:^{
// Code todo when the user cancel
...
} otherButtonBlock:^{
// Code todo when the user accept
...
}];
...
您可以使用警报视图的引用对UIAlertView进行任何自定义。例如
self.stAlertView = [[STAlertView alloc] initWithTitle:@"Alert view with a textfield"
message:@"Native UIAlertView with a textfiled."
textFieldHint:@"Write something"
textFieldValue:nil
cancelButtonTitle:@"Cancel"
otherButtonTitle:@"Store"
cancelButtonBlock:^{
...
} otherButtonBlock:^(NSString * result){
...
}];
//You can make any customization to the native UIAlertView
self.stAlertView.alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[[self.stAlertView.alertView textFieldAtIndex:1] setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[self.stAlertView show];
STAlertView可在MIT许可下使用。有关更多信息,请参阅LICENSE文件。
通过gittip支持本项目。