diff --git a/QBPopupMenu/QBPopupMenuItem.h b/QBPopupMenu/QBPopupMenuItem.h index b159c7e..91afa6e 100644 --- a/QBPopupMenu/QBPopupMenuItem.h +++ b/QBPopupMenu/QBPopupMenuItem.h @@ -12,6 +12,7 @@ @property (nonatomic, weak, readonly) id target; @property (nonatomic, assign, readonly) SEL action; +@property (nonatomic, copy, readonly) void(^actionBlock)(); @property (nonatomic, copy, readonly) NSString *title; @property (nonatomic, copy, readonly) UIImage *image; @@ -24,4 +25,13 @@ - (instancetype)initWithImage:(UIImage *)image target:(id)target action:(SEL)action; - (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image target:(id)target action:(SEL)action; ++ (instancetype)itemWithTitle:(NSString *)title actionBlcok: (void(^)())action; ++ (instancetype)itemWithImage:(UIImage *)image actionBlock: (void(^)())action; ++ (instancetype)itemWithTitle:(NSString *)title image:(UIImage *)image actionBlcok: (void(^)())action; + +- (instancetype)initWithTitle:(NSString *)title actionBlock: (void(^)())action; +- (instancetype)initWithImage:(UIImage *)image actionBlock: (void(^)())action; +- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image actionBlcok: (void(^)())action; + + @end diff --git a/QBPopupMenu/QBPopupMenuItem.m b/QBPopupMenu/QBPopupMenuItem.m index ecf98eb..aa768da 100644 --- a/QBPopupMenu/QBPopupMenuItem.m +++ b/QBPopupMenu/QBPopupMenuItem.m @@ -12,6 +12,7 @@ @interface QBPopupMenuItem () @property (nonatomic, weak, readwrite) id target; @property (nonatomic, assign, readwrite) SEL action; +@property (nonatomic, copy, readwrite) void(^actionBlock)(); @property (nonatomic, copy, readwrite) NSString *title; @property (nonatomic, copy, readwrite) UIImage *image; @@ -60,4 +61,38 @@ - (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image target:(i return self; } + ++ (instancetype)itemWithTitle:(NSString *)title actionBlcok: (void(^)())action +{ + return [[self alloc] initWithTitle:title actionBlock:action]; +} ++ (instancetype)itemWithImage:(UIImage *)image actionBlock: (void(^)())action +{ + return [[self alloc] initWithImage:image actionBlock:action]; +} ++ (instancetype)itemWithTitle:(NSString *)title image:(UIImage *)image actionBlcok: (void(^)())action +{ + return [[self alloc] initWithTitle:title image:image actionBlcok:action]; +} + +- (instancetype)initWithTitle:(NSString *)title actionBlock: (void(^)())action +{ + return [self initWithTitle:title image:nil actionBlcok:action]; +} +- (instancetype)initWithImage:(UIImage *)image actionBlock: (void(^)())action +{ + return [self initWithTitle:nil image:image actionBlcok:action]; +} +- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image actionBlcok: (void(^)())action +{ + self = [super init]; + if (self) { + self.title = title; + self.image = image; + self.actionBlock = action; + } + return self; +} + + @end diff --git a/QBPopupMenu/QBPopupMenuItemView.m b/QBPopupMenu/QBPopupMenuItemView.m index fe26bf2..fe25231 100644 --- a/QBPopupMenu/QBPopupMenuItemView.m +++ b/QBPopupMenu/QBPopupMenuItemView.m @@ -95,6 +95,9 @@ - (void)setItem:(QBPopupMenuItem *)item - (void)performAction { + if (self.item.actionBlock) { + self.item.actionBlock(); + } if (self.item.target && self.item.action) { [self.item.target performSelector:self.item.action withObject:nil afterDelay:0]; } diff --git a/QBPopupMenuDemo/QBPopupMenuDemo/Base.lproj/Main.storyboard b/QBPopupMenuDemo/QBPopupMenuDemo/Base.lproj/Main.storyboard index dec4ac3..1a05f0b 100644 --- a/QBPopupMenuDemo/QBPopupMenuDemo/Base.lproj/Main.storyboard +++ b/QBPopupMenuDemo/QBPopupMenuDemo/Base.lproj/Main.storyboard @@ -1,7 +1,9 @@ - + - + + + @@ -18,38 +20,64 @@ - - + - + - - - + + + + + + + + + + + + @@ -67,4 +95,4 @@ - \ No newline at end of file + diff --git a/QBPopupMenuDemo/QBPopupMenuDemo/QBPopupMenuDemo-Info.plist b/QBPopupMenuDemo/QBPopupMenuDemo/QBPopupMenuDemo-Info.plist index 6c6187f..ef694cf 100644 --- a/QBPopupMenuDemo/QBPopupMenuDemo/QBPopupMenuDemo-Info.plist +++ b/QBPopupMenuDemo/QBPopupMenuDemo/QBPopupMenuDemo-Info.plist @@ -33,8 +33,6 @@ UISupportedInterfaceOrientations UIInterfaceOrientationPortrait - UIInterfaceOrientationLandscapeLeft - UIInterfaceOrientationLandscapeRight diff --git a/QBPopupMenuDemo/QBPopupMenuDemo/ViewController.m b/QBPopupMenuDemo/QBPopupMenuDemo/ViewController.m index d60866a..99443ac 100644 --- a/QBPopupMenuDemo/QBPopupMenuDemo/ViewController.m +++ b/QBPopupMenuDemo/QBPopupMenuDemo/ViewController.m @@ -16,6 +16,9 @@ @interface ViewController () @property (nonatomic, strong) QBPopupMenu *popupMenu; @property (nonatomic, strong) QBPlasticPopupMenu *plasticPopupMenu; +@property (nonatomic, strong) QBPopupMenu *popupMenuBlcok; +@property (nonatomic, strong) QBPlasticPopupMenu *plasticPopupMenuBlcok; + @end @implementation ViewController @@ -39,6 +42,37 @@ - (void)viewDidLoad QBPlasticPopupMenu *plasticPopupMenu = [[QBPlasticPopupMenu alloc] initWithItems:items]; plasticPopupMenu.height = 40; self.plasticPopupMenu = plasticPopupMenu; + + + QBPopupMenuItem *bItem = [QBPopupMenuItem itemWithTitle:@"Hello" actionBlcok:^{ + NSLog(@"Hello"); + }]; + QBPopupMenuItem *bItem2 = [QBPopupMenuItem itemWithTitle:@"Cut" actionBlcok:^{ + NSLog(@"Cut"); + }]; + QBPopupMenuItem *bItem3 = [QBPopupMenuItem itemWithTitle:@"Copy" actionBlcok:^{ + NSLog(@"Copy"); + }]; + QBPopupMenuItem *bItem4 = [QBPopupMenuItem itemWithTitle:@"Delete" actionBlcok:^{ + NSLog(@"Delete"); + }]; + QBPopupMenuItem *bItem5 = [QBPopupMenuItem itemWithImage:[UIImage imageNamed:@"clip"] actionBlock:^{ + NSLog(@"Clip Image"); + }]; + QBPopupMenuItem *bItem6 = [QBPopupMenuItem itemWithTitle:@"Delete" image:[UIImage imageNamed:@"trash"] actionBlcok:^{ + NSLog(@"Trash Image"); + }]; + NSArray *bItems = @[bItem, bItem2, bItem3, bItem4, bItem5, bItem6]; + + QBPopupMenu *popupMenuBlcok = [[QBPopupMenu alloc] initWithItems:bItems]; + popupMenuBlcok.highlightedColor = [[UIColor colorWithRed:0 green:0.478 blue:1.0 alpha:1.0] colorWithAlphaComponent:0.8]; + self.popupMenuBlcok = popupMenuBlcok; + + QBPlasticPopupMenu *plasticPopupMenuBlcok = [[QBPlasticPopupMenu alloc] initWithItems:bItems]; + plasticPopupMenuBlcok.height = 40; + self.plasticPopupMenuBlcok = plasticPopupMenuBlcok; + + } @@ -53,6 +87,15 @@ - (IBAction)showPlasticPopupMenu:(id)sender UIButton *button = (UIButton *)sender; [self.plasticPopupMenu showInView:self.view targetRect:button.frame animated:YES]; } +- (IBAction)showPopupMenuBlock:(id)sender { + UIButton *button = (UIButton *)sender; + [self.popupMenuBlcok showInView:self.view targetRect:button.frame animated:YES]; +} +- (IBAction)showPlasticPopupMenuBlcok:(id)sender { + UIButton *button = (UIButton *)sender; + [self.plasticPopupMenuBlcok showInView:self.view targetRect:button.frame animated:YES]; + +} - (void)action {