PHP Web Dev 101
PHP Web Dev 101

Reputation: 555

Multiple UIwebViews on Xcode

I have about +3 UIwebViews, how do I code each one to go to different URLs.

Here the code from my ViewController.m file

 #import "ViewController.h"
    @interface ViewController ()


    @property (weak, nonatomic) IBOutlet UIWebView *webView;


    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
        NSString *url=@"http://test.bithumor.co/test22.php";
        NSURL *nsurl=[NSURL URLWithString:url];
        NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
        [webview loadRequest:nsrequest];
        [self.view addSubview:webview];
        // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }



    @end

How can I keep the same exact code for each UIwebViews but for each one to go to different webpages?

Example:
UIwebView1 = http://example.com/1
UIwebView2 = http://example.com/2
UIwebView1 = http://example.com/3

Upvotes: 0

Views: 798

Answers (1)

Fred Faust
Fred Faust

Reputation: 6790

You have two choices, you can create three instances of a UIWebView with a URL & request for each or you can re-use the same one and just keep re-assinging it's URL request then calling it again.

If you want to create three separate UIWebView instances you'll just do wha you have except twice more, though adding each one to your current view will just give you the last one being visible.

UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    NSString *url=@"http://test.bithumor.co/test22.php";
    NSURL *nsurl=[NSURL URLWithString:url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webview loadRequest:nsrequest];

UIWebView *webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    NSString *url2=@"http://nextUrl";
    NSURL *nsurl2=[NSURL URLWithString:url2];
    NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
    [webview2 loadRequest:nsrequest2];

UIWebView *webview3=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    NSString *url3=@"http://anotherNexturl";
    NSURL *nsurl3=[NSURL URLWithString:url3];
    NSURLRequest *nsrequest3=[NSURLRequest requestWithURL:nsurl3];
    [webview3 loadRequest:nsrequest3];

Or, depending on how you're switching between your web views you can just re-assign the NSURLRequest and re use the same instance of the UIWebView

UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    NSString *url=@"http://test.bithumor.co/test22.php";
    NSURL *nsurl=[NSURL URLWithString:url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webview loadRequest:nsrequest];

    NSString *url2=@"http://nextUrl";
    NSURL *nsurl2=[NSURL URLWithString:url2];
    NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
    [webview loadRequest:nsrequest2];

    NSString *url3=@"http://nextUrl";
    NSURL *nsurl3=[NSURL URLWithString:url3];
    NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl3];
    [webview loadRequest:nsrequest3];

Upvotes: 2

Related Questions