美文网首页
iOS 开发中遇到的UIMenuController的一个小问题

iOS 开发中遇到的UIMenuController的一个小问题

作者: 镌写不一样的人生 | 来源:发表于2017-01-23 11:51 被阅读0次

在之前的开发中用到了UIMenuController这个类,发现一个小问题,记录下来,具体如下

UIMenuController *menu = [UIMenuController sharedMenuController];

menu.menuItems = @[

[[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copy:)],

[[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(delete:)]

];

此处注意,如果响应事件名字这向上边那样命名的话,menu 显示的时候会多出来两个按钮,具体原因不是很清楚,猜想是与其默认名字有所冲突,另外将初始化menultems 方法注释的话,在- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 方法内实现return (action == @selector(copy:) || action == @selector(delete:),menu会显示拷贝和删除两个按钮,所以突然发现这也是实现UIMenuController 的一种方法,不过title 就是系统的

下面是我的UIMenuController 实现的具体方法

if (!ISNULLSTR(self.model.user_id)) {//首页

if ([self.model.user_id longLongValue] == [LoginManager shareLoginManager].loginResponse.user_id) {//自己发的动态

menu.menuItems = @[

[[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(commentcopyed:)],

[[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(commentdelete:)]

];

}else

{

if (!ISNULLARRAY(self.model.commentArray)) {

TT_DynamicStateCommentModel *commentModel = self.model.commentArray[self.tag-1];

if ([commentModel.user_id longLongValue] == [LoginManager shareLoginManager].loginResponse.user_id) {//别人发表的动态,自己发表的评论

menu.menuItems = @[

[[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(commentcopyed:)],

[[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(commentdelete:)]

];

}else

{

menu.menuItems = @[[[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(commentcopyed:)],];

}

}

}

}

if (!ISNULLSTR(self.detailModel.user_id)) {//动态详情

if ([self.detailModel.user_id longLongValue] == [LoginManager shareLoginManager].loginResponse.user_id) {//自己发的动态

menu.menuItems = @[

[[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(commentcopyed:)],

[[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(commentdelete:)]

];

}else

{

if (!ISNULLARRAY(self.detailModel.commentArray)) {

TT_DynamicDetailCommentModel *commentModel = (TT_DynamicDetailCommentModel *)self.detailModel.commentArray[self.tag-1];

if ([commentModel.user_id longLongValue] == [LoginManager shareLoginManager].loginResponse.user_id) {//别人发表的动态,自己发表的评论

menu.menuItems = @[

[[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(commentcopyed:)],

[[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(commentdelete:)]

];

}else

{

menu.menuItems = @[[[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(commentcopyed:)],];

}

}

}

}

[self showMenuView:menu];

DLog(@"长按评论。。。。。");

}

}

}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender

{

return (action == @selector(commentcopyed:) || action == @selector(commentdelete:) || action == @selector(commentNamecopyed:));

}

- (void)showMenuView:(UIMenuController *)menu

{

[menu setTargetRect:self.frame inView:self.superview];

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(handleMenuWillShowNotification:)

name:UIMenuControllerWillShowMenuNotification

object:nil];

[menu setMenuVisible:YES animated:YES];

}

- (BOOL)canBecomeFirstResponse

{

return YES;

}

#pragma mark - Notifications

- (void)handleMenuWillHideNotification:(NSNotification *)notification {

self.backgroundColor = [UIColor clearColor];

[[NSNotificationCenter defaultCenter] removeObserver:self

name:UIMenuControllerWillHideMenuNotification

object:nil];

}

- (void)handleMenuWillShowNotification:(NSNotification *)notification {

self.backgroundColor = [UIColor clearColor];

[[NSNotificationCenter defaultCenter] removeObserver:self

name:UIMenuControllerWillShowMenuNotification

object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(handleMenuWillHideNotification:)

name:UIMenuControllerWillHideMenuNotification

object:nil];

}

相关文章

网友评论

      本文标题:iOS 开发中遇到的UIMenuController的一个小问题

      本文链接:https://www.haomeiwen.com/subject/wyrabttx.html