0

我是 Objective-C 的新程序员。

我在我的应用程序中使用故事板。它包含 UITableViewController。

当我单击它是使用 segue 的单元格时,转到下一个视图控制器。但我想使用-(void)onLongPress:(UILongPressGestureRecognizer*)pGesture 并使用相同的单元格显示另一个 ViewController。

我的 TableView 显示公司。我想根据单元格 LongClick 显示公司详细信息。

4

1 回答 1

1

您需要创建一个 UILongPressGestureRecognizer。

然后您需要将它附加到您希望识别 longPress 的视图上。当你附加它时,你定义了一个动作选择器和一个目标。动作选择器是一种在识别手势时将在目标中触发的方法。

假设您在 tableViewController 中创建手势识别器并且这也是目标,那么它看起来像这样

UILongPressGestureRecognizer* longPGR =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(onLongPress:)];
[self.relevantViewInTableViewCell addGestureRecognizer:longPGR];

然后你创建一个动作方法来拦截水龙头

-(void)onLongPress:(UILongPressGestureRecognizer*)pGesture
{
    //statement
}

如果您使用动态单元格创建表格,则 longPGR 创建应该在您创建单元格时进行。

如果您有静态单元格,您可以使 IBOutlet @properties 连接到相关单元格,并在您的 longPGR 创建中使用该属性。

要显示另一个 viewController,不需要使用 segue。您可以在 longPress 方法中将新的 viewController 推送到 NavigationController 的堆栈中:

[self.navigationController pushViewController:newViewController];

这与使用 segue 具有相同的效果。

于 2013-01-11T05:40:50.910 回答