在之前的開發中用到了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];
}