就像花生杯一样,当它们融合在一起时,事情会变得更好。
无论是黄油果酱和巧克力,爵士和嘻哈,还是 ActionSheets 和 nibs...
是的,action sheets 和 nibs,它们就像这样简单
[[[FRActionSheet alloc] initWithNibNamed:@"My Nib"
handler:^(FRActionSheet *sheet, NSInteger idx){
//Handle business
}] showInView:[self view]];
这就完了!当按下 nib 中的任何按钮时,您的处理程序块将被调用,并会提供该按钮的标记以供参考。
如果您认为 nibs 只适合业余爱好者,那么也可以以传统方式使用 action sheet。
[[[FRActionSheet alloc] initWithButtonTitles:@[@"Button title A",@"Button title B"]
handler:^(FRActionSheet *sheet, NSInteger idx){
//Handle business
}] showInView:[self view]];
UI 自定义很糟糕。我认为比自定义 UIKit 更糟糕的是自定义 UIKit 替代组件。
您可以使用 decorator 属性为您的作用表分配一个代表对象,用它能设置按钮和标题视图的样式。
如果您想自定义按钮的内部布局,同时又不想创建 nib,您可以创建一个子类,并重载 layoutTitleLabel:buttons:inView: 方法。
这个类内部使用自动布局(因为它很棒),但在不支持自动布局的视图中也可以使用。
还可以创建一系列子类来创建不同类型的 action sheets
FRDateActionSheet 带有 UIDatePicker 的 action sheet。支持 UIDatePicker 的所有格式和月份选择器。
FRDateActionSheet *sheet;
//This will display a list of *Localized* month names, (January, February or Enero, Febrero)
sheet = [[FRDateActionSheet alloc] initWithTitle:
minimumDate:[NSDate distantPast]
maximumDate:[NSDate distantFuture]
datePickerMode:FRDateActionSheetMonthMode
handler:^(FRDateActionSheet *sheet, NSDate *date){
...
}];
[sheet showInView:aView];
FRPickerActionSheet 带有 UIPickerView 的 action sheet,拾取器的内容可以作为字典提供。
FRPickerActionSheet *sheet;
sheet = [[FRPickerActionSheet alloc] initWithTitle:@"Best cities in the world?"
handler:^(FRPickerActionSheet *sheet, NSUInteger idx){
NSString *optionKey;
optionKey = [[sheet sortedPickerOptionKeys] objectAtIndex:idx];
//The Value assocated with the chosen key
[[sheet pickerOptions] objectForKey:optionKey];
}];
[sheet showInView:aView];
//Your options can be loaded at anytime, before or after you present the sheet
// The format is DISPLAY STRING -> IDENTIFIER
[sheet setPickerOptions:@{
@"Paris":@1,
@"London":@2,
@"New York":@3
}];
//You can also have you picker options sorted
[sheet setSortDescriptors:@[
...
]];
FRNetworkedPickerActionSheet 与上面相同,但您可以使用一个 NSURLRequest提供 NSURLRequest (也就是说,它可以工作认证 API!) 和您要从 JSON 中提取条目的 keypath。真酷!请求使用 NSURLConnection 加载,而 JSON 解析则使用 NSJSONSerialization 并在主线程之外完成。
FRNetworkedPickerActionSheet *sheet;
/*
* Example JSON at amazingPlaces.com
* {"countries":[{"id":1,"name":"Colombia"},{"id":2,"name":"Hong Kong"}, ...]}
*/
sheet = [[FRNetworkedPickerActionSheet alloc] initWithTitle:@"Whats your favorite country?"
request:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://amazingPlaces.com/api/countries.json"]]
keyPath:@"countries"
mapping:^(id obj){
//Note: This block is not run on the main thread
return @{[obj valueForKeyPath:@"name"]:[obj valueForKeyPath:@"id"]}
}
handler:^(FRNetworkedPickerActionSheet *sheet, NSUInteger idx){
NSString *optionKey;
optionKey = [[sheet sortedPickerOptionKeys] objectAtIndex:idx];
//The Value assocated with the chosen key
[[sheet pickerOptions] objectForKey:optionKey];
}];
[sheet showInView:aView]
0.0.4 (03/04/2014)
0.0.3 (11/03/2014)
0.0.2 (05/03/2014)
0.0.1 (25/02/2014)