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.
You don’t have to use an
NSMutableStringfor this.Add a property
url, just a normalNSString, to the second view controller. Before pushing the second view controller onto the navigation stack, you set thaturlproperty to the local url you want to load.Then, in the second view controller class, implement:
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.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.
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 classWebViewController, then you can have a variable calledwebViewController. 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:
Now, in the first view controller, update
[self.myWebViewController loadUrl:@"some-local-url"];toself.myWebViewController.url = @"some-local-url";.