0

I have a UIWebView with a Bookmark button. The bookmarks are saved within a UITableView. The name of the cell is the link of the bookmark. Now I want to click the cells and then the url of the bookmark should loud in my webview. But it does not work, when i click on the cells. This is my code for this:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [bookmarks objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    explorerView.urlField.text = [bookmarks objectAtIndex:indexPath.row];
    [explorerView textFieldShouldReturn:explorerView.urlField];
    [self.parentViewController dismissModalViewControllerAnimated:true];
}

Can you please give me a code example how to do this? This would be awesome.

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
J. Scott
  • 27
  • 7

1 Answers1

1

You missing actual code, that will load URL in UIWebView. You should add something like following code to your tableView:didSelectRowAtIndexPath:

NSString *URLString = [bookmarks objectAtIndex:indexPath.row];
NSURL *URL = [NSURL URLWithString: URLString];
NSURLRequest *URLRequest = [NSURLRequest requestWithURL:URLRequest];
[webView loadURLRequest:URLRequest];
Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
  • Thank you for fast reply, but I have this code already, but it does not work – J. Scott Mar 14 '16 at 20:04
  • Or maybe it is working, but I can't see it, because my uitableview is still there. How can I show my viewcontroller with the uiwebview to the user? – J. Scott Mar 14 '16 at 20:07
  • Hmm, I assumed that your table view controller presented over view controller with `UIWebView`. In this case after `[self.parentViewController dismissModalViewControllerAnimated:true]` call you should see your web view. By the way, please, use `YES` (Objective-C) instead of `true` (C++). – Borys Verebskyi Mar 14 '16 at 20:11
  • No, thats not the case. I have one ViewController for the webview. Then I have a slide out menu where you have a cell called "Bookmarks". And from this cell you reach the tableview with all bookmarks. And from here I want to get back to my first viewcontroller with the webview, which is presenting the bookmarked page. Sorry for the "true". Im very new in objective c and I coded before in c++ – J. Scott Mar 14 '16 at 20:34
  • I tried this: UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"Identifier"]; [self.navigationController pushViewController:vc animated:YES]; But now i have a thread 1: signal SIGABRT error – J. Scott Mar 14 '16 at 20:38
  • It really depends on how your slide out menu is implemented. If you used `pushViewController:animated:` to present it, you should use `[self.navigationController popViewControllerAnimated:YES]` to hide it. If you use some custom 3rd-party component, it should have some method to hide menu. It's very hard to guess without access to code itself – Borys Verebskyi Mar 14 '16 at 20:43
  • If you need to unwind segue, see https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html#//apple_ref/doc/uid/TP40007457-CH15-SW1 – Borys Verebskyi Mar 14 '16 at 20:51
  • OK, i made it very difficult to understand it for you, so I explain it simple: The slide out menu is not the problem. I'm in the table view menu. Normally I generate here cells on my own and connect them with the view controllers. But now they are generated by the user (the bookmarks), so I can not link them to any view controller. How can I do this programmatically? Normally i did this through the storyboard. – J. Scott Mar 14 '16 at 21:27
  • Take a look on the following question - http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers - hope it helps – Borys Verebskyi Mar 14 '16 at 21:41