RASettingOptionModel.m 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // RASettingSelectionModel.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/9/12.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RASettingOptionModel.h"
  9. @implementation RASettingOption
  10. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  11. }
  12. @end
  13. @implementation RASettingOptionModel
  14. - (CGFloat)height {
  15. return 60.0f;
  16. }
  17. - (void)setOption:(RASettingOption *)option {
  18. _option = option;
  19. if (self.delegate && [self.delegate respondsToSelector:@selector(refreshUI)]) {
  20. [self.delegate refreshUI];
  21. }
  22. if (option && self.keyPath.length > 0) {
  23. [RASingleton.sharedInstance setValue:@(option.option) forKeyPath:self.keyPath];
  24. }
  25. }
  26. - (void)setKeyPath:(NSString *)keyPath {
  27. _keyPath = keyPath;
  28. if (self.options.count > 0 && _keyPath.length > 0) {
  29. [self.options enumerateObjectsUsingBlock:^(RASettingOption * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
  30. if (model.option == [[RASingleton.sharedInstance valueForKeyPath:self.keyPath] intValue]) {
  31. self.option = model;
  32. }
  33. }];
  34. }
  35. }
  36. - (void)setOptions:(NSArray<RASettingOption *> *)options {
  37. if (!options) {
  38. _options = nil;
  39. return;
  40. }
  41. NSMutableArray<RASettingOption *> *arr = [NSMutableArray array];
  42. for (NSDictionary *option in options) {
  43. RASettingOption *model = [RASettingOption new];
  44. [model setValuesForKeysWithDictionary:option];
  45. if (self.keyPath.length > 0) {
  46. if (model.option == [[RASingleton.sharedInstance valueForKeyPath:self.keyPath] intValue]) {
  47. self.option = model;
  48. }
  49. }
  50. [arr addObject:model];
  51. }
  52. _options = arr;
  53. }
  54. - (int)selectedIndex {
  55. if (self.options.count == 0 || self.option == nil || ![self.options containsObject:self.option]) {
  56. return -1;
  57. }
  58. return (int)[self.options indexOfObject:self.option];
  59. }
  60. - (void)setSelectedIndex:(int)selectedIndex {
  61. if (selectedIndex >= self.options.count) {
  62. return;
  63. }
  64. self.option = [self.options objectAtIndex:selectedIndex];
  65. }
  66. @end