gpt4 book ai didi

objective-c - UIMenuController sharedMenuController - uicollectionview 的自定义菜单项不显示在 ios 7 中

转载 作者:太空狗 更新时间:2023-10-30 03:41:33 26 4
gpt4 key购买 nike

我正在使用 UIMenuItemUICollectionView 单元格长按中执行自定义操作。这与 iOS 6 完美配合,但现在我将我的应用程序转换为 iOS 7 和 Xcode 5,但它不起作用。自定义项未显示。

UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Unfavorite"
action:@selector(unFavorite:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];
[UIMenuController sharedMenuController].menuVisible = YES;

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender;
{
//do not show default itens like copy, paste....
[self becomeFirstResponder];
return NO;
}


- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
// The selector(s) should match your UIMenuItem selector
if (action == @selector(unFavorite:)) {
return YES;
}
return NO;
}

- (void)collectionView:(UICollectionView *)collectionView
performAction:(SEL)action
forItemAtIndexPath:(NSIndexPath *)indexPath
withSender:(id)sender {

}

- (BOOL)collectionView:(UICollectionView *)collectionView
shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {

myIndexPath = indexPath;
return YES;
}

最佳答案

我不知道 iOS 6,但在 iOS 7 中它非常简单。您只需要三个标准的 Collection View 委托(delegate)菜单处理方法,以及单元格子类中的一个操作方法。没有必要玩第一响应者或类似的东西。因此,例如(在此示例中,Copy 是标准项,但 Capital 是我添加到菜单中的内容):

// collection view delegate:

- (BOOL)collectionView:(UICollectionView *)collectionView
shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
UIMenuItem* mi = [[UIMenuItem alloc] initWithTitle:@"Capital"
action:NSSelectorFromString(@"capital:")];
[[UIMenuController sharedMenuController] setMenuItems:@[mi]];
return YES;
}

- (BOOL)collectionView:(UICollectionView *)collectionView
canPerformAction:(SEL)action
forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return (action == NSSelectorFromString(@"copy:") ||
action == NSSelectorFromString(@"capital:"));
}

- (void)collectionView:(UICollectionView *)collectionView
performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath
withSender:(id)sender {
// in real life, would do something here
NSString* state = (self.sectionData)[indexPath.section][indexPath.row];
if (action == NSSelectorFromString(@"copy:"))
NSLog(@"copying %@", state);
else if (action == NSSelectorFromString(@"capital:"))
NSLog(@"fetching the capital of %@", state);
}

// cell subclass:

-(void)capital:(id)sender {
// find my collection view
UIView* v = self;
do {
v = v.superview;
} while (![v isKindOfClass:[UICollectionView class]]);
UICollectionView* cv = (UICollectionView*) v;
// ask it what index path we are
NSIndexPath* ip = [cv indexPathForCell:self];
// talk to its delegate
if (cv.delegate &&
[cv.delegate respondsToSelector:
@selector(collectionView:performAction:forItemAtIndexPath:withSender:)])
[cv.delegate collectionView:cv performAction:_cmd
forItemAtIndexPath:ip withSender:sender];
}

关于objective-c - UIMenuController sharedMenuController - uicollectionview 的自定义菜单项不显示在 ios 7 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18906991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com