UILongPressGestureRecognizer在视图控制器之间切换

时间:2014-11-11 14:41:10

标签: ios objective-c uitableview uigesturerecognizer

当我从视图控制器切换到另一个不显示的视图控制器时,我已UILongPressGestureRecognizeruimenucontroller单元格上显示uitableview。为什么?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    cell = [tableView dequeueReusableCellWithIdentifier:@"CellNotificationTableViewCell" forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [cell addGestureRecognizer:recognizer];
    [self longPress:recognizer];
return cell;
}

- (void)longPress:(UILongPressGestureRecognizer *)recognizer {
 UITableViewCell *cell2 = (UITableViewCell *)recognizer.view;
    if (recognizer.state == UIGestureRecognizerStateBegan) {
       // UITableViewCell *cell = (UITableViewCell *)recognizer.view;

        UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(DeleteMsg:)];
        UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Read" action:@selector(ReadMsg:)];
        UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"UnRead" action:@selector(UnReadMsg:)];

        UIMenuController *menu = [UIMenuController sharedMenuController];
        [menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];
        [menu setTargetRect:cell2.frame inView:cell2.superview];
        [menu setMenuVisible:YES animated:YES];
    }
    selectedIndexPath = [self.tableView indexPathForCell:cell2];

    NSLog(@"%ld",(long)selectedIndexPath.row);
    NSUserDefaults *nssave = [NSUserDefaults standardUserDefaults];
    [nssave setInteger:selectedIndexPath.row forKey:@"selectedcellrow"];

}

1 个答案:

答案 0 :(得分:0)

我不完全确定您的要求,但是当您设置手势识别器时,您将目标设置为自我(即当前视图控制器)。因此,如果您更改视图控制器,则目标不再存在。