开发者

back button in webview

I have a UITableView and when an item is selected I load an WebView like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSURL *url=[NSURL URLWithString:[[nameCatalog objectAtIndex:indexPath.row] valueForKey:@"url"]];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:webView];
    [webView setHidden:NO];
}

What I wanna do is implement a BackButton so when I'm on the webview I can come back to UITableView.

So I did this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSURL *url=[NSURL URLWithString:[[nameCatalog objectAtIndex:indexPath.row] valueForKey:@"url"]];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:webView];
    [webView setHidden:NO];
    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] 
                                   initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)];

    self.navigationItem.leftBarButtonItem = infoButton;
}

- (void) info_clicked:(id)sender {

    self.navigationItem.leftBarButtonItem 开发者_如何学编程= nil;
    [webView removeFromSuperView];
    }

But when I load the webview there is no button appearing.!!!!! Please help!


Probably because you are adding the webview as a subview, rather than pushing another viewController on the stack.

Also: the back button is configured by the title of the view controller. To change that to a back button you need to create a back button in the view before the view is pushed. I have two examples:

BackButton.zip which shows how the back button is configured in the init method of a view controller.

LoadWebViewTable.zip which shows how the back button is configured in the awakeFromNib method. This example follows your app more closely I think.

The point is that the back button is configured in the view that it is returning to. Also, both examples use nil-targetted actions, so you don't even need to have a callback to pop the view off the stack - it comes for free.


In order to do that, put the web view in a different view controller and use

[self.navigationController pushViewController:webViewController animated:YES];

instead of

[self.view addSubview:webView];


Maybe you should do some of this stuff in your interface. Connect a table view to your code. Have a button in your interface connected to an IBAction, hidden when you launch the app, then when your user clicks a table view cell, unhide the button.

Hope this helped!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜