【问题标题】:UIWebView not loadingUIWebView 未加载
【发布时间】:2023-03-03 15:58:01
【问题描述】:

我知道这是一个如此简单的问题,但我一直在努力解决它,它就是行不通。我试图让 UITableView 在选择特定行时打开 URL。使用 NSLog 我可以看到 urlSelected 变量设置正确并正确传递给 UrlViewController,但它根本没有显示 webview。它仅显示带有黑屏的导航菜单。如果我在两个视图控制器之间添加推送关系,则 ViewController 会正确加载,但将 urlSelected 变量传递给 UrlViewController 时遇到问题。传递此变量的正确方法是什么,或者我是否需要在两个 ViewController 之间设置不同的关系?这是我目前拥有的:

RootViewController.m:

    #import "RootViewController.h"
    #import "UrlViewController.h"
    ..........
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath         *)indexPath
    {

        UrlViewController *webController = [[UrlViewController alloc] init];


        if(indexPath.row == 0){
            webController.urlAddress = @"http://www.yahoo.com";
            NSLog(@"urlAddress being passed is:%@", webController.urlAddress);
            [self.navigationController pushViewController:webController animated:YES];

        }else if(indexPath.row == 1){
            webController.urlAddress = @"http://www.google.com";
            NSLog(@"urlAddress being passed is:%@", webController.urlAddress);
            [self.navigationController pushViewController:webController animated:YES];


        }else{
            webController.urlAddress = @"http://www.abc.com";
            NSLog(@"urlAddress being passed is:%@", webController.urlAddress);
            [self.navigationController pushViewController:webController animated:YES];

        }    

    }

UrlViewController.h:

      @interface UrlViewController : UIViewController{
          NSString *urlAddress;
      }
      @property (strong) IBOutlet UIWebView *webView;
      @property (nonatomic, retain) NSString *urlAddress;

UrlViewController.m:

      @implementation UrlViewController
      @synthesize webView;
      @synthesize urlAddress;
      ...........
     - (void)viewWillAppear:(BOOL)animated {
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:requestObj];
        NSLog(@"The value being receieved is:%@", urlAddress);
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }

我从 NSLogs 收到以下输出:

    2012-03-28 09:53:30.266 BeloitTurner[4278:f803] urlAddress being passed is:http://www.yahoo.com
    2012-03-28 09:53:30.269 BeloitTurner[4278:f803] The value being receieved is:http://www.yahoo.com

非常感谢任何帮助。

【问题讨论】:

  • 我已经回答了这个here!同样的问题很安静。

标签: xcode ios5 uiwebview tableview


【解决方案1】:

听起来您正在使用包含 RootViewController 和 UrlViewController 视图的故事板。如果是这种情况,您需要使用情节提要实例化 UrlViewController:

UrlViewController* webController = [self.storyboard instantiateViewControllerWithIdentifier:@"UrlViewController"];

使用它而不是在 tableView:didSelectRowAtIndexPath: 方法中分配初始化 webController 的方式。此外,在包含这两个视图控制器的视图的情节提要中,将 Interface Builder 中属性检查器的 View Controller 部分下的 Identifier 字段设置为字符串 UrlViewController。这样做而不是仅仅使用 alloc init 的原因是描述 UrlViewController 视图的文件位于情节提要内,因此 alloc 初始化 UrlViewController 意味着它不会找到其关联的视图,只会显示黑屏。

您也可以使用 Push 关系执行此操作。而不是 tableView:didSelectRowAtIndexPath: 方法,您只需设置从 RootViewController 中的单元格到 UrlViewController 的推送关系(一个 segue)。在您的 RootViewController.m 文件中,添加以下方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"PassUrlToUrlViewControllerSegue"]) {
        segue.destinationViewController.urlAddress = @"http://www.yahoo.com";
    }
}

这需要您设置标识符才能使 Push segue 工作。在情节提要中选择代表 Push segue 的箭头,并将其 Identifier 设置为字符串 PassUrlToUrlViewControllerSegue。

您可以通过使用 prepareForSegue:sender: 中提供给您的 sender 参数来实现动态化,以识别选择了哪个单元格,从而确定要传递哪个 url。

【讨论】:

  • 成功了,这正是我想要的。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2011-08-04
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 2014-04-02
相关资源
最近更新 更多