UIMenuController用法

本文介绍了一个简单的iOS应用示例,使用Objective-C实现了带有长按手势触发的上下文菜单功能。通过UITableView展示数据,并为单元格添加了长按手势识别器来显示复制、转发、收藏和删除等选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface CellMenuController : UIViewController

@property(strong, nonatomic) NSMutableArray *dataSource;
@property(strong, nonatomic) UITableView *tableView;

@end

#import "CellMenuController.h"

@interface CellMenuController () <UITableViewDelegate, UITableViewDataSource>


@end

@implementation CellMenuController

- (id)init {
    if (self = [super init]) {
        self.dataSource = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *Identifity = NSStringFromClass([CellMenuController class]);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifity];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifity];
    }
    
    cell.textLabel.text = self.dataSource[indexPath.row];
    
    // 增加长按首饰
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
    [cell addGestureRecognizer:longPress];
    
    
    return cell;
}

- (void)longTap:(UILongPressGestureRecognizer *)longPressGestureRecognizer {
    if (longPressGestureRecognizer.state == UIGestureRecognizerStateBegan) {
        [self becomeFirstResponder];
        
        UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyMenuItem)];
        UIMenuItem *item1 = [[UIMenuItem alloc] initWithTitle:@"转发" action:@selector(repostMenuItem)];
        UIMenuItem *item2 = [[UIMenuItem alloc] initWithTitle:@"收藏" action:@selector(collectMenuItem)];
        UIMenuItem *item3 = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(deleteMenuItem)];
        
        UIMenuController *menuController = [UIMenuController sharedMenuController];
        menuController.menuItems = @[item, item1, item2, item3];
        [menuController setTargetRect:self.view.bounds inView:self.view];
        [menuController setMenuVisible:YES animated:YES];
    }
}
// 此方法如果不实现,菜单则显示不出来
- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void) copyMenuItem {
    NSLog(@"复制");
}

- (void) repostMenuItem {
      NSLog(@"转发");
}

- (void) collectMenuItem {
      NSLog(@"收藏");
}

- (void) deleteMenuItem {
      NSLog(@"删除");
}

@end


效果截屏:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风流 少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值