prepareForSegue not sending parameters when called, resulting in a crash - ios

I have two segues going out of my initial view controller. The push segue is connected to the cell and the modal is connected to the + UIBarButtonItem.
When this is what my prepareForSegue code looks like, everything is fine and filePath is sent through the push segue, but the filePath isn't passed through the modal segue.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showTimes"]) {
[[segue destinationViewController] setFilePath:filePath];
}
// if ([[segue identifier] isEqualToString:#"addEvent"]) {
// [[segue destinationViewController] setFilePath:filePath];
// }
}
If I uncomment the second if statement and segue identifier, the app crashes. filePath is sent nowhere. I've tried moving [[segue destinationViewController] setFilePath:filePath]; out of both if statements in an attempt to ensure that that line was executed, but that also crashes my app.
Both segues have the correct identifiers set up in the Attributes. However, I'm not sure that Xcode is actually realizing that the correct identifiers are set up because when I change the segue types, those changes aren't reflected the next time I run the app.
I'm a loss for what to do. I'm relatively new to Objective-C.

The addEvent segue points to a navigation controller, not your view controller. Instead, get your view controller and set the file path on that:
[[[segue destinationViewController] topViewController] setFilePath:filePath];

Related

Programmatic segue not working

How do you make a programmatic segue?
I tried this:
[self performSegueWithIdentifier:#"next" sender:self];
but it only gives me this console message:
Snapshotting a view that has not been rendered results in an empty snapshot.
Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
What I want to do is Have a button which triggers a image picker to appear. When the user has selected an image, a segue to the next view controller is performed.
I don't have enough reputation to add a comment but if you are attempting to present UIImagePickerController I would advice you to read this post
You could prepare the segue before showing it (Supposing you already connected the segue in the Storyboard and named it)(You wait for the execution block of the picker to complete before performing the Segue).
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"next"]) {
NextViewController *vc = [segue destinationViewController];
// Call anything from the vc
}
}
Hope you get some clues.

Passing data to destination view controller with performSegue

So I know that if there is a segue which is setup and activated through the storyboard, one can use the prepareForSegue method to pass data to the destination view controller.
I'm wondering if there is a way to do this if one uses the performSegueWithIdentifier method? It seems the prepareForSegue method is only called if the segue is activated with the storyboard, not with performSegue.
One particular problem is that I can't access the UIStoryboardSegue (it is not an argument in performSegue as it is in prepareForSegue), and therefore can't access the destination VC either.
Answer is here in StackOverflow.
How to pass prepareForSegue: an object
// When any of buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"MySegue" sender:sender];
}
// This will get called too before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MySegue"]) {
// Get destination view
SecondView *vc = [segue destinationViewController];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
// Pass the information to your destination view
[vc setSelectedButton:tagIndex];
}
}

how to get identifier of current segue

I have a bug in a rendering of a view but only when the user hits the back button in a UINavigationController. I am thinking about just supressing the offending code via something like this:
if(currentSuge.identifier isEqualToString:#'pushItemSegue'){
doCode // only in this situation
}
How would I get the name of the identifier of the current segue?
This is not possible. You cannot simply get the "current Segue" at any point in the app. Because Segues Exist only at specific points in time during your Apps Life cycle. That being when your app is preparing for the transition from one view controller to another and also during the actual visual transition.
The best you can do is override -prepareForSegue: and store the UIStoryboardSegue somewhere in your ViewController as a property.
more info: https://developer.apple.com/library/ios/documentation/uikit/reference/UIStoryboardSegue_Class/Reference/Reference.html
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//Here you have name of your segue
NSLog(#"Segue name is %#", [segue identifier]);
//Use this to performe some action
if ([[segue identifier] isEqualToString:#"pushItemSegue"])
{
//your code
}
//Add this for next seque
if ([[segue identifier] isEqualToString:#"secondSegue"])
{
//someting else
}
}

performSegue causes Warning Attempt to present while a presentation is in progress

Hello I have a button that must present a modal view controller when tapped
Here is the action:
- (IBAction)addNewLevelAction:(id)sender
{
[self performSegueWithIdentifier:kNewLevelConfigureSegue sender:self];
}
in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:kNewLevelConfigureSegue]) {
PSLevelConfigViewController *dest = (PSLevelConfigViewController *)[segue destinationViewController];
dest.delegate = self;
}
}
However when I tapp it I get:
Warning: Attempt to present <PSLevelConfigViewController: 0x98cbd00> on <UINavigationController: 0x98aac70> while a presentation is in progress!
Why is this? There is no other presentation there...
If you perform the segue programmatically, the segue must be connected to the controller and not directly to the button.
Check if your storyboard is set correctly (post some screenshots if you need help).
In such situation, if your segue is connected to the button AND, at the same time, to an action that performs it programmatically, your segue will be performed twice, and the second perform will cause that error.

Unwind works properly until I add a prepareForSegue

I need help with this app I am building. If you need to see the full code, I can post it, but here is the scenario:
I have 3 UIViewControllers, SMPViewController, SMPLetterViewController and SMPDetailsViewController.
In SMPViewController I have a prepareForSegue, and an UnWind:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
SMPLetterViewController *destination = [segue destinationViewController];
destination.lblSectionText = _lblForSectionTitles;
}
-(IBAction)returnToMain:(UIStoryboardSegue *)segue{
//just return to Home page
}
In SMPLetterViewController I have a prepareForSegue, an UnWind and a button titled, “Back” that connects with the UnWind of the SMPViewController:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
SMPDetailsViewController *destination = [segue destinationViewController];
destination.lblTextLetter = _lblWordSort;
}
-(IBAction)returnToLetterList:(UIStoryboardSegue *)segue{
//just return to Letter page
}
In the SMPDetailsViewController, I have a button titled, “Home” which connects the UnWind of the SMPViewController, and a button titled, “Back” which connects with the UnWind of the SMPLetterViewController.
When I run the program everything is working properly except the Back button on the SMPLetterViewController, it keeps crashing my App. with an error telling me something is wrong with the lblTextLetter in the prepareForSegue in the SMPLetterViewController. When I comment out the prepareForSegue in the SMPLetterViewController everything works great, except I cannot transfer the information to the SMPDetailsViewController.
To me the prepareForSegue’s in both the SMPViewController and SMPLetterViewController syntax is correct so why does it not work and why is it singling out the lblTextLetter?
Any ideas as to what is wrong? Do I just need another pair of eyes as I am missing something?
Thanks for any help,
socamorti
In storyboard add segue identifier and change your prepareForSegue to something like that:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"YOUR_IDENTIFIER"]) {
SMPDetailsViewController *destination = [segue destinationViewController];
destination.lblTextLetter = _lblWordSort;
}
}
And do something like that for your second prepareForSegue as well.
This issue happens because when you run unwind action the prepareForSegue is called as well.

Resources