0

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

我正在尝试做的事情:

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

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

如何将按钮 1 和 2 中的 URL 传递给 ViewController2 中的 UIWebView?

谢谢你的帮助。

4

2 回答 2

2

在表单的第二个 UIViewController(子类)上有一个属性

@property (strong, nonatomic) NSURL *url;

按下按钮时,根据需要进行设置,然后将 UIViewController 推送到 UINavigationController 上。

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

于 2013-02-07T23:56:19.593 回答
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];
}

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

于 2013-02-07T23:59:26.133 回答