0

Spent the past day trying to figure this out and the solution is probably simple but I just can't figure it out. Basically I'm trying to send the current URL of my WebView to a NSTextField in another class. Sending the WebView URL to a string in MyViewController works fine so I know it is being passed. However, when I assign this string to my TextField.StringValue, it returns (null).

  • Passing a variable from one view controller to another isn't easy. A simple solution is to use a singleton such that you send a variable to it and then let it pass to another. – El Tomato Apr 02 '16 at 13:47

2 Answers2

1

It looks like you're having a problem with the relationships between your view controllers. How are the two things supposed to be related?

Consider this point in your code:

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame{
    // Getting url from WebView
    NSString *newURL = [[[[frame dataSource] request] URL] absoluteString];
    MyViewController *vc = [[MyViewController alloc] init];
    [vc UrlSet:newURL];
}

Here you create a brand new instance of MyViewController and then you pass it a URL, but you never do anything with that brand new instance. It is simply deleted once the routine finishes. Did you mean to find an already existing instance of MyViewController and tell that instance about the URL? That seems much more reasonable.

Scott Thompson
  • 22,629
  • 4
  • 32
  • 34
  • Think I'm going to need to rethink how my methods work as I haven't taken into account my relationships between View Controllers. Currently I have MyViewController --> tabHandler --> FirstWebController, where tabHandler is in a Container View in MyViewController. Gonna have another go tomorrow and see if I can sort it out – JonnyRoberts95 Apr 02 '16 at 20:45
0

So after spending another few days trying to solve the problem using delegates, I gave up and moved onto NSNotificationCenter. Its probably not the most efficient method but it worked a treat and does the job perfectly.

I simply sent the current URL from my FirstWebController class, to my MyViewController class, using NSNotificationCenter and setting the object to my current URL. Once in the MyViewController class, I called the notification method and assigned my textfield to the object.

Here's a good example

Community
  • 1
  • 1