0

Today I'm working with an interesting problem. Right now, I have a first view that contains a webview, and then a separate view that contains a table with a list of URLs in a mutable array.

My question is, How do I load a URL from the row selected in the table in the first view, into the webview that is in a separate viewController?

I assume I'm going to need this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

What I've done so far is set up a property for the webViewController, and what I'm working on now is passing it to the webView from the bookmarks view, but the help would be appreciated.

Thanks for your time!

  • Jake
Jtaylorapps
  • 5,680
  • 8
  • 40
  • 56

1 Answers1

0

Well, you either need to implement an explicit intermediary class, or elect to adopt a design pattern that will enable communication. The two that come to mind in iOS would be a delegation pattern or a notification pattern.

In this case, my advice would be delegation. You'll define a delegate protocol on your list controller, and you'll conform to the protocol in your web view class. Somewhere up the chain the webview must be assigned as the table view's delegate, and then, in the tableView's didSelectRowAtIndexPath, you would implement the delegate call, passing the URL to the delegate, and the delegate then loads your URL in it's own view.

How do I create delegates in Objective-C?

Community
  • 1
  • 1
isaac
  • 4,867
  • 1
  • 21
  • 31
  • Now, I know NSUserDefaults aren't supposed to be for transferring information, but how would you feel about passing the item selected in the cell to NSUserDefaults, then bringing it back somehow? – Jtaylorapps Mar 05 '12 at 22:09
  • Yeah, that's not really what defaults are there for. The solution you're after is a fairly trivial affair using any number of approaches (if you really want to keep it *really* simple just use NSNotifications), I just wouldn't see any reason to resort to writing what is essentially an ivar into the user's defaults bundle... – isaac Mar 05 '12 at 22:32
  • Thanks for the insight. I really want to keep it as simple as I can, because I literally just need to move a string from one class to another. The problem I'm having is how I'm supposed to load it automatically as well. – Jtaylorapps Mar 05 '12 at 22:34
  • Your delegate method would take care of reloading the webview. – jsd Mar 05 '12 at 22:39