UI10_快捷菜单

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    [_window release];
    RootViewController *rootVC = [[RootViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
    self.window.rootViewController = naVC;
    [rootVC release];
    [naVC release];

    return YES;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.m

#import "RootViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;



}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        //  添加一个长按手势
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
        [cell addGestureRecognizer:longPress];
        [longPress release];
    }
    cell.textLabel.text = @"猞猁";
    return cell;
}
//  长按的触发方法
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    //  根据手势对象找视图
    UITableViewCell *cell = (UITableViewCell *)longPress.view;
    //  创建快捷菜单
    UIMenuController *menu = [UIMenuController sharedMenuController];
    //  给快捷菜单进行定位显示
    [menu setTargetRect:cell.frame inView:cell.superview];
    //  让快捷菜单显示出来
    [menu setMenuVisible:YES animated:YES];

    //  自定义的功能
    UIMenuItem *addItem = [[UIMenuItem alloc] initWithTitle:@"添加" action:@selector(addAction:)];
    UIMenuItem *flagItem = [[UIMenuItem alloc] initWithTitle:@"flag" action:@selector(flagAction:)];

    //  把功能添加到快捷菜单上
    [menu setMenuItems:@[addItem, flagItem]];
    [addItem release];
    [flagItem release];



}
//  自定义的item必须实现方法, 才能在菜单上显示
- (void)addAction:(id)sender {

}
- (void)flagAction:(id)sender {

}
//  这个方法必须实现, 如果不实现, 快捷菜单无法显示
- (BOOL)canBecomeFirstResponder {
    return YES;
}
//  只要重写系统的这些方法, 我们就可以在快捷菜单上显示
- (void)delete:(id)sender {
    NSLog(@"delete");
}
- (void)copy:(id)sender {
    NSLog(@"copy");
}
- (void)select:(id)sender {
    NSLog(@"select");
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值