How do you bring normal control into NSView?

What I'm actually trying to do is put the WebKitView in a ScreenSaver (which inherits from NSView). I am completely new to MacOS X and Cocoa (but I am very familiar with Objective-C and have used some parts of GNUStep). Do I need to pave? I only want to show one control in the whole NSView.

0


a source to share


3 answers


In initWithFrame:isPreview:

, create a WebView in the usual way , then send a message addSubview:

passing the webview :

webView = [[WebView alloc] initWithFrame:frame];
[self addSubview:webView];

      

If you're wondering which rectangle you should pass as a WebView frame, read the View Programming Guide . Also, don't forget to release webView

to dealloc

(or save it to initWithFrame:isPreview:

).

Once you have a web view, you will need to load a page into it. Set a suitable long animation interval (at least a couple of seconds) and load the page in animateOneFrame

:



- (void) animateOneFrame {
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com/"]]];
}

      

You can also handle WebViewProgressFinishedNotification

and defer reloading the webview until it happens (to compensate for slow or saturated connections). You will do this with an instance variable that you set in YES

both initWithFrame:isPreview:

the notification handler method and then inspect and set NO

to animateOneFrame

:

- (void) animateOneFrame {
    if (hasFinished) {
        [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com/"]]];
        hasFinished = NO;
    }
}

      

+4


a source


[aScreenSaverView addSubview:aWebKitView];

      



But why add a UIWebView to a screen saver view when you can just make a UIWebView yourself? It is not a good idea to present view hierarchies where they are not needed, as it increases the processing required to render the interface.

0


a source


You can also not worry too much about your animation interval by calling

[self stopAnimation];

      

at the end of your method animateOneFrame

.

0


a source







All Articles