本文介绍了Swift 2-单击“图像"以加载新的ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Xcode(Swift2)完全陌生.
I'm totally new to Xcode (Swift2).
我想要实现的是,当我单击main.storyboard
上的图像时,我想打开一个新的视图控制器或将其指向一个新的视图控制器.我该怎么办?
What I want to achieve is when I click on an image which is on my main.storyboard
, I want to open a new view controller or point it to a new view controller. How can I do this?
例如,我可以使用一个按钮来执行此操作,方法是在设计时拖动按钮(Ctrl +右键单击)并将其指向新的视图控制器,它的效果很好,但是我想在触摸/轻按时执行此操作图像在运行时.
For example, I can do this using a button by dragging the button (Ctrl+right click) and pointing it to the new view controller at design time and it works great but I want to do this at the touch/tap of the image at run time.
推荐答案
image.userInteractionEnabled = true
let tapRecog = UITapGestureRecognizer(target: self, action: "imgTap:")
image.addGestureRecognizer(tapRecog)
func imgTap(gestureRecognizer: UITapGestureRecognizer)
{
let tapImg = gestureRecognizer.view!
let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("NewViewController") as! NewViewController
secondViewController.DisplayImg = tapImg //NewViewController create DisplayImg UIImageView object
self.navigationController!.pushViewController(secondViewController, animated: true)
}
这篇关于Swift 2-单击“图像"以加载新的ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!