NNPickerController 是一个 UI 库。应用程序用户可以通过此 UI 选择元素之一。
要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
。
在您的 UIViewController 中,您需要编写相应的代码:
NSArray *elementsArray ... // Tha element will be picked by PickerController
// Allocate and Initialize
NNPickerController *picker = [[NNPickerController alloc] init];
// Picker's cancel button was clicked handler;
picker.cancelHandler = ^(NNPickerController *picker) {
[picker dismissPickerController];
};
// Element was picked handler
picker.finishPickingHandler = ^(NNPickerController *picker, UITableView *tableView,NSIndexPath *indexPath){
[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.elementLabel.text = elementsArray[indexPath.row];
[picker dismissPickerController];
};
// Configuration for Picker Controller
[picker setNumberOfSection:^NSInteger{
return 1;
} withNumberOfRow:^NSInteger(NSInteger section) {
return elementsArray.count;
} withCellForRowAtIndexPath:^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
NSString *cellIdentifier = [NSString stringWithFormat:@"cell%@/%@", @(indexPath.row),@(indexPath.section)];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell != nil) {
return cell;
}
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.textLabel.text = elementsArray[indexPath.row];
if (indexPath.row == 0) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}];
// show picker controller
[picker showPickerControllerForViewController];
numa08, [email protected]
NNPickerController 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。