How can I segue to a view controller by touching a view embedded inside my view controller's view? It's easy to do this with a UIButton but not a UIView. Can anyone help please?
Add a UITapGestureRecognizer to the subview then performSegue in the selector method.
subview.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleViewTap)))
Then:
#objc func handleViewTap() {
performSegue(withIdentifier: "identifier", sender: self)
}
Related
I'm new to IOS and learning the IDE at the moment. I was wondering if its possible to link an imageView, when clicked, to a View Controller
Sure. First, in viewDidLoad of your UIViewController make it tappable:
imageView.isUserInteractionEnabled = true
Then assign the gesture recognizer:
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.imageTap)))
Once the action is triggered, perform a transition to a new VC:
// Func in your UIViewController
#objc func imageTap() {
// present modally
self.present(YourNewViewController())
// or push to the navigation stack
self.navigationController?.push(YourNewViewController())
// or perform segue if you use storyboards
self.preformSegue(...)
}
Yes. It is possible. You'll need to add a tap gesture recogniser to image view and in the function perform segue.
SWIFT 4
let singleTap = UITapGestureRecognizer(target: self,action:Selector(“imageTapped”))
yourImageView.isUserInteractionEnabled = true
yourImageView.addGestureRecognizer(singleTap)
Function to handle Tap:
#objc func imageTapped() {
performSegue(withIdentifier: "yourNextScreen", sender: "")
}
Yes. You can bind a tap gesture recognizer to your imageview. You can follow this link here on a previous answer on how to do it programmatically
You could do it completely in the interface builder by putting a UIButton, with a transparent background and no text, over the ImageView and then control drag the button to the viewcontroller you want to trigger a segue on touch.
Of course it is. You should add a UITapGestureRecognizer to the UIImageView and add a selector to trigger the pushing a new viewcontroller method.
For example;
#IBOutlet weak var imageView: UIImageView! {
didSet {
let imageTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
imageView.addGestureRecognizer(imageTapGestureRecognizer)
imageView.isUserInteractionEnabled = true
}
}
func imageTapped() {
//navigate to another view controller
}
add gesture and if you want to animate it then see this pod
I have two view controllers in my storyboard: view1 and view2.
In view1, I used the storyboard to make a segue from its tableview to view2.
But now in view2, I have programmatically created a button, and it is supposed to segue to another instantiated view2.
When the button is clicked, I made a function to execute:
func buttonPressed(sender: UIButton){
//TODO:- GET NEXT PAGE
self.performSegueWithIdentifier("anotherView2", sender: sender)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "anotherView2"){
let anotherView2Controller = segue.destinationViewController as! View2
...
...
}
}
Of course it does not work, since there is no segue identifier called anotherView2. So I want to make it somehow, but I really cannot find good way to manage this.
Is there anyway to create the segue programmatically? I tried to make the segue from storyboard, but since the button is created programatically, I cannot segue to view2 to view2.
You want to use presentViewController instead of performSegue:
// however you want to initialize the new vc
let vc2 : View2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("view2.id") as! View2
an alternate way to init the vc is to do this:
let vc2 = View2()
and then set the data and present:
vc2.index = index // whatever you want to pass
self.presentViewController(vc2, animated: true, completion: nil)
Otherwise you could create a new view controller in your storyboard, but I wouldn't recommend it.
I have a UIButton in a prototype UItableviewcell that is made in a nib.
The UITableView is inside a UIViewController.
The requirement is: When I press the UIButton, a segue should be performed from Main UIViewController to another second UIViewController . I can access the UIButton IBOutlet from the table , so is there any way to call a function through that UIButton Variable.
// for reference
let x= cell.followButton;
Add a target to the button to call a method in your view controller. In this code, self is your view controller.
cell.followButton.addTarget(self, action: #selector(buttonPressed), forControlEvents: .TouchUpInside)
Then implement buttonPressed to handle segueing.
func buttonPressed() {
performSegueWithIdentifier("Identifier", sender: self)
}
I am working with a library from github which requires a table view to be within a view controller.
Like this: HidingNavigationBarManager(viewController: self, scrollView: tableView)
But my table view is not inside my VC, instead I have a container view with an embedded segue to the table view.
So how can I pass my embedded table view thats inside my container view in the function:
HidingNavigationBarManager(viewController: self, scrollView: tableView)
If a ViewController has containerViews in it, it triggers prepareForSegue Method in a ViewController that contains containerViews after viewDidLoad. There you can get reference of viewController which is embedded in a containerView.
So for example you have a containerView which is linked with viewcontroller of class TestViewController:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var vc: AnyObject = segue.destinationViewController
if vc .isKindOfClass(TestViewController) {
NSLog("GOTCHA!")
}
}
So you can refer to the tableView in a TestViewController like forexample: vc.tableView inside the if block of prepareForSegue method.
I have a navigation controller, with a table view. When I press a cell, the detail view controller opens.
In my root view controller I have :
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "detailview" {
var destination:DetailViewController = segue.destinationViewController as DetailViewController
destination.delegate=self
}
}
In my detail view controller I have a back button :
#IBAction func back() {
self.navigationController?.popViewControllerAnimated(true)
}
The issue is, after 2 go to and return, my app crashes when I go back on the root view controller pressing back button. The console doesn't give me errors. It just crashes.
I think I have forgotten to unwind the segue.
So in my detail view controller I added :
#IBAction func unwindToViewController(segue: UIStoryboardSegue) {
println("unwind function")
}
I connect this function to my back button with "exit" in my storyboard.
When I run my app, If I press on the back button, the console doesn't display my print "unwind function", so unwindToViewController isn't called. Why ?
And my app still crashes...
Your unwindToViewController method should be placed in your root viewController, then ctrl-drag from the button in the detailViewController to the Exit icon in InterfaceBuilder. Choose that method in the popup menu.
Another approach would be to declare a protocol with a function in the rootViewController that is called from the detailViewController. You already set the rootViewController as the delegate of the detailViewController. Within that function you call dismissViewController.
Swift answer...
I had a similar problem.
The func: "segueForUnwindingToViewController(toViewController: UIViewController, fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue" was never called.
Solution:
since I didn't have a "UINavigationController", because I simply embeded the app in a Navigation Controller, I created a UINavigationController subclass for the Navigation Controller and added the function named above on it. Now the app calls "segueForUnwindingToViewController"