开发者

How can an instance of an UITableViewCell, which was created by an instance of my ViewControllers, tell the ViewController, to do something?

I created a instance of my ViewController(TimeLineViewController), which will be presented. This ViewController contains a UITableView, which gets the cells from a instance of my TableViewCell. So the ViewController creates an instance of the TableCellView. The TableViewCell contains a UITextView with enabled weblinks. Now I want to disable the function that safari opens. I did a subclass of the UITextView and:

- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener
{   NSLog(@"request: %@", request); //Console shows the link
}

Now I want that with a click on the weblink a new ViewController(WebViewController) appears. The problem is that the TableViewCell can´t "open" a new ViewController. So I tried this: In the TableViewCell:

- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener
{   NSLog(@"request: %@", request);
    TimeLineViewController * web = [[TimeLineViewController alloc]init];
    [web loadView];
}

And in the ViewController:

- (void)loadWeb{
    WebViewController *lust = [[WebViewController alloc] initWithNibName:nil bundle:nil];
    lust.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:lust animated:YES];
    [lust release];
}

The problem is that he always just reload the TimeLineViewController, but doesn't load the WebViewController. Why? How can I fix it? (I know that the WebViewController doesn't get the weblink in my posted code and I know how to do it. That shouldn't be the problem, when I know, how to fix my problem.)

Thanks for your help! If you have questions, just ask - Sorry for my bad English.

UPDATE: I did what Frank mention, but I doesn't work. I created a WebViewTableCellDelegate.h with:

@protocol WebViewTableCellDelegate
-(void)loadWeb;
@end

Then I created a instance variable of the WebViewDelegate in the TableViewCell:

__weak NSObject <WebViewTableCellDelegate> *_delegate;

and in the .m:

@interface UITextView (Override)
@end

@class WebView, WebFrame;
@protocol WebPolicyDecisionListener;

@implementation UITextView (Override)

- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener
{   NSLog(@"request: %@", request开发者_Go百科);
    [_delegate loadWeb];
}

@end

In my TimeLineViewController I implemented the WebViewTableCellDelegate with <> and in the line, where I create the cells, I set the owner to self:

  TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if(cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
        cell = tableCell;
    }

Why doens´t it work? There is no error-warning.


I would do the following:

  1. Create a WebViewTableCellDelegate protocol (look up how to create a protocol on Apple's site). The protocol should include your loadWeb: method.
  2. Have your TimeLineViewController implement the protocol and give your UITextView subclass an instance variable of type <WebViewTableCellDelegate>.
  3. When you're creating the table view cell, set it's delegate to self (the TimeLineViewController).
  4. When someone taps a link, call [delegate loadWeb:request]
  5. Have your loadWeb: method accept an NSURLRequest and load it into the web view.

That should do the trick.


I don't think it is a good idea to put a UIWebView in every cell. Consider an implementation where you simply utilize the tableView:didSelectRowAtIndexPath: method in the UITableViewDelegate protocol to present your UIWebView when a particular cell is tapped.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜