如何从另一个视图控制器加载uiwebview

时间:2013-08-27 12:07:47

标签: uiviewcontroller uiwebview protocols nsurlrequest uiwebviewdelegate

我在第一个viewcontroller中有一个uiwebview,我在secondview控制器上有一个uibutton,当我点击uibutton我正在改变加载请求。问题是,网址已更改但webview没有重新加载。

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

但是webview没有加载,我想提前加载www.apple.com.Thanks

3 个答案:

答案 0 :(得分:0)

你在第二个视图控制器并调用加载第一个视图控制器的方法。并且WebView作为第一个视图控制器的子视图。所以你需要推动你的第一个视图控制器才能看到效果。在第二个视图控制器你可以'看看你的第一次发生了什么。

答案 1 :(得分:0)

first of all you pass ur link two another view controller//


your first view coding like this
- (IBAction)goFirst:(id)sender 
{
    secondview *video=[[secondview
  alloc] initWithNibName:@"secondview" bundle:nil];
    video.videourl=@"http://www.google.com";
    [self.navigationController pushViewController:video animated:YES];
    [video release];
}

secondview controller

in .h file
 NSString *videourl;
@property(nonatomic,retain) NSString *videourl;
in .m file
@synthesize videourl;
 -(void)viewDidLoad
{
   [self loadWebview];
}
-(void)loadWebview
{
    NSString *urlAddress = videourl;

    //Create a URL object.
    NSURL *url = [NSURL URLWithString:urlAddress];

    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    //Load the request in the UIWebView.
    [_youtube_web(your webview object) loadRequest:requestObj];

}

答案 2 :(得分:0)

在第一个视图控制器上弹出按钮click.write.So上的视图在viewWillAppear中写入loadWebView的方法而不是viewWillLoad.And在secondController中为什么要分配新的控制器对象并弹出以前的视图控制器。不需要分配和加载。只需弹出视图。