我有一个表格视图,可以查看单元格点击的详细视图。然而,新的视图推动我试图将网址传递给加载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];
}
非常感谢任何帮助。
答案 0 :(得分: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;
答案 1 :(得分:1)
您应该将实例变量添加到DetailViewController
,该变量将保存地址&将其设置为所需的值。然后(使用适当的访问器)在推动视图之前/之后设置它。
e.g。如果您将变量定义为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];
唯一还有待完成的事情是将网络视图的地址设置为viewWillAppear:
上的此值。
编辑:
示例viewWillAppear:
方法如下所示:
- (void)viewWillAppear:(BOOL)animated
{
// construct request
NSURL *url = [NSURL URLWithString:webAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
[super viewWillAppear:animated];
}