简体   繁体   English

如何在Objective-C中使用Segue表视图控制器单元格单击和单元格长按

[英]how to use segue table view controller cell click and cell long click in Objective-C

I am a new programmer to Objective-C. 我是Objective-C的新程序员。

I use a storyboard in my app. 我在应用中使用情节提要。 It contains UITableViewController. 它包含UITableViewController。

When I click it is cell using segue go to next view controller. 当我单击它是使用segue的单元格时,转到下一个视图控制器。 But I want to use -(void)onLongPress:(UILongPressGestureRecognizer*) pGesture and show another ViewController by using the same cell. 但是我想使用-(void)onLongPress:(UILongPressGestureRecognizer*) pGesture并通过使用相同的单元格显示另一个ViewController。

My TableView shows Companies. 我的TableView显示了公司。 I want to show company details according to cell LongClick. 我想根据单元格LongClick显示公司详细信息。

You need to create a UILongPressGestureRecognizer. 您需要创建一个UILongPressGestureRecognizer。

Then you need to attach it to the view that you wish to recognise the longPress. 然后,您需要将其附加到您想要识别longPress的视图上。 When you attach it you define an action selector and a target. 附加它时,您将定义一个动作选择器和一个目标。 The action selector is a method that will be trigged in the target when the gesture is recognised. 动作选择器是一种方法,可在识别手势时在目标中触发。

Assuming you create the gesture recogniser in your tableViewController and that is also the target then it would look something like this 假设您在tableViewController中创建了手势识别器,并且它也是目标,那么它将看起来像这样

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

then you create an action method to intercept the taps 然后创建一个动作方法来拦截水龙头

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

If you are creating your table with dynamic cells, your longPGR creation should take place when you create the cell. 如果要使用动态单元格创建表,则在创建单元格时应进行longPGR创建。

If you have static cells, you can make IBOutlet @properties connected to the cells concerned, and use that property in your longPGR creation. 如果您有静态单元格,则可以使IBOutlet @properties连接到有关的单元格,并在longPGR创建中使用该属性。

To show the other viewController, it is not necessary to use a segue. 要显示另一个viewController,没有必要使用segue。 You can push the new viewController onto the NavigationController's stack in the longPress method: 您可以使用longPress方法将新的viewController推送到NavigationController的堆栈上:

[self.navigationController pushViewController:newViewController];

That has the same effect as using a segue. 与使用segue具有相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Objective-C 中单击 UILabel 上的超链接打开视图 controller - How to open a view controller on click of hyperlink on UILabel in Objective-C Segue 不继续 Table Cell 单击 - Segue not going on Table Cell click 如何在Objective-C中使用自定义单元格 - how to use custom cell in objective-c 突出显示长按的表格视图单元格,并使用Objective-C在iOS中取消突出显示 - Highlight long pressed table view cell and unhighlight in iOS using objective-C iOS使用Segue导航到表单元格单击上的新视图 - iOS Navigating to New View on Table Cell Click using a Segue 表单元未出现在表视图objective-C中 - table cell not appear in table view objective-C 选项卡和导航视图控制器中的Table单元格的Segue - Segue of Table cell in a tab and navtigation view controller 突出显示表格视图单元格不会使用Objective-C突出显示表格视图单元格的子视图 - Highlighting table view cell will not highlight subviews of table view cell using objective-C Objective-C:单击第一个单元格时,会出现一个问题,即选择了最后一个单元格 - objective-c : When you click on the first cell, an issue occurs in which the last cell is selected 在Objective-C中单击视图中的按钮后,如何按下标签栏控制器? - How can I push tab bar controller after click a button from a view in Objective-C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM