YCXMenuItem.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // YCXMenuItem.m
  3. // YCXMenuDemo_ObjC
  4. //
  5. // Created by 牛萌 on 15/5/6.
  6. // Copyright (c) 2015年 NiuMeng. All rights reserved.
  7. //
  8. #import "YCXMenuItem.h"
  9. @interface YCXMenuItem ()
  10. @property (nonatomic, assign) BOOL showSelected;
  11. @end
  12. @implementation YCXMenuItem
  13. @synthesize showSelected = _showSelected;
  14. #pragma mark - Public Methods
  15. + (instancetype)menuItem:(NSString *)title image:(UIImage *)image target:(id)target action:(SEL)action {
  16. YCXMenuItem *item = [[YCXMenuItem alloc] init:title image:image tag:0 userInfo:nil target:target action:action];
  17. item.alignment = NSTextAlignmentCenter;
  18. item.showSelected = (item.target != nil && item.action != NULL);
  19. return item;
  20. }
  21. + (instancetype)menuItme:(NSString *)title ImageUrl:(NSString *)imageUrl target:(id)target action:(SEL)action{
  22. YCXMenuItem *item = [[YCXMenuItem alloc] init:title image:nil tag:0 userInfo:nil target:target action:action];
  23. item.imageUrl = imageUrl;
  24. item.alignment = NSTextAlignmentCenter;
  25. item.showSelected = (item.target != nil && item.action != NULL);
  26. return item;
  27. }
  28. + (instancetype)menuItem:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag userInfo:(NSDictionary *)userInfo {
  29. YCXMenuItem *item = [[YCXMenuItem alloc] init:title image:image tag:tag userInfo:userInfo target:nil action:NULL];
  30. item.alignment = NSTextAlignmentCenter;
  31. item.showSelected = YES;
  32. return item;
  33. }
  34. + (instancetype)menuTitle:(NSString *)title WithIcon:(UIImage *)icon {
  35. YCXMenuItem *item = [[YCXMenuItem alloc] init:title image:icon tag:0 userInfo:nil target:nil action:NULL];
  36. item.showSelected = NO;
  37. item.alignment = NSTextAlignmentCenter;
  38. return item;
  39. }
  40. + (instancetype)menuItem:(NSString *)title image:(UIImage *)image target:(id)target action:(SEL)action userInfo:(NSDictionary *)userInfo
  41. {
  42. YCXMenuItem *item = [[YCXMenuItem alloc] init:title image:image tag:0 userInfo:userInfo target:target action:action];
  43. item.alignment = NSTextAlignmentCenter;
  44. item.showSelected = (item.target != nil && item.action != NULL);
  45. return item;
  46. }
  47. - (BOOL)enabled {
  48. return self.showSelected;
  49. }
  50. - (void)performAction {
  51. __strong id target = self.target;
  52. if (target && [target respondsToSelector:_action]) {
  53. [target performSelectorOnMainThread:_action withObject:self waitUntilDone:YES];
  54. }
  55. }
  56. #pragma mark - Private Methods
  57. /// init
  58. - (id)init:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag userInfo:(NSDictionary *)userInfo target:(id)target action:(SEL) action {
  59. NSParameterAssert(title.length || image);
  60. self = [super init];
  61. if (self) {
  62. _title = title;
  63. _image = image;
  64. _tag =tag;
  65. _userInfo = userInfo;
  66. _target = target;
  67. _action = action;
  68. }
  69. return self;
  70. }
  71. - (NSString *)description {
  72. return [NSString stringWithFormat:@"<%@ :%p> \n {\n\ttitle: %@,\n\timage: %@,\n\ttag: %zd,\n\tuserInfo: %@\n}\n", [self class], self, _title, _image, _tag, _userInfo];
  73. }
  74. @end