I'm trying to make a segue from a UIImagePickerController to another view controller.
I have tried a push segue, because I knew that UIImagePickerController was a subclass of UINavigationController, but it didn't work. The error is "Push segues can only be used when the source controller is managed by an instance of UINavigationController."
Now I try a modal segue, but when it occurs, the screen goes black.
Is there a link between the two problems, and how can I do please ?
You cannot perform segue directly from a UIImagePickerController.
Try to add a push segue from the view controller that instantiates the UIImagePickerController to the target VC.
Now implement UIImagePickerControllerDelegate method didFinishPickingMediaWithInfo: in your source VC and inside that method call self performSegueWithIdentifier: sender: with the segue identifier of the segue, you have created lately.
Of course, you need to set an identifier for the segue and add UINavigationControllerDelegate, UIImagePickerControllerDelegate protocol references in your source VC header interface.
Also you might need to implement prepareForSegue: method in your source VC if you need to pass some data (such as image information) to the target VC.
Related
I have the following scenario: I have a view controller and want to navigate programmatically to another view f.e. to give the user the ability to change settings.
So I have VC1 and VC_settings and a present modally segue between those controllers. I gave the segue an identifier and call it like this:
performSegue(withIdentifier: "SegueToSettingsView)
Ok, that works without any problems. The settings view is opened and I can navigate back to VC1.
Now I want to have the same functionality in another view controller VC2. I want to reuse the settings view but the problem is that the segue is already connected between VC1 and VC_settings and I can't connect a new one for VC2.
And if I try to call the existing segue with the coce above in VC2 then the app crashes.
In swift it is possible to create segues from multiple views two one viewcontroller. Here I have set it so there are segues from VC1 to Settings and VC2 to settings which should work.
Hope it helped
Since you have VC1 and VC_settings connected with segue named "SegueToSettingsView" also the perform selector is working fine.
As per your need make another segue from VC2 to VC_Settings and give a different name for it. then make use of below code to perform segue.
performSegue(withIdentifier: "SegueToSettingsViewFromVC2")
Steps to make segue:
Then select the connection and set identifier
I have implemented a transitiondelegate, and assigned it to the destination viewcontroller in the segue (at method viewDidLoad). My question is: How iOS knows to use the same delegate when peforming the segue from source to destination? I mean, I didn't assign the delegate at the source viewcontroller. Only assigned it at the destination, and still it is being called when performing the segue.
Who should have the delegate? The presentingViewController or the destinationViewController?
I would expected it to only be called when doing a segue the opposite way.
Thanks, and hope I was clear.
Before the transition, the destination view controller asks its transitioning delegate, for the animation controller.
You should set the transitioning delegate for the destination controller.
I have defined a segue from source tableview controller to another and trying to programmatically transition to the destination controller upon click of a UITableViewCell using the following statement:
[self performSegueWithIdentifier:#"pushToDestinationViewController" sender:self];
However, I find that the segue doesn't result in a "push" to the destination view controller.
On the other hand, if I wire the segue to a specific cell in the source view controller it works.
In addition, if I call the destination viewcontroller programmatically using:
[self showViewController:DestinationViewController] it works.
Can someone advise if this is a known issue in iOS 8 or if I am missing something here.
Did you have a segue in your Storyboard called pushToDestinationViewController? You will need to make sure that you have this segue in your Storyboard first like below.
Or, if you just want to simply push another view controller, you can just use [self.navigationController pushViewController:destinationViewController].
I have a storyboard segue with an identifier that is 'Push to ResumeView'.
I try calling it in the ViewController that I'm in at the point, by doing
[self performSegueWithIdentifier: #"Push to ResumeView" sender: self];.
But nothing happens?
I'd much rather just push the ViewController using the top NavigationController or something, but I can't see how to do that either.
Try implementing the shouldPerformSegueWithIdentifier:sender: or prepareForSegue:sender: methods in the 'from' view controller. Put a break point or NSLog() inside the method to inspect the segue identifier. This will prove that you indeed set up the segue correctly in the storyboard.
If you want to manually push your next view controller to the top of the navigation controller, use pushViewController:animated:. However, if you are using storyboard, the preferred way is to use segues.
Try this one.
UIViewController *yourResumeView=[self.storyboard instantiateViewControllerWithIdentifier:#"PushToResumeView"];
[self.navigationController pushViewController:yourResumeView animated:YES];
Using storyboards, what is the proper way to dismiss a modal?
using IBAction and writing code to dismiss after a button click?
using segue and notify the parent view controller after a button click?
See Here Dismissing a Presented View Controller about halfway down
When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it.
So you should use an IBAction and writing code to dismiss after a button click
According Alex Cio answer for Swift 3 and XCode 8.3:
Create class:
import UIKit
class DismissSegue: UIStoryboardSegue {
override func perform() {
self.source.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
But in storyboard you should choose:
Action Segue -> Custom -> dismiss
Only after this option appear on Action Segue menu
I've found that usually when I'm attempting to do this in storyboard I'd rather not create extra classes. It still makes sense to perform the dismiss from the presenting view controller, so that requires a class to back it.
If you create an IBAction in the presenting view controller and name it appropriately e.g.
- (IBAction)dismissAnyModel:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Then from storyboard wherever you want to trigger the dismiss from you create an action to the first responder as shown below. You can extend this to work with multiple presenting view controllers by creating unique names for the IBActions.
More information on first responder and the responder chain
See my answer here. It gives you two ways to dismiss the modal view controller with storyboard. I like method two described because one you add the class in your project your return from modal views can be done with no code using storyboard alone. That said, if you have implemented a delegate and delegate protocol, it is also a good place to put the dismissModalViewController statement.
To do this inside the UIStoryboard you need first to create an Object of the type UIStoryboardSegue in your project
Then insert following method inside the class. Here is my class
#implementation DismissController
- (void)perform{
UIViewController *sourceVC = self.sourceViewController;
[sourceVC.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
}
Now you can use it inside your UIStoryboard. Select the button that should make the UIViewController Disappear and drag it to the UIViewController you want to go to. In my case it shows **dismiss Controller* because of the name of my Class.
Select it and you are done!
There is also a very good explanation on this website.
As the Apple online documentation indicates, the presenting view controller is responsible for dismissing the modal (presented) view.
There's a post and example available
here