Search code examples
uitableviewios6uiwebviewxcode4.5master-detail

Multiple URL Calls in UIWebView


I have a master/detail view app that I am currently building. Basically it has a list in a table for the master view, when the user selects an item, it will display a web page in a UIWebView in the detail view. There will be about 6 different websites that will needed to be loaded in response to the 6 objects in the table. I have the code working to a point, however it is loading the same web page for each item in the table. I thought it would be best to write a switch case statement to tell it which web page to load etc. I have used the following code to launch my web page in the WebView:

//create the UIWeb objects
NSString *standardForm = @"http://jeremy.atkin.id.au";
NSString *csiroForm = @"http://www.csiro.gov.au";
NSString *kpmgForm = @"http://www.kpmg.com.au";
NSString *pacbrandsForm = @"http://www.pacbrands.com.au";
NSURL *url = [NSURL URLWithString:standardForm];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_formView loadRequest:requestObj];

I then tried to put together the switch case statement, but seem to by making the wrong declarations and constantly get errors...

switch (_formNumber)
{
    case 0:
        standardForm.url = standardForm;
        break;
    case 2:
        url.URLWithString = csiroForm;
        break;
    case 3:
        url.URLWithString = kpmgForm;
        break;
    case 4:
        url.URLWithString = pacbrandsForm;
        break;
}

Any help and direction to get this working correctly would be greatly appreciated. I have never built a master/detail app before so I am thinking I jumped straight in the deep end!! Cheers


Solution

  • Add the links to one NSArray , then create url based on the tableviewCell selected index and load your webview with the request.

    1)Create a property for urlList in master

    @property (nonatomic, retain) NSArray *urlList;
    

    Then synthesis in implementation.

    @synthesize urlList;
    

    2)In your init or viewDidLoad initialize the array

    self.urlList = [[NSArray alloc]initWithObjects:@"http://jeremy.atkin.id.au",@"http://www.csiro.gov.au", @"http://www.kpmg.com.au",@"http://www.pacbrands.com.au",nil];
    

    3)On didSelectRowAtIndexPath:index create the request and load your web view.

    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:[[NSURL alloc]initWithString:[urlList objectAtIndex:indexPath]]];
    [_formView loadRequest:request];
    

    Don't forget to release (retained object) in dealloc .If using arc then change retain in property to strong.