iOS 開發(fā)中遇到的UIMenuController的一個(gè)小問題

在之前的開發(fā)中用到了UIMenuController這個(gè)類,發(fā)現(xiàn)一個(gè)小問題,記錄下來,具體如下

UIMenuController *menu = [UIMenuController sharedMenuController];

menu.menuItems = @[

[[UIMenuItem alloc] initWithTitle:@"復(fù)制" action:@selector(copy:)],

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

];

此處注意,如果響應(yīng)事件名字這向上邊那樣命名的話,menu 顯示的時(shí)候會(huì)多出來兩個(gè)按鈕,具體原因不是很清楚,猜想是與其默認(rèn)名字有所沖突,另外將初始化menultems 方法注釋的話,在- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 方法內(nèi)實(shí)現(xiàn)return (action == @selector(copy:) || action == @selector(delete:),menu會(huì)顯示拷貝和刪除兩個(gè)按鈕,所以突然發(fā)現(xiàn)這也是實(shí)現(xiàn)UIMenuController 的一種方法,不過title 就是系統(tǒng)的

下面是我的UIMenuController 實(shí)現(xiàn)的具體方法

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

if ([self.model.user_id longLongValue] == [LoginManager shareLoginManager].loginResponse.user_id) {//自己發(fā)的動(dòng)態(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ā)表的動(dòng)態(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)) {//動(dòng)態(tài)詳情

if ([self.detailModel.user_id longLongValue] == [LoginManager shareLoginManager].loginResponse.user_id) {//自己發(fā)的動(dòng)態(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ā)表的動(dòng)態(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];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容