[英]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 : 您必须执行以下步骤:
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
(非常重要!),否则不启用水龙头。 label
. 为Tap Gesture Recognizer定义一个动作,以处理label
内的Tap 。 To present the another ViewController
you can do one of the following two things : 要显示另一个ViewController
您可以执行以下两项操作之一:
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
,可以通过将UILabel
的userInteractionEnabled
属性设置为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.