Question : alloc - how to know when to create an instance of a class.

I've discovered that when working with UIWebView, you don't have to call alloc to instantiate the object from the UIWebView class.  In fact, if I do call alloc and init, and then attempt to use loadRequest, the URL will not be loaded in my object at all.
Can someone explain why?

From the Developer Docs it looks like loadRequest is an instance method.  Shouldn't I have to instantiate the object before calling one of it's methods?


Eg.  in my viewController.h

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
#import <UIKit/UIKit.h>

@interface simWebViewController : UIViewController {
	IBOutlet UIWebView *myWebView;
	NSString *myURLString;
	NSURL *myURL;
	NSURLRequest *myRequest;
}

- (IBAction) myMethod:(id)sender;

@property (nonatomic, retain) UIWebView *myWebView;
@end


and in the viewController.m

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
- (IBAction) myMethod:(id)sender {
	myURLString = [[NSString alloc] initWithString:@"http://www.apple.com"];
	myURL = [[NSURL alloc] initWithString:myURLString];
	myRequest = [[NSURLRequest alloc] initWithURL:myURL];

	//myWebView = [[UIWebView alloc] init];
	
	[myWebView loadRequest:myRequest];
}

- (void)viewDidLoad {
	[super viewDidLoad];
	[myWebView setScalesPageToFit:YES];
}



Also, why can I set the scalePageToFit property of myWebView object within viewDidLoad before my object has been instantiated?  Or does my object exist already?

Answer : alloc - how to know when to create an instance of a class.

//myWebView = [[UIWebView alloc] init];
Of course, not. You have added a webview in Interface Builder, so you have created an instance. This line when it uncommented creates new object.
About scalePageToFit: test yourself. Read this post for an info about it:
Right Scale for a UIWebView
http://iphoneincubator.com/blog/windows-views/right-scale-for-a-uiwebview
Random Solutions  
 
programming4us programming4us