Issue sending NSString objects from UITableViewController to UIViewController (using storyboard)

so I have this problem that drives me crazy for hours and I feel so stupid because it seems so simple. I've searched the web and fiddled with the code for the last 3 hours and I still can't figure it out :(

Before starting, I am using Xcode 4.3.1 and testing on iOS 5.1 (both simulator and physical device).

So, here's what I have done so far:

  • Use storyboards to connect all of my views.
  • One of my views is a UITableViewController and it references a UIViewController (using "push" and it works correctly in two ways)
  • UITableViewController subclass is named ScheduleViewController
  • UIViewController subclass named EventViewController
  • I have already filled all my sections and table cells with the appropriate content (I am using textLabel and detailTextLabel (this is not a problem) - For UIViewController, I only added a label and generated an IBOutlet for a label called: headerLabel

This is what I am trying to accomplish:

  • When user clicks on a table cell in ScheduleViewController go to EventViewController (up to this point it works!)
  • Based on the selected table cell, pass textLabel.text, detailTextLabel.text and section title text to EventViewController (kind of works, all values ​​are displayed in NSLog, but what is this ???)
  • I figured it was best to start small, so I'll start by setting the headerLabel text (in the EventViewController) to the section header text (in the ScheduleViewController). Once again, this works when I check the value with NSLog, but when I try to set the actual headerLabel text, it just displays and an empty string :(

This is where I got stuck and what I have tried so far:

  • Well, first of all, I already did the same (except iOS 4 and I didn't use storyboards) what I did in the didSelectRowAtIndexPath: method in the UITableViewController, I just did something like (and worked great :)):

    EventViewController * eventViewController = [[EventViewController alloc] initWithNibName: @ "EventViewController" bundle: nil]; eventViewController.headerLabel.text = stringToPass;

  • So of course this was the first thing I tried and of course it didn't work because that's how programming is haha.

  • So, I created my own initialization method in the EventViewController:

    • (id) initEventTime: (NSString *) eventTime andName: (NSString *) eventName andDate: (NSString *) eventDate;
  • I first tried calling it in the didSelectRowAtIndexPath: method in the ScheduleViewController and it worked, here is the method in the 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 (@ "% @", event_name); // 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 yourself; }

  • And this is how I called the method in the didSelectRowAtIndexPath: method (in the ScheduleViewController):

    EventViewController * evc = [[EventViewController alloc] initEventTime: timeValue andName: eventValue andDate: dateValue];

  • So, as you can see above, I first checked to see if the values ​​appeared in the output window using NSLog and they were all done!

  • But when I tried to set the headerLabel text it still returns an empty string
  • So, I tried to create a getter and setter (as you can see above) in the EventViewController, getting the name getTheEventDate and setTheEventDate.
  • In these methods, I just setTheEventDate using the eventDate argument and then get the value; and again it showed up in the output window but the headerLabel is empty
  • I also tried using NSLog to get the eventDate value, NSLog to get the headerLabel text value, and another NSLog to make sure the EventDate is irrelevant.
  • Here is the output of the output window:

    2012-03-31 my custom 2012-03-31 Jul 28 2012-03-31 2: Jul 28 2012-03-31 label: (null) 2012-03-31 3: Jul 28

+3


source to share


1 answer


If your view controllers are loaded from storyboard then anything about anything else would be a bad idea. :-)

The magic method for handling transitions from a storyboard is a ready-made ForSeque :. Take a look at it; segue has links to the controllers involved, and the sender parameter links you to the control that initiated the action.

Edit:



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    EventViewController *next = (EventViewController *)[segue destinationViewController];
    next.headerLabel.text = @"Hello!";
}

      

In real code, you would need to use the sender to find out what actual information to pass by querying a table for the sender pointer path or something.

0


source







All Articles