| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //
- // YCXMenuItem.m
- // YCXMenuDemo_ObjC
- //
- // Created by 牛萌 on 15/5/6.
- // Copyright (c) 2015年 NiuMeng. All rights reserved.
- //
- #import "YCXMenuItem.h"
- @interface YCXMenuItem ()
- @property (nonatomic, assign) BOOL showSelected;
- @end
- @implementation YCXMenuItem
- @synthesize showSelected = _showSelected;
- #pragma mark - Public Methods
- + (instancetype)menuItem:(NSString *)title image:(UIImage *)image target:(id)target action:(SEL)action {
- YCXMenuItem *item = [[YCXMenuItem alloc] init:title image:image tag:0 userInfo:nil target:target action:action];
- item.alignment = NSTextAlignmentCenter;
- item.showSelected = (item.target != nil && item.action != NULL);
- return item;
- }
- + (instancetype)menuItme:(NSString *)title ImageUrl:(NSString *)imageUrl target:(id)target action:(SEL)action{
- YCXMenuItem *item = [[YCXMenuItem alloc] init:title image:nil tag:0 userInfo:nil target:target action:action];
- item.imageUrl = imageUrl;
- item.alignment = NSTextAlignmentCenter;
- item.showSelected = (item.target != nil && item.action != NULL);
- return item;
- }
- + (instancetype)menuItem:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag userInfo:(NSDictionary *)userInfo {
- YCXMenuItem *item = [[YCXMenuItem alloc] init:title image:image tag:tag userInfo:userInfo target:nil action:NULL];
- item.alignment = NSTextAlignmentCenter;
- item.showSelected = YES;
- return item;
- }
- + (instancetype)menuTitle:(NSString *)title WithIcon:(UIImage *)icon {
- YCXMenuItem *item = [[YCXMenuItem alloc] init:title image:icon tag:0 userInfo:nil target:nil action:NULL];
- item.showSelected = NO;
- item.alignment = NSTextAlignmentCenter;
- return item;
- }
- + (instancetype)menuItem:(NSString *)title image:(UIImage *)image target:(id)target action:(SEL)action userInfo:(NSDictionary *)userInfo
- {
- YCXMenuItem *item = [[YCXMenuItem alloc] init:title image:image tag:0 userInfo:userInfo target:target action:action];
- item.alignment = NSTextAlignmentCenter;
- item.showSelected = (item.target != nil && item.action != NULL);
- return item;
- }
- - (BOOL)enabled {
- return self.showSelected;
- }
- - (void)performAction {
- __strong id target = self.target;
- if (target && [target respondsToSelector:_action]) {
- [target performSelectorOnMainThread:_action withObject:self waitUntilDone:YES];
- }
- }
- #pragma mark - Private Methods
- /// init
- - (id)init:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag userInfo:(NSDictionary *)userInfo target:(id)target action:(SEL) action {
- NSParameterAssert(title.length || image);
- self = [super init];
- if (self) {
- _title = title;
- _image = image;
- _tag =tag;
- _userInfo = userInfo;
- _target = target;
- _action = action;
- }
- return self;
- }
- - (NSString *)description {
- return [NSString stringWithFormat:@"<%@ :%p> \n {\n\ttitle: %@,\n\timage: %@,\n\ttag: %zd,\n\tuserInfo: %@\n}\n", [self class], self, _title, _image, _tag, _userInfo];
- }
- @end
|