napolux
napolux

Reputation: 16094

UIWebView loadString not working in storyboards prepareForSegue function?

I see online lot of examples with the loadString called to load static HTML into UIWebView.

I'm trying to build the same in a prepareForSegue() function of my app.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    MYCLASSController *destViewController = segue.destinationViewController;

    if ([segue.identifier isEqualToString:@"fromListToDetail"]) {
        // let's suppose getHtmlTextForItem returns the NSString @"<b>Some HTML text</b> with formatting"       
        [destViewController.htmlText loadHTMLString: [db getHtmlTextForItem:bookId] baseURL:nil];
    }
}

But It doesn't load anything. Do you have any idea on how to solve this??? Am I doing something wrong?

Upvotes: 0

Views: 286

Answers (2)

jrturton
jrturton

Reputation: 119272

The web view is not instantiated until after your segue has been performed. You can confirm this in the debugger.

You should pass the HTML to the view controller as a string property, and then load it into the web view in viewDidLoad. It's a good general principle to only allow a view controller to manage its own views. External objects shouldn't be able to modify them. See this fantastic article for more.

Upvotes: 1

subzero
subzero

Reputation: 3450

Try loading the content in the viewDidLoad method of your class MYCLASSController.

- (void) viewDidLoad {
[UIWebViewInstance loadHTMLString: @"something" baseURL:nil];
}

Upvotes: 1

Related Questions