I have been working around one small project which needs a similar GUI as that of SplitViewController. Now what i have done is, I have two UIViewControllers namely MasterViewController and DetailViewController which are added as subview of ViewController
The code is shown below for ViewController.m
- (void)viewDidLoad
{
UIScreen *screen = [UIScreen mainScreen];
CGRect fullScreenRect = screen.bounds; // always implicitly in Portrait orientation.
width = fullScreenRect.size.width;
height = fullScreenRect.size.height;
detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
masterViewController = [[[MasterViewController alloc]initWithNibName:@"MasterViewController" bundle:nil] autorelease];
masterViewController.view.frame = CGRectMake(0, 0, 299, height);
detailViewController.view.frame = CGRectMake(300, 0, width-300, height);
[self.view addSubview:masterViewController.view];
[self.view addSubview:detailViewController.view];
}
Now in the MasterViewController didLoad method i have added as a subview MasterTableViewController…
MasterTableViewController class looks like this…
@interface MasterTableViewController : UITableViewController
{
DetailViewController *detailViewController;
}
All the functions related to UITableViewController are already made… and i have also added the functionality to add URL from user and store it in the MasterTableView.
Now in the following function I am trying to call loadWebView function of DetailViewController with the required string. The call is made perfectly to the function.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *urlString = [_objects objectAtIndex:indexPath.row];
detailViewController = [[DetailViewController alloc]init];
[detailViewController loadWebView:urlString];
}
In the loadWebView method of DetailViewController i have the following implementation..
-(void) loadWebView: (NSString *) urlToLoad
{
self.webView = [[[UIWebView alloc]
initWithFrame:CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height)] autorelease];
[self.webView setDelegate:self];
NSLog(@"SELF->WEBVIEW: %@",self.webView);
NSURL *url = [NSURL URLWithString:urlToLoad];
NSLog(@"loading url %@ %@", urlToLoad, self.webView);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
I have also implemented the following function just to make sure that my Webview is working..
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"webViewDidStartLoad");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"webViewDidFinishLoad");
}
Now when i run my program the log prints all these things…
2012-10-15 20:04:21.621 SyneBlogApp[2320:c07] device width height 768.000000 1024.000000
2012-10-15 20:04:21.630 SyneBlogApp[2320:c07] calling viewDidLoad for detailView
2012-10-15 20:04:21.636 SyneBlogApp[2320:c07] /Users/Library/Application Support/iPhone Simulator/5.1/Applications/B846EDA4-49CF-4DD3-AAB3-FF8BC19C65DA/Documents/Data.plist:
2012-10-15 20:04:22.942 SyneBlogApp[2320:c07] calling viewDidLoad for detailView
2012-10-15 20:04:22.954 SyneBlogApp[2320:c07] SELF>WEBVIEW: <UIWebView: 0x6a94510; frame = (0 45; 768 1004); layer = <CALayer: 0x6a94930>>
2012-10-15 20:04:22.955 SyneBlogApp[2320:c07] loading url http://wordpress.com <UIWebView: 0x6a94510; frame = (0 45; 768 1004); layer = <CALayer: 0x6a94930>>
2012-10-15 20:04:22.994 SyneBlogApp[2320:c07] webViewDidStartLoad
2012-10-15 20:04:26.828 SyneBlogApp[2320:c07] webViewDidFinishLoad
According to log my webview loads the data perfectly, but when i have a look at the emulator i get a white screen. That is the content is not shown up…
Now just to make sure that nothing is wrong with webview i tried in DetailViewController viewDiDloadMethod the following which works and shows up the google page…
- (void)viewDidLoad
{
self.webView = [[[UIWebView alloc]
initWithFrame:CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height)] autorelease];
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.view addSubview:self.webView];
[super viewDidLoad];
NSLog(@"calling viewDidLoad for detailView");
}
Now I seriously have no idea of what is going wrong. Even after all the things are working up well in log still the webpage content is not shown up.. Guys if you have any clue of what is going wrong please help…
This…
…creates a new
DetailViewControllerinstead of using the one you already created and made part of your display in the first code segment you showed. You are therefore loading up your web view for a controller/view that isn’t on screen.