Supertecnoboff
Supertecnoboff

Reputation: 6606

Cannot pass data via UINavigation controller- iOS

I have an iOS app setup as a Single View Controller. I am opening and passing in a string to a UINavigationController which is in a separate storyboard file with the following code:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"RSSView" bundle:nil];
RSSNav *nav_view = [storyboard instantiateViewControllerWithIdentifier:@"RSSNav"];
nav_view.pass_nav_data = [NSString stringWithFormat:@"%@", [action_options objectAtIndex:indexPath.row]];
[self addChildViewController:nav_view];
nav_view.view.frame = self.view.frame;
[self.view addSubview:nav_view.view];
[nav_view didMoveToParentViewController:self];

That all works fine, the problem is that, the UINavigationController has a rootviewcontroller called RSSView (setup as a Table View Controller). I want to pass the passed in string from the navigation controller to the RSSView.

In the navigation controller I have the following code which attempts to pass the passed in string to the tableview controller (set in the viewDidLoad method):

// Pass in the RSS url data.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"RSSView" bundle:nil];
RSSView *rss_view = [storyboard instantiateViewControllerWithIdentifier:@"RSSView"];
[rss_view setPass_data:pass_nav_data];

I can pass data to the navigation controller, but I can't pass that data to the table view controller. When I check the passed in string in the table view controller, it comes in as (null) even though I am passing in a string which is not null.

So what is wrong? All I want to do is to pass in once string value.

I really don't want to have to resort to using NSUserDefaults here, but I guess I will have to, if there is no way to pass in one string value.

Thanks for your time, Dan.

Upvotes: 0

Views: 46

Answers (1)

Levi
Levi

Reputation: 7343

What you are doing is creating a new instance of your RSSViewwhich should already exist as root of your navigation controller. So when you are setting the pass_data, it is done to an instance that is not on the screen.

You could read this value e.g. in the viewDidLoad of your RSSViewby doing something like:

RSSNav *nav_view = self.navigationController;
[self setPass_data:nav_view.pass_nav_data];

I am not sure, but something like this might also work:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"RSSView" bundle:nil];
RSSNav *nav_view = [storyboard instantiateViewControllerWithIdentifier:@"RSSNav"];
nav_view.pass_nav_data = [NSString stringWithFormat:@"%@", [action_options objectAtIndex:indexPath.row]];
[self addChildViewController:nav_view];

RSSView *rss_view = [nav_view visibleViewController]; 
[rss_view setPass_data:pass_nav_data];

nav_view.view.frame = self.view.frame;
[self.view addSubview:nav_view.view];
[nav_view didMoveToParentViewController:self];

I am using the same names as you, but I suggest you read some coding guidlines and use different names for your classes and variables.

Upvotes: 1

Related Questions