MenuButton.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // MenuButton.m
  3. // CommonLibrary
  4. //
  5. // Created by AlexiChen on 14-1-17.
  6. // Copyright (c) 2014年 CommonLibrary. All rights reserved.
  7. //
  8. #import "MenuButton.h"
  9. @interface MenuButton ()
  10. @property (nonatomic, copy) MenuAction action;
  11. @end
  12. @implementation MenuButton
  13. - (instancetype)initWithMenu:(MenuItem *)item
  14. {
  15. return [self initWithTitle:item.title icon:item.icon action:item.action];
  16. }
  17. - (instancetype)initWithTitle:(NSString *)title action:(MenuAction)action
  18. {
  19. return [self initWithTitle:title icon:nil action:action];
  20. }
  21. - (instancetype)initWithTitle:(NSString *)title icon:(UIImage *)icon action:(MenuAction)action
  22. {
  23. if (self = [super init])
  24. {
  25. self.title = title;
  26. self.icon = icon;
  27. self.action = action;
  28. [self setTitle:title forState:UIControlStateNormal];
  29. // self.titleLabel.font = [UIFont systemFontOfSize:16];
  30. [self setImage:icon forState:UIControlStateNormal];
  31. [self addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
  32. }
  33. return self;
  34. }
  35. - (instancetype)initWithBackground:(UIImage *)icon action:(MenuAction)action
  36. {
  37. if (self = [super init])
  38. {
  39. self.icon = icon;
  40. self.action = action;
  41. [self setBackgroundImage:icon forState:UIControlStateNormal];
  42. [self addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
  43. }
  44. return self;
  45. }
  46. - (void)setClickAction:(MenuAction)action
  47. {
  48. self.action = action;
  49. [self addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
  50. }
  51. - (void)onClick:(id)sender
  52. {
  53. if (_action) {
  54. _action(self);
  55. }
  56. }
  57. @end