按下一个按钮并在另一个ViewController中打开一个URL [英] Press a Button and open a URL in another ViewController

查看:58
本文介绍了按下一个按钮并在另一个ViewController中打开一个URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过制作一个简单的应用程序来学习Xcode.但是我在网上呆了好几个小时(几天),无法弄清楚如何制作一个在另一个ViewController中打开UIWebView的按钮:S

I am trying to learn Xcode by making a simple app. But I been looking on the net for hours (days) and I cant figure it out how I make a button that open a UIWebView in another ViewController :S

首先让我向您展示一些我已经准备好的代码:

first let me show you some code that I have ready:

我的主故事板上有一些按钮,每个按钮都带有一些国家代码,例如英国,加拿大和丹麦.

I have a few Buttons om my main Storyboard that each are title some country codes like UK, CA and DK.

当我按下其中一个按钮时,我将得到一个IBAction,如下所示:

When I press one of those Buttons I have an IBAction like this:

- (IBAction)ButtonPressed:(UIButton *)sender {
// Google button pressed

NSURL* allURLS;

if([sender.titleLabel.text isEqualToString:@"DK"]) {

    // Create URL obj

    allURLS = [NSURL URLWithString:@"http://google.dk"];



}else if([sender.titleLabel.text isEqualToString:@"US"])

{

    allURLS = [NSURL URLWithString:@"http://google.com"];

}else if([sender.titleLabel.text isEqualToString:@"CA"])

{

    allURLS = [NSURL URLWithString:@"http://google.ca"];

}
NSURLRequest* req = [NSURLRequest requestWithURL:allURLS];

[myWebView loadRequest:req];

}

如何在另一个名为myWebView的Viewcontroller上打开UIWebview?

How do I make this open UIWebview on my other Viewcontroller named myWebView?

请帮助一个失落的人:D

please help a lost man :D

推荐答案

firstViewController.m

firstViewController.m

   - (IBAction)ButtonPressed:(UIButton *)sender {
    NSURL* allURLS;
    //get your URL
    secondViewControlelr *secondView=[[secondViewControlelr alloc]init];
    second.urlToLoad=allURLS;
    [self.navigationController pushViewController:secondView animated:YES];
    }

secondViewControlelr.h//声明网址并设置属性

secondViewControlelr.h //declare url and set property

NSURL *urlToLoad;

secondViewControlelr.m

secondViewControlelr.m

- (void)viewDidLoad
{
         myWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
         [self.view addSubview:myWebView];
          NSURLRequest *urlReq=[NSURLRequest requestWithURL:self.urlToLoad cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10];
    [myWebView loadRequest:urlReq];

}

这篇关于按下一个按钮并在另一个ViewController中打开一个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆