سؤال

I am having a strange problem that I don't understand, and therefore don't know where to start debugging.

FIRSTLY, THE REQUIREMENT

I have a UISplitView. The Master view contains a table which loads different views into the Detail view. For one table cell, I want a website loaded full screen. Therefore, rather than hiding the Master view and resizing the Detail view, I am presenting a view controller. It is with this that I have the problem.

THE CODE

Below is the code i currently have - greatly stripped down to avoid clutter

-- MASTER VIEW --

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProjectWebViewController *detailView=[[ProjectWebViewController alloc] init];
    UIViewController  *localdetailViewController = detailView;

    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:localdetailViewController];
    [self presentViewController:navController animated:YES completion:nil];
}

I then have the following in the presented view controller

-- PRESENTED VIEW CONTROLLER --

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];

    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
   [webView loadRequest:request];

    [[self view] addSubview:webView];

   UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:@selector(closeSelf)];
    [[self navigationItem] setLeftBarButtonItem:backBtn];
}

-(void)closeSelf
{
    [[self navigationController] dismissViewControllerAnimated:YES completion:nil];
}

THE PROBLEM

The above works well with one exception. The back button i added to the navigation bar does nothing when clicked. However, if i click any white space on the web page that is loaded, the back button works. I have tested this many times. Even if I click some white space and then click a link on the web page, the back button stops working again until more white space is clicked on the current web page.

Can anyone point me in right direction?

Thanks

هل كانت مفيدة؟

المحلول

UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:@selector(closeSelf)];

target should be set to self instead of nil

UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(closeSelf)];

نصائح أخرى

I would recommend since the page is taking over the entire screen instead of being pushed as a detail view controller in the split view to add it to the screen as a UIView and add that to parent so you don't need to worry about the entire navigation controller. Then you could simply call [self.view removeFromSuperview]; to dismiss the webview.

Inside of the didSelectRowForIndexPath call createModalWebView for your row selection; Below is a bit of untested code that should work based on your provided code.

- (void)createModalWebView{
        UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];

        NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
       [webView loadRequest:request];

        //YOUR MASTERVIEW 
        [[self view] addSubview:webView];

        UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:@selector(closeSelf)];
        [[self navigationItem] setLeftBarButtonItem:backBtn];
    }

-(void)closeSelf
{
    //REMOVE THE WEBVIEW FROM SCREEN
    [[self view] removeFromSuperview];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top