views:

47

answers:

2

I am relatively new to Objective C/iPhone Development.

I am developing a simple app with two views using a Navigation controller.

The first view contains several buttons a and the second view contains a uiwebview.

The idea is that when a button is pressed it switches to the second view and the web view loads a local html file. Which html file is loaded depends on which button was pressed.

At the moment, the view successfully changes and I can specify a html file to load in the viewdidload method of the webviewcontroller. However this obviosly means that every button triggers the same html file.

Therefore I want to create a NSMutable string which can be accessed from both views(i.e set to a particular filename in the first view and then retrieved when the seoncd view loads). I have searched the internet for hours and trued to use global variables, the singleton method and accessing a variable in the appdelegate. However no matter which method I try to implement, the uiwebview always displays the same html file(which is the first file alphabetically)

Thanks for any help given. I greatly appreciate any suggestions.

A: 

I assume that the web view controller is initialised from the navigation controller, so why don't you just pass it that string when it is initialised. Then when a button is pressed it uses the correct string to instigate the web view controller.

Another idea, one I think you've tried, but I am not sure, is that the web view controller has a static method and value. When you press the button it then calls the static method on the web view controller. Then when web view controller is loading up, it calls a get static method of the value.

kuroutadori
Thanks for your swift response. This is the code which is inside the button's IBAction:if (self.myWebViewController == nil) {//initialise webViewController *viewWeb = [[webViewController alloc] initWithNibName:@"webViewController" bundle:[NSBundle mainBundle]]; self.myWebViewController = viewWeb; [viewWeb release]; } [self.navigationController pushViewController:self.myWebViewController animated:YES];How can I "pass" it the string as you mentioned?I'm sorry but I do not understand your second suggestion.Thanks again
JTreanor
Extend web view controller and implement to static methods, a getter and a setter. Have a static NSString that the getter and setter, return and manipulate. When it comes to loading the URL, set it to the static value, via getter.So in an interface the setter would be`setURL:(NSString *)url;` and the getter would be `+ (NSString *) getURL; + (void) ` then in the implementation the setter`+ (void) setURL:(NSString *)aUrl { static myUrl = aUrl }` and the getter `+ (NSString *) getURL { return myUrl }`
kuroutadori
A: 

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";.

JoostK
Thank you very much. I have added a NSString property url to the second view controller and my only question is how can I call this setUrl method from the first view controller?
JTreanor
Please see my updates answer. I got rid of the property since it's not really necessary.
JoostK
Thank you so much. That worked almost perfectly. The only issue is that when I press a button it gives me a blank uiview but if I then press back on the nav bar and press any button it shows the correct html file. Any ideas as to why this is occurring?
JTreanor
This is caused by the fact that your `UIWebView` will get initialized when the view is being loaded. The first time you push the controller, the view will be loaded. However, you call `loadUrl:` before pushing, so then the view hasn't been loaded yet, `webView` will still point to `nil`. To solve this, you can either initialize the `UIWebView` earlier, in `init`, but since you're using Interface Builder this won't work. I did not think of this issue, but to fix it you do have to use the property. I'll update my answer, just a moment.
JoostK
Thank you that worked flawlessly.
JTreanor