0

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.

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

}

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

please help a lost man :D

Hemang
  • 26,840
  • 19
  • 119
  • 186
D79
  • 11
  • 5
  • Do u want to push to another view controller? Is yes then declare a string variable in that view controller and pass this url to that variable. Then load the string url in ViewDidLoad. – Ramaraj T Nov 03 '12 at 10:34
  • @Suresh how do I do that?, I am a new n00b in Xcode. – D79 Nov 03 '12 at 10:41

2 Answers2

0

Well, you've firstViewController already designed and coded, now you've to create secondViewController with a UIWebView binded in IB it self, also it have a setter NSString variable like strUrl that you need to pass at the time of pushing or presenting secondViewController, and assign it to UIWebView in viewDidLoad of secondViewController. See my answer about how to pass a NSString? Also UIWebView has its delegates method (What's delegate & datasource methods? - Apple Doc) Which you can use to handle URL request.

These are the delegate of UIWebView, if you'll going to use it, you need to give UIWebView delegate to self.

- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
Community
  • 1
  • 1
Hemang
  • 26,840
  • 19
  • 119
  • 186
0

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 //declare url and set property

NSURL *urlToLoad;

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

}
Ramaraj T
  • 5,184
  • 4
  • 35
  • 68
  • okay I tryed that, and edit for my code, but I get error(s) in the second.urltoload=allURLS; the error is "Use of undeclared identifier 'second' – D79 Nov 03 '12 at 13:14
  • okay that got fixed. But I don't understand what to type in my secondviewcontroller.h file? – D79 Nov 03 '12 at 13:29