-1

我有一个表格视图,它会在单元格单击时推送详细视图。新视图推送但是我试图将网址传递给加载 webView 的详细视图,但我不确定如何执行此操作。这是我的代码:

-(void)setupArray {
    webAddress = [[NSMutableDictionary alloc]init];
    [webAdress setObject:@"http://www.google.com.au" forKey:@"first item"];
    [webAdress setObject:@"http://www.yahoo.com.au" forKey:@"second item"];

menuItems = [webAdress allKeys];

}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [webAdress count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = [menuItems objectAtIndex:indexPath.row];
return cell;

}

- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
[self.navigationController pushViewController:detailView animated:YES];
} 

任何帮助将不胜感激。

4

2 回答 2

2

您需要在详细视图中使用该属性并在 didSelectRowAtIndexPath 方法中传递 URL,如下所示:

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
   DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
   detailView.webAddress = [menuItems objectAtIndex:indexPath.row];
   [self.navigationController pushViewController:detailView animated:YES];
}

并在您的 DetailView 中:

@property (nonatomic, strong) NSString *webAddress;
于 2013-09-18T16:24:38.803 回答
1

您应该向其中添加一个实例变量,该变量DetailViewController将保存地址并将其设置为所需的值。然后(使用适当的访问器)在推送视图之前/之后设置它。

例如,如果您将变量定义为webAddress

DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
[detailView setWebAddress:[webAddress objectForKey:[menuItems objectAtIndex:[indexPath row]]]]; // or some other way to get the correct address
[self.navigationController pushViewController:detailView animated:YES];

唯一需要做的就是将 web 视图的地址设置为这个值viewWillAppear:或其他东西。

编辑:

示例viewWillAppear:方法如下所示:

- (void)viewWillAppear:(BOOL)animated
{
    // construct request
    NSURL *url = [NSURL URLWithString:webAddress];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [webView loadRequest:req];

    [super viewWillAppear:animated];
}
于 2013-09-18T16:24:03.600 回答