美文网首页
UIMenuController基本使用

UIMenuController基本使用

作者: iOS小王子 | 来源:发表于2015-12-13 09:19 被阅读151次

    -(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基本使用

          本文链接:https://www.haomeiwen.com/subject/gmzyhttx.html