user2045137
user2045137

Reputation: 1

Pass URL from ViewController

I have a ViewController1 with 2 buttons. And a second ViewController2 whith a UIWebView.

What I´m trying to do:

1- when i click Button1 in ViewController1 it goes to UIWebView in ViewController(2) and open @"www.google.com".

2 - When I click Button2 in ViewController1 it goes to the same UIWebView in ViewController2 and open @"www.apple.com".

How can I pass the URL from the button1 & 2 to the UIWebView in ViewController2?

thanks for your help.

Upvotes: 0

Views: 219

Answers (2)

Caleb
Caleb

Reputation: 124997

Assuming you have one action that's triggered by both buttons, you'd do something like:

- (IBAction)buttonPushed:(UIButton*)sender
{
    ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
    switch (sender.tag) {
        case 1:  // button one tapped
            vc2.url = [NSURL URLWithString:@"url for button 1 here";
            break;
        case 2:  // button one tapped
            vc2.url = [NSURL URLWithString:@"url for button 2 here";
            break;
    [self.navigationController pushViewController:vc2 animated:YES];
}

As you can see, the idea is to have the first view controller pass along whatever information the second view controller needs when it creates the second view controller.

Upvotes: 0

sapi
sapi

Reputation: 10224

Have a property on the second UIViewController (subclass) of the form

@property (strong, nonatomic) NSURL *url;

When the button is pressed, set this as appropriate, then push the UIViewController onto the UINavigationController.

In the second UIViewController's viewDidLoad method, instruct the UIWebView to open self.url.

Upvotes: 2

Related Questions