so I am having this issue that has been driving me crazy for hours, and I feels so stupid because it seems so simple. I’ve been searching the web and messing around with the code for the last 3 hours and I still can’t figure it out 🙁
Before I start, I am using Xcode 4.3.1 and testing on iOS 5.1 (both simulator and physical device).
So here is what I’ve done so far:
- Use storyboards to connect all my views
- One of my views is a UITableViewController, and it links to a UIViewController (using ‘push’, and it works correctly going back and forth between the 2 views)
- The UITableViewController subclass is called ScheduleViewController
- The UIViewController subclass is called EventViewController
- I have already populated all of my sections and table cells with the appropriate content (I’m using textLabel and detailTextLabel (this is not an issue)
-For UIViewController, I have only added a label and generated an IBOutlet for the label called: headerLabel
This is what I am trying to accomplish:
- When the user clicks on a table cell in ScheduleViewController, go to EventViewController (up to this point it works!)
- Based on the selected table cell, pass the textLabel.text, detailTextLabel.text, and Section Header text to EventViewController (kind of works, all values appear with NSLog, but that’s it???)
- I figured it’s best to start small, so I am going to start by setting the headerLabel text (in EventViewController), to the Section Header Text (in ScheduleViewController) — Once again this seems to be working when I check the value with NSLog, but when I try to set the actual text of headerLabel, it just displays and empty string 🙁
Here is where I am stuck and what I have tried so far:
-
Well first of all I’ve already done something exactly the same (except it was with iOS 4 and I was not using storyboards), what I did was in the didSelectRowAtIndexPath: method in the UITableViewController, I just did something like (and worked fine 🙂 ):
EventViewController *eventViewController = [[EventViewController alloc] initWithNibName:@”EventViewController” bundle:nil];
eventViewController.headerLabel.text = stringToPass; -
So of course, that was the first thing I tried, and of course it didn’t works because that’s just how programming is haha.
-
So next I created a custom init method in my EventViewController:
- (id) initEventTime: (NSString *) eventTime andName: (NSString *) eventName andDate: (NSString *) eventDate;
-
First I tried calling it in the didSelectRowAtIndexPath: method in ScheduleViewController, and it kind of worked, here is the method in EventViewController:
//Method to initialize event time, name, and date.-
(id) initEventTime: (NSString *) eventTime andName: (NSString *) eventName andDate: (NSString *) eventDate
{
self = [super initWithNibName:@”EventViewController” bundle:nil];
if (self)
{
NSLog(@”my custom”);
// Custom initialization//NSLog(@”%@”,eventTime);
//NSLog(@”%@”,eventName);
//NSLog(@”%@”,eventDate);
NSString *mystr = [NSString stringWithFormat:@”%@”,eventDate];
NSLog(@”1: %@”,mystr);
[self setTheEventDate:eventDate];
NSString *mystr2 = [self getTheEventDate];
NSLog(@”2: %@”,mystr2);
headerLabel.text = [self getTheEventDate];
NSLog(@”label: %@”, headerLabel.text);
NSLog(@”3: %@”, eventDate);//self.headerLabel.text = [NSString stringWithFormat:@”%@”,eventDate];
}
return self;
}
-
-
And here is how I called the method in the didSelectRowAtIndexPath: method (in ScheduleViewController):
EventViewController *evc = [[EventViewController alloc] initEventTime:timeValue andName:eventValue andDate:dateValue];
-
So as you can see above I first checked if the values appeared in the output window using NSLog, and they all did!
- But when I tried to set the headerLabel text is still returned an empty string
- So then I tried creating a getter and setter (as you can see above) in EventViewController, called getTheEventDate and setTheEventDate.
- In those methods I simply setTheEventDate using the eventDate argument, and then get the value; and once again it appeared in the output window, but an empty string of headerLabel
- I also tried using NSLog to get the value of eventDate, an NSLog to get the text value of headerLabel, and another NSLog to make sure eventDate was not null.
-
Here is the output from the output window:
2012-03-31 my custom
2012-03-31 July 28
2012-03-31 2: July 28
2012-03-31 label: (null)
2012-03-31 3: July 28
If your view controllers are loaded from a storyboard, then anything concerning a nib is going to be a bad idea. 🙂
The magic method for handling storyboard transitions is prepareForSeque:. Take a look at it; the segue has references to the controllers that are involved and the sender parameter links you to the control that initiated the action.
Edit:
In real code, you would need to use the sender to find out what actual information to pass by asking the table for the sender’s index path or something of that kind.