tags:

views:

30

answers:

2

Hi,

I'm trying to create WebView object programmatically:

WebView *webView = [WebView alloc]; // referencing error

:(

Any ideas ?

Thnx!

+1  A: 

I guess you miss initialization here, something like

WebView *webView = [[WebView alloc] init];

or using initWithFrame:frameName:groupName: initializer

Vladimir
No, I don't miss it. It should work both ways..
Андрей Игуменов
I've tried to create a webview programmatically in a sample project - it throws error if -init call is missing but seems to work fine with initialization. So I assume that must be the issue here
Vladimir
Actually, no, it shouldn't necessarily work. In Cocoa, you must always do allocation and initialization at the same time because the object returned from alloc is not guaranteed to be the same object retuned by init.
Alex
I think the question here is if we can just allocate objects and not call any initializer on them
Vladimir
@Андрей Игуменов: Absolutely wrong. You can't just use any old initializer with some objects (especially not views, which at a minimum require a frame - -initWithFrame: - in order to initialize properly). You should read up on "designated initializer" in the Cocoa documentation. It's a very important concept to understand so you can avoid issues just like this one.
Joshua Nozzi
WebView *webView = [[WebView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100) frameName:nil groupName:nil];same error..
Андрей Игуменов
May be you could provide more context? Sample with just [[webview alloc] init], setting frame and url works fine...
Vladimir
A: 

The designated initializer for WebView is -initWithFrame:frameName:groupName:. If you're not using the designated initializer, you won't get the desired results.

Joshua Nozzi