UIMenuController基本使用

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.backgroundColor = [UIColor blueColor];
cell.textLabel.backgroundColor = [UIColor redColor];
cell.textLabel.text = [NSString stringWithFormat:@"indexpath = %zd",indexPath.row];

return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

UIMenuController *nemu = [UIMenuController sharedMenuController];
UIMenuItem *item1 = [[UIMenuItem alloc]initWithTitle:@"分享" action:@selector(share:)];
UIMenuItem *item2 = [[UIMenuItem alloc]initWithTitle:@"投诉" action:@selector(tousu:)];
nemu.menuItems = @[item1,item2];

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

//设置menu显示在哪儿
[nemu setTargetRect:cell.textLabel.frame inView:cell];

//设置item 显示
[nemu setMenuVisible:YES animated:YES];

}

-(void)share:(UIMenuController *)nemu{
NSLog(@"%s",func);
}

-(void)tousu:(UIMenuController *)nemu{
NSLog(@"%s",func);
}

/**

  • 告诉控制器能成为第一响应者
    */
    -(BOOL)canBecomeFirstResponder{
    return YES;
    }

/**

  • 告诉能控制器nemuitem能返回什么
    */
    -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    NSString *str = NSStringFromSelector(action);
    if ([str isEqualToString:@"share:"] || [str isEqualToString:@"tousu:"]) {
    return YES;
    }
    return NO;
    }

你可能感兴趣的:(UIMenuController基本使用)