I am developing one application.In that first page having one UIWebView.And i create a property for that one.And i created a object for that class in another class and give the html data for that webview like as below.But it didn't show the data on UIWebView.Please tell me how to do that one.
ViewController *view=[[ViewController alloc]init];
NSString *result1 = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];
NSString *htmldata=[NSString stringWithFormat:#"<html>%#</html>",result1];
[view.web loadHTMLString:htmldata baseURL:nil];
This result1 string contain html data without html tag.That' why iam adding that tags.
Hope you had called pushViewController or popViewController to present the view or added the view object's view as a subview of the current view. Then make sure that the webview is allocated, given a frame and added as a subview of ViewController. You did not used the method initWithNibName:, therefor, if the webview is created from xib, it will not take.
Related
So basically I want a UITableView to display a bunch of websites in the RootView. Then from there, using the UINavigationController, I would like to open a different view (WebViewController) and load the URL specified in the button, in the UIWebView.
What would be the best way to do this?
EDIT: Okay so there are basically two views. The first view is the RootViewController, which is includes the UINavigationController. Basically this will include some buttons (that have custom URL's already assigned to them) that will change & load the URL in the webView using the following.
-(IBAction)linkButton:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://URL_HERE.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
The second view will be called the "CodeView" and it will include the UIWebView object dubbed webView.
So what I'm trying to accomplish is this: After clicking on one of the buttons in the RootViewController, it will switch to the CodeView and load whatever URL has been assigned to the button itself, in the UIWebView object. Then of course, have the option to return to the RootViewController via the UINavigationController, which is just common sense when it comes to using a Navigation Controller.
Well, you have to put the root view inside a navigation controller (i suupose you are already doing this) then for each button you tap you could create a new codeview with the url and push it to the navigation controller.
This way you could have your webview inside the codeview. Did i miss something?
I was wondering how I can add an int to a bundle and pass it to the next view that I am calling.
What I am trying to do is click an item on a UITableView and that will call the next .m file and pass the int that pertains to a document that I will be looking up online. This document has a specific id and without passing it to the next view I will not be able to get the list that I need to compile and show on the next UITableView.
How are you displaying the next view? Can you not just add an instance variable to the view to be called?
View2 *view = [[View2 alloc] init]];
[view setSpecificId:myId];
display the view....
I'm getting a little confused about when I should use a NIB file and when I should use code.
Here's my problem :
I have a navigation based application with a RootController and its NIB file.
The RootController's NIB file contains a TableView.
When I click on a cell I initialize a new connection with a request to load content.
When the connection has finished loading I create a new postViewController (custom) from a NIB file and I push it on the navigationController viewController stack like that :
PostViewController *postViewController = [[PostViewController alloc] initWithNibName:#"PostViewController" bundle:[NSBundle mainBundle]];
[postViewController.webView setDelegate:self];
postViewController.postContent = [[postsData objectForKey:#"post"] objectForKey:#"content"];
[self.navigationController pushViewController:postViewController animated:YES];
[PostViewController release];
Then, as you can see I try to set the rootViewController as the delegate for the webView in order to be able to intercept a click on a link and push a new ViewController on the stack. I need that new view to have the navigation bar with the back button.
Problem : looks like the setDelegate isn't working because the webView:shouldStartLoadWithRequest:navigationType never gets called !
I guess I should set the delegate in the NIB file but I have no idea how. The NIB file from PostViewController doesn't know about the RootViewController...
Here are screenshots of the NIB files :
If you need more detail just ask me.
Thanks a lot...for helping me not banging my head for another day :)
Make sure postViewController.webView isn't nil when you call setDelegate: on it. Also make sure it isn't being called anywhere else, and make sure the delegate outlet isn't connected to anything in your NIB.
A couple other comments:
It's a bit unusual to use your root view controller as the webview's delegate in a case like this. It should work, but it might make more sense to move those delegate methods down into your PostViewController. From there you can still intercept the link clicks and call [self.navigationController pushViewController:animated:].
[PostViewController release] is releasing the class object. Not a good idea. Instead you should be calling [postViewController release].
[postViewController.webView setDelegate:self]; sets the delegate for the current Controller not postViewController. Try:
[postViewController.webView setDelegate:postViewController];
And you can put this line below pushViewController:animated: then the webView should already exist.
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 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";.
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.
I am a newbie at iPhone development and have a question, which may end up helping understand this concept in general.
Basically, I have UIViewController which loads up a view with a bunch of stuff. Now I want when a user clicks on a button, to load up a different view, which happens to be a webView. Now, I want the weview to load up a different url depending on which button was pressed in the original view.
how do i go about doing this? Basically in my head I thought i could load and swap the views when the button is pressed, like so:
In
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedLink = [valuesForSection objectAtIndex:row];
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
[self.view removeFromSuperview];
[self.view insertSubview:webView atIndex:0];
}
Is this the right way of doing it? Or how do i go about doing this?
That would be the way I'd go about it, yes. Create a single UIWebView and, depending on which cell gets selected, load a NSURLRequest into the view using loadRequest:. It has the advantages of not requiring you to build separate web views for each cell, and of being asynchronous.
However, I wouldn't necessarily remove self.view from its superview whenever a cell gets clicked; rather, I'd pop up either a modal view controller (presentModalViewController:animated:) with the web view as its view, or I'd push a new controller onto a navigation controller stack (pushViewController:animated:). It's a smoother transition and will look better to the user.
Edit (in response to comment): Yes, it's better to have another controller than to just swap views. Doing removeFromSuperview and addSubview: keeps both views within the same controller, but as a result will make your code more difficult to manage (one controller will deal with two views) and have a worse user experience (there's no transition, like is built-in with a navigation controller push).
In order to do the push properly, you should:
Build a navigation controller for your existing view controller
When you need to, create a new instance of UIViewController with its view set to the UIWebView you create and load with your HTML
Push the new view controller onto the navigation controller stack from your old view controller by calling:
[self.navigationController pushViewController:newViewController animated:YES];