Search code examples
uiviewcontrolleruiwebviewprotocolsnsurlrequestuiwebviewdelegate

How to load uiwebview from another view controller


I have a uiwebview in first viewcontroller,and i have an uibutton on secondview controller,when i click uibutton i am changing load request. The thing is ,the url is changed but webview didnt reload.

FirstViewController

 -(void)viewDidLoad
{
    a=@"http://www.google.com";
   [self loadWebview];
}
-(void)loadWebview
{
       a=[[NSUserDefaults standardUserDefaults]objectForKey:@"key"];
        NSLog(@"current URL:%@",a);
       self.myweb.delegate=self;
       [self.myweb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:a]]];
 }

  - (IBAction)goSecond:(id)sender 
  {  
         SecondViewController *vv=[[SecondViewController alloc]init]; 
         [self.navigationController pushViewController:vv animated:YES];
  }

SecondViewController

- (IBAction)goFirst:(id)sender {

      NSString* b=@"http://www.apple.com";
     [[NSUserDefaults standardUserDefaults] setObject:b forKey:@"key"];    
      FirstViewController *vv=[[FirstViewController alloc]init]; 
      [vv loadWebview];
      [self.navigationController popViewControllerAnimated:YES];


 }

Result is:
 current URL:http://www.google.com
 current URL:http://www.apple.com

But webview didn't load,i want to load www.apple.com.Thanks in advance


Solution

  • You pop the view on button click.write.So on first view controller write the method of loadWebView in viewWillAppear instead of viewWillLoad.And in secondController why are you allocating a new controller object and pop the previous view controller.No need to allocate and load.Simply pop the view.