在之前的開發(fā)中用到了UIMenuController這個類莹菱,發(fā)現(xiàn)一個小問題吠卷,記錄下來蒜危,具體如下
UIMenuController *menu = [UIMenuController sharedMenuController];
menu.menuItems = @[
[[UIMenuItem alloc] initWithTitle:@"復(fù)制" action:@selector(copy:)],
[[UIMenuItem alloc] initWithTitle:@"刪除" action:@selector(delete:)]
];
此處注意她君,如果響應(yīng)事件名字這向上邊那樣命名的話域仇,menu 顯示的時候會多出來兩個按鈕,具體原因不是很清楚王暗,猜想是與其默認(rèn)名字有所沖突悔据,另外將初始化menultems 方法注釋的話,在- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 方法內(nèi)實現(xiàn)return (action == @selector(copy:) || action == @selector(delete:)俗壹,menu會顯示拷貝和刪除兩個按鈕科汗,所以突然發(fā)現(xiàn)這也是實現(xiàn)UIMenuController 的一種方法,不過title 就是系統(tǒng)的
下面是我的UIMenuController 實現(xiàn)的具體方法
if (!ISNULLSTR(self.model.user_id)) {//首頁
if ([self.model.user_id longLongValue] == [LoginManager shareLoginManager].loginResponse.user_id) {//自己發(fā)的動態(tài)
menu.menuItems = @[
[[UIMenuItem alloc] initWithTitle:@"復(fù)制" 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) {//別人發(fā)表的動態(tài)绷雏,自己發(fā)表的評論
menu.menuItems = @[
[[UIMenuItem alloc] initWithTitle:@"復(fù)制" action:@selector(commentcopyed:)],
[[UIMenuItem alloc] initWithTitle:@"刪除" action:@selector(commentdelete:)]
];
}else
{
menu.menuItems = @[[[UIMenuItem alloc] initWithTitle:@"復(fù)制" action:@selector(commentcopyed:)],];
}
}
}
}
if (!ISNULLSTR(self.detailModel.user_id)) {//動態(tài)詳情
if ([self.detailModel.user_id longLongValue] == [LoginManager shareLoginManager].loginResponse.user_id) {//自己發(fā)的動態(tài)
menu.menuItems = @[
[[UIMenuItem alloc] initWithTitle:@"復(fù)制" 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) {//別人發(fā)表的動態(tài)头滔,自己發(fā)表的評論
menu.menuItems = @[
[[UIMenuItem alloc] initWithTitle:@"復(fù)制" action:@selector(commentcopyed:)],
[[UIMenuItem alloc] initWithTitle:@"刪除" action:@selector(commentdelete:)]
];
}else
{
menu.menuItems = @[[[UIMenuItem alloc] initWithTitle:@"復(fù)制" 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];
}