jwknz
jwknz

Reputation: 6824

Passing a NSString to another view with Storyboard

Alright,

I have read some Q&A's on this, but I need to be able to relate it to my code and I am not being successful, I hope someone is able help me:-)

I have a UITableViewController that is populated with a NSMUtableArray.

I would like to link to each entry and then have that URL display in a DetailViewController which has UIWebView in it.

Now I had the idea of passing a string along from one VC to the next, but that does not seem to work.

This is the code I have in the First TVC that is relevant:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int currentindex = [indexPath row];
switch (currentindex) {
    case 0:
            _urlAddress = @"http://localhost:8888/thePalmsMenu/breakfast_menu.php";

        break;

    case 1:

            _urlAddress = @"http://localhost:8888/thePalmsMenu/lunch_menu.php";

        break;
    default:
        break;

}

webVC *sView = [[webVC alloc] init];
sView.urlAddress = _urlAddress;
[self.navigationController pushViewController:sView animated:YES];

}

And then I have this in the Second View Controller which has the UIWebView in it.

-(void)viewDidLoad
{

    _vc.urlAddress = _urlAddress;


    NSURL *url = [NSURL URLWithString:_urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
   [_webView loadRequest:requestObj];

}

Now also at the moment the code show I am doing a push navigation, but that is not working - if I embed the first view then I get an error saying I can't push that view twice, so they must be related.

this is the error message for that:

2012-06-08 22:23:28.965 menu_test[68491:f803] nested push animation can result in corrupted navigation bar 2012-06-08 22:23:29.447 menu_test[68491:f803] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted. 2012-06-08 22:23:29.449 menu_test[68491:f803] Unbalanced calls to begin/end appearance transitions for .

Now even though these seem like 2 questions, they are sure to be related.

Also I have tried playing with NSUserDefaults, but that is not working as smooth, because the link needs to be set before I can use it, rather than directly passing it on.

Any help would be great:-)

Upvotes: 0

Views: 519

Answers (1)

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

What is happen is that you are really pushing the view controller twice, first time is from the storyboard push sequence, and the second time you do it from code,

You have 2 solutions I will write down the easiest of them (if you need the second too, please ask)

you will need to pass the data from the prepareForSegue method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"TheIdentifierOfYourDetailView"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        int currentindex = [indexPath row];
        switch (currentindex) {
            case 0:
                _urlAddress = @"http://localhost:8888/thePalmsMenu/breakfast_menu.php";

                break;

            case 1:

                _urlAddress = @"http://localhost:8888/thePalmsMenu/lunch_menu.php";

                break;
            default:
                break;

        }

        [[segue destinationViewController] setUrlAddress:_urlAddress];
    }
}

Upvotes: 1

Related Questions