Passing Data from a View Controller to a First Detail Controller and then
to a Second Detail Controller
Posting my first question here on stackoverflow! Hope my question is
clear, here it is...
I have a Navigation Controller embedded in a First View Controller that
has a table view. From the table view I have a segue that pushes to a
First Detail Controller. Everything works fine, the segue passes the data
to the First Detail Controller, however I would like to push again from a
button to a Second Detail Controller.
The layout in the storyboard looks like this Image of storyboard
I set-up a Second Detail Controller and pushed to that from a button on
the First Detail Controller. However I can not figure out how to pass the
data I have in an array along to the Second Detail Controller. I added an
NSLog line to see what was being passed along and the debug panel is
outputing "(null)"
Here is my viewdidload and segue code from the .m First View Controller
named ColorBookViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
Color *color1 = [Color new];
color1.name = @"Red";
color1.hexnumber = @"FF0000";
color1.spanishname =@"Rojo";
Color *color2 = [Color new];
color2.name = @"Green";
color2.hexnumber = @"00FF00";
color2.spanishname =@"Verde";
Color *color3 = [Color new];
color3.name = @"Blue";
color3.hexnumber = @"0000FF";
color3.spanishname = @"Azul";
colors = [NSArray arrayWithObjects: color1,color2,color3, nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showColorDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ColorDetailViewController *destViewController =
segue.destinationViewController;
destViewController.color = [colors objectAtIndex:indexPath.row];
}
}
Here is the view did load from the .m of the First Detail Controller named
ColorDetailController:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"This is...%@", color.name);
self.nameLabel.text = color.name;
self.hexnumberLabel.text = color.hexnumber;
}
My .h for the SecondDetailController:
#import <UIKit/UIKit.h>
#import "Color.h"
@interface SecondDetailViewController : UIViewController
@property (nonatomic,weak) IBOutlet UILabel *spanishnameLabel;
@property (nonatomic, strong) Color *color;
@end
Here is the view did load from the .m of the Second Detail Controller
named SecondDetailController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = color.name;
NSLog(@"This is...%@", color.spanishname);
self.spanishnameLabel.text = color.spanishname;
}
The app runs fine in the simulator, but I get a blank screen for the
Second Detail Controller and I get the "null" result from NSLog(@"This
is...%@", color.spanishname);
Any suggestions as to why the segue passes and holds the instance for the
first Detail View but not the Second Detail View? Or am I missing
something more fundamental here? I tried setting up a second segue on the
First Detail Controller and the NSLog produces the correct result (the
color name in Spanish), but I am not sure how I would pass this onto the
Second Detail Controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showMoreDetail"]) {
NSLog(@"This is... %@",color.spanishname); //works fine
self.spanishnameLabel.text = color.spanishname; //throws error
}
}
Thanks for any help or insight!
No comments:
Post a Comment