You don't have to use an NSMutableString
for this.
Add a property url
, just a normal NSString
, to the second view controller. Before pushing the second view controller onto the navigation stack, you set that url
property to the local url you want to load.
Then, in the second view controller class, implement:
- (void)setUrl:(NSString *)newUrl {
if(url != newUrl){
[url release];
url = [newUrl retain];
// [webView load.... load the new url in the webView.
}
}
This may be beyond you current knowledge, it contains some vital Objective-C code, like properties, setters and memory management, but just test it out and see what you can do to make it work.
EDIT You don't even need this property. Just implement a method like loadUrl:
in the webViewController.
- (void)loadUrl:(NSString *)url {
// [webView load.... load the URL in the webView
}
Then, before pushing the webViewController, call this method with the URL you want to load. Avoids a property and gets rid of all the additional memory management code.
if (self.myWebViewController == nil) {
//initialise
webViewController *viewWeb = [[webViewController alloc] initWithNibName:@"webViewController" bundle:[NSBundle mainBundle]];
self.myWebViewController = viewWeb;
[viewWeb release];
}
[self.myWebViewController loadUrl:@"some-local-url"];
[self.navigationController pushViewController:self.myWebViewController animated:YES];
As a note, a good practice is to capitalize each classname. Now you have a class webViewController
, but in Cocoa it's common to name the class WebViewController
, then you can have a variable called webViewController
. Also, you can clearly see whether you are dealing with object (variable) or just a class.
EDIT Updated
So I forgot about the fact that the webView will not have been loaded until your view gets loaded. To solve it you'll have to reintroduce the property I talked about earlier. This time some adjustments are made. In your webViewController class, add the following code:
- (void)viewDidLoad {
// viewDidLoad gets automatically called once the view has loaded. Here we want to load the webView.
[self loadWebView];
}
- (void)setUrl:(NSString *)newUrl {
if(url != newUrl){
[url release];
url = [newUrl retain];
// Update the webView with the newly set URL. This will not do anything if the view hasn't been loaded, since webView will still be nil. That's why we call loadWebView again when the view gets loaded, in viewDidLoad.
[self loadWebView];
}
}
- (void)loadWebView {
// Here you'll have to load the url. You can access it using `self.url`.
//[webView load...
}
Now, in the first view controller, update [self.myWebViewController loadUrl:@"some-local-url"];
to self.myWebViewController.url = @"some-local-url";
.