0

I'm trying to get WebKit to display the page title of the currently opened document window, which includes a WebView. The document won't display after I run the app.

I'm using Document.m and windowNibName to achieve this, but I'm sure what I'm doing is wrong. I think it might be with both the NSStrings, which I can't have. Though if I don't have them I can't return the Document title... (WebKit/WebKit.h is imported)

   - (NSString)windowNibName:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
{
    // Report feedback only for the main frame.
    if (frame == [sender mainFrame]){
        [[sender window] setTitle:title];
    }
    return @"Document";
}
ATV
  • 3,246
  • 3
  • 18
  • 40
  • Objective-C methods can't be nested like this. (You have the definition of `-webView:didReceiveTitle:forFrame:` inside the definition of `-windowNibName`. In addition, the return type of `-windowNibName` is `NSString *` whereas here nothing is returned. – Nate Chandler Feb 18 '13 at 00:04
  • HA! I'm so stupid, you're right! Could I basically nest - (NSString *)windowNibName:(WebView *) and return("+webView+"); ? –  Feb 18 '13 at 00:10
  • 1
    @Pxlc: Even if concatenating random objects with strings were supported in Cocoa, the `description` of a WebView probably isn't what you're looking for. – Peter Hosey Feb 19 '13 at 05:40

1 Answers1

2

You've implemented a method named windowNibName:didReceiveTitle:forFrame:, but nothing sends such a message (unless you are, but if you are, you left that code out).

The correct method name is webView:didReceiveTitle:forFrame:. Assuming you only have one web view in the window and only one window in the document, and assuming that the document is the web view's delegate, you can simply set the document's display name from there.

If you have something more complex going on, with multiple web views per window and/or multiple windows per document, then you'll need to ask the web view for its window, the window for its window controller, and the window controller for its document, and then set the document's display name as above.

Also, windowNibName has nothing to do with setting the document's title to the web page's title. The web view probably hasn't even started to load a page yet, if it even has a URL to load at all. That's why you need to be the web view's delegate and respond to that delegate message.

Peter Hosey
  • 93,914
  • 14
  • 203
  • 366