简体   繁体   English

如何通过点击表格视图单元格内的标签来显示另一个视图控制器

[英]How to present another view controller by tapping on a label inside a tableview cell

I want to create a segue to from an object (label) inside a tableview cell to another view controller. 我想创建一个segue从一个tableview单元内的对象(标签)到另一个视图控制器。

example: imagine the instagram feed tableview - tapping the number of likes under an image will push a second view with the list of people who liked an image. 例如:想象instagram feed的tableview-点击图片下的“喜欢”次数,将在第二个视图中显示喜欢图片的人的列表。

How can i do that? 我怎样才能做到这一点? I know it's possible when tapping a cell but i need a solution for when tapping a label inside the cell... 我知道点击单元格时有可能,但是我需要一种点击单元格内标签的解决方案...

thanks! 谢谢!

You have to make the following steps : 您必须执行以下步骤:

  1. Define userInteractionEnabled = true for the label using Interface Builder or in code, as you wish, (VERY IMPORTANT!!) without this the tap is not enabled. 根据需要,使用Interface Builder或在代码中为标签定义userInteractionEnabled = true (非常重要!),否则不启用水龙头。
  2. Define an action to the Tap Gesture Recognizer to handle the tap inside the label . Tap Gesture Recognizer定义一个动作,以处理label内的Tap
  3. To present the another ViewController you can do one of the following two things : 要显示另一个ViewController您可以执行以下两项操作之一:

    • Make a modal segue from the Tap Gesture Recognizer to the another ViewController using the Interface Builder or make an action for the Tap Gesture Recognizer and then present the another ViewController manually using the presentViewController function. 使从一个模态SEGUE Tap Gesture Recognizer到另一个ViewController使用界面生成器,或进行了轻击手势识别器的操作,然后呈现另一个ViewController使用手动presentViewController功能。

I thought the first is more easy to present the another ViewController , Is up to you. 我认为第一个比较容易呈现另一个ViewController ,由您决定。

I hope this help you. 希望对您有所帮助。

You can simply use a button and set its title instead of using a label. 您只需使用按钮并设置其标题即可,而不使用标签。 If you have to use a UILabel for some reason, then you can do so by setting its userInteractionEnabled property to true and then adding a UITapGestureRecognizer to it. 如果由于某种原因必须使用UILabel ,可以通过将UILabeluserInteractionEnabled属性设置为true ,然后向其中添加UITapGestureRecognizer来实现。

You can use UITapGestureRecognizer . 您可以使用UITapGestureRecognizer

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var lblTxt: UILabel!

override func viewDidLoad() {
        super.viewDidLoad()

        let recognizer = UITapGestureRecognizer(target: self, action:Selector("handleTap:"))
        recognizer.numberOfTapsRequired = 1;
        lblTxt.addGestureRecognizer(recognizer)
        lblTxt.userInteractionEnabled = true;
}

func handleTap(recognizer: UITapGestureRecognizer){
        println("tapped!")
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("secondView") as SecondViewController
        self.presentViewController(secondViewController, animated:true, completion:nil)
    }

暂无
暂无

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

相关问题 如何在表格视图单元格上按以向视图控制器显示导航控制器中的文本 - How to press on a tableview cell to present a view controller with the text in navigation controller 如何在点击按钮时将 Tableview 单元格内的文本字段中的数据打印到控制台 - How to Print data from textfield present inside Tableview cell to console on tapping button 如何在另一个View Controller中使用核心数据编辑Tableview单元格数据 - How To Edit Tableview Cell Data with Core Data In Another View Controller 通过点击TableView单元格将数据传递到详细信息视图 - Pass Data to Detail View by Tapping TableView Cell 如何在Swift中向另一个内部呈现视图控制器? - How do I present a view controller inside another in Swift? 在子视图控制器中点击单元格以访问嵌套环境中父视图的父视图控制器的tableview委托 - Tapping on cell in childviewcontroller accessing tableview delegate of parent's parent view controller in a nested environment 将TableView单元的Firebase数据检索到视图控制器的标签 - Retrieving firebase data of a tableview cell to a view controller's label 将单元格标签或标题传递给另一个视图控制器 - Passing cell label or title to another view controller 如何在标签栏控制器视图内的按钮按下时在标签栏下显示另一个视图控制器? - How to present another view controller under tab bar on button press inside a tab bar controller view? 当我单击表格视图内的复选框时,一个标签内的数量应添加到iOS中的另一个标签上 - when I clicked checkbox inside a tableview the amount present inside one label should add to another label in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM