سؤال

I have three classes :

1.TyphoonViewController 2.WaterTableNaviController 3.WaterTableViewController

I am trying to pass a string from the first one to the last. But I only pass the string from the first to the second. It will be a black screen when I present the third ViewController.

I've tried to make the WaterTableViewController be the root of WaterTableNaviController in storyboard. It did present screen, but the string wasn't passed.

From TyphoonViewController

- (IBAction)ParseBut:(id)sender {
WaterTableNaviViewController *vc = (WaterTableNaviViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"WaterTableNavi"];
[vc setUrl:@"My String"];
[self presentViewController:vc animated:YES completion:nil];
}

From WaterTableNaviController

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];

WaterTableViewController* vc = (WaterTableViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"WaterTable"];
[vc setParseUrl:m_url];
[self.navigationController pushViewController:vc animated:YES];
}
هل كانت مفيدة؟

المحلول

The code you have in the navigation controller is wrong - if the WaterTableVC is the root view controller of the navigation controller, it's already been instantiated, so you shouldn't do it again. Also, you don't push to the root view controller. You don't need to subclass the navigation controller at all to do what you are trying to do here. You should make a modal segue from the button in TyphoonViewController to the navigation controller, and make WaterTableViewController the root view controller of that navigation controller. The button should not have any action method, because it triggers the segue directly. Instead, you should have the following code in prepareForSegue,

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UINavigationController *nav = segue.destinationViewController;
    WaterTableViewController *waterTableVC = (WaterTableViewController *)nav.topViewController;
    [waterTableVC setURL:@"My String"];
}

نصائح أخرى

You can simply make another singleton class and put a property that can contain that string, so wherever you are, you can read that property

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top