Question

I have a table view which pushes a detailed view on cell click. The new view pushes however I'm trying to pass a web address to the detailed view which loads a webView but I'm unsure on how to do this. Here is my code:

-(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];
} 

Any help would be greatly appreciated.

Was it helpful?

Solution 2

You should add an instance variable to the DetailViewController which will hold the address & set it to the desired value. Then (with the proper accessor) set it before/after pushing the view.

e.g. if you defined the variable as 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];

The only thing that remains to be done is to set the web view's address to this value on viewWillAppear: or something.

Edit:

A sample viewWillAppear: method would look like this:

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

    [super viewWillAppear:animated];
}

OTHER TIPS

You need to use a property for that in your detail view and pass the URL in the didSelectRowAtIndexPath method like this :

- (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];
}

and in your DetailView :

@property (nonatomic, strong) NSString *webAddress;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top