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.
Related
I'm trying to create a manual segue, which happens only if there is a certain conidition (a successful login).
The steps I have followed are these:
1) I've created a button in source viewcontroller, and i connected the button with a drag with the destination viewcontroller.
2) I set the identifier for the segue, calling it login_ok
3) I've created a -(IBAction)buttonPressed:(id)sender
Now, I tried to make an example, to see if the work or not. I wrote:
- (IBAction)buttonPressed:(id)sender
{
BOOL test = false;
if(test == true)
{
[self performSegueWithIdentifier:#"login_ok" sender:self];
}
Theoretically, the segue should not work, because i set test=false, and i said the segue should work only if test=true.
But the segue works and makes me go to the destination viewcontroller.
Why?
If you connected the button with a drag to the destination controller, it should segue anyway.
You need to connect the segue between screens and not from the button to the screen. Then, you can control your segue from the code.
You should define your segue between controllers not from button to the controller. Defining segue between view controllers is more useful than defining segue from any component to a view controller, because when you want to use a condition for any action, you can use these two methods effectively
[self performSegueWithIdentifier:#"segueID" sender:self];
and
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
For example:
- (IBAction)buttonPressed:(id)sender
{
BOOL test = YES;
if(test == YES)
[self performSegueWithIdentifier:#"login_ok" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"login_ok"])
{
YourNextController *controller = [segue destinationViewController];
controller.welcomeText = #"Hi! Test is true congratulations";
}
}
Especially passing data from controller to another one, this way is always useful.
In my app, I use segment to switch in three view. Every segmment represented with container view and they become hidden or appear according to segment. That is how my the design works, and it works well. You can find the picture below, so you can understand the structure more:
I'm having trouble with giving an instance (namely, user) created in the view controller to segment's container's view controller. I created user in the user and assign it the values. user instance has the values, I checked it. So, as usual to give with segue, I tried prepareForSegue:sender method to achieve my purpose. In the viewLoad method of the table views(you can find the table views: each of them are attached to its own container in the view controller that has segment. There are 3 containers.) user instances of table views are null. I can't give it, basically. Shouldn't the view controller pass the user value? It might being container view, but still I think it should be :) Anyway, please find below what I have written so far:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
DovizTableViewController *controller = (DovizTableViewController *)navController.topViewController;
controller.user = self.user;
}
You would remove the sender part of your code.
It should look like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue{
//your code
//check if the next view exists by checking for the id
if ([[segue identifier] isEqualToString:#"Segue_Name"])
{
DovizTableViewController *controller = [segue destinationViewController];
controller.user = self.user;
}
}
Try this.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
DovizTableViewController *controller = [segue destinationViewController];
// Pass any objects to the view controller here, like...
controller.user = self.user;
}
}
Hope it helps.
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
}
}
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];
I am trying to create an app with multiple scenes the first two scenes connect and transition fine using a segue, but when i add a third scene and connect it with a segue nothing happens when i click the control to transition. Here is the code i have so far
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:#"showStops"]){
NSIndexPath *indexPath = [self.directionTableView indexPathForSelectedRow];
StopsViewController *destViewController = (StopsViewController *)segue.destinationViewController;
destViewController.routeNumber =[[feeds
objectAtIndex:indexPath.row]objectForKey:#"rtNumber" ];
destViewController.direction =[[feeds objectAtIndex:indexPath.row]objectForKey:#"Direction"];
}
}
So let me see if I got it. U have a screen that connect to two others screens right? So u need two Segues with differents identifier. Than in method prepareForSegue u will check wich one was clicked.
Something like this!
- (IBAction)firstConnection:(UIButton *)sender {
[self performSegueWithIdentifier:#"firstSegue" sender:nil];
}
- (IBAction)secondConnection:(UIButton *)sender {
[self performSegueWithIdentifier:#"secondSegue" sender:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"firstSegue"]){
// YOUR TRANSITION
}
if([segue.identifier isEqualToString:#"secondSegue"]){
// YOUR TRANSITION
}
}