从ViewController传递URL

时间:2013-02-07 23:52:56

标签: ios xcode uiwebview

我有一个带2个按钮的ViewController1。第二个ViewController2和UIWebView。

我想做什么:

1-当我点击ViewController1中的Button1时,它会转到ViewController(2)中的UIWebView并打开@“www.google.com”。

2 - 当我在ViewController1中单击Button2时,它会转到ViewController2中的同一个UIWebView并打开@“www.apple.com”。

如何从button1&中传递网址? 2到ViewController2中的UIWebView?

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

在表单

的第二个UIViewController(子类)上有一个属性
@property (strong, nonatomic) NSURL *url;

按下按钮时,将其设置为适当的,然后将UIViewController按到UINavigationController上。

在第二个UIViewController的viewDidLoad方法中,指示UIWebView打开self.url

答案 1 :(得分:0)

假设您有一个由两个按钮触发的动作,您可以执行以下操作:

- (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];
}

如您所见,我们的想法是让第一个视图控制器传递第二个视图控制器在创建第二个视图控制器时所需的任何信息。