ZZRadio.m 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // ZZRadio.m
  3. // ZZUIKit
  4. //
  5. // Created by Max on 2021/3/3.
  6. //
  7. #import "ZZRadio.h"
  8. #import <ZZUIKit/ZZCheckBox.h>
  9. @interface ZZRadio()
  10. @property(nonatomic,strong) NSMutableArray *itemArr;
  11. @property(nonatomic,copy) NSArray <NSString *> * labels;
  12. @end
  13. @implementation ZZRadio
  14. - (void)onInit{
  15. _itemArr = [NSMutableArray array];
  16. }
  17. FLEXSET(label){
  18. self.labels = [sValue componentsSeparatedByString:@"/"];
  19. }
  20. - (void)willMoveToWindow:(UIWindow *)newWindow{
  21. for (NSString *str in self.labels) {
  22. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveRadio:) name:ZZRadioNotification(str) object:nil];
  23. }
  24. }
  25. -(void)onClick{
  26. for (NSString *str in self.labels) {
  27. [[NSNotificationCenter defaultCenter] postNotificationName:ZZRadioNotification(str) object:nil userInfo:@{
  28. @"address":[NSString stringWithFormat:@"%p",self]
  29. }];
  30. }
  31. for (UIView *view in self.subviews) {
  32. if ([view respondsToSelector:@selector(selected)]) {
  33. if ([view isKindOfClass:NSClassFromString(@"ZZOption")] || [view isKindOfClass:NSClassFromString(@"ZZCheckBox")]) {
  34. if (![[view valueForKey:@"selected"] boolValue]) {
  35. continue;
  36. }
  37. self.selectStr = [[view valueForKey:@"titleLab"] valueForKey:@"text"];
  38. if (self.selectedBlock) {
  39. self.selectedBlock(self.selectStr);
  40. //退出循环
  41. return;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. -(void)receiveRadio:(NSNotification *)noti{
  48. //是自己发出的通知就return
  49. if ([noti.userInfo[@"address"] isEqualToString:[NSString stringWithFormat:@"%p",self]]) {
  50. return;
  51. }
  52. if (_itemArr.count == 0) {
  53. for (UIView *view in self.subviews) {
  54. if ([view respondsToSelector:@selector(setSelected:)]){
  55. [_itemArr addObject:view];
  56. }
  57. }
  58. }
  59. for (ZZCheckBox *box in _itemArr) {
  60. box.selected = false;
  61. self.selectStr = nil;
  62. }
  63. }
  64. -(void)dealloc{
  65. [[NSNotificationCenter defaultCenter] removeObserver:self];
  66. }
  67. @end