简体   繁体   English

由我的ViewController实例创建的UITableViewCell实例如何告诉ViewController做某事?

[英]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. 我创建了ViewController(TimeLineViewController)的实例,该实例将被呈现。 This ViewController contains a UITableView, which gets the cells from a instance of my TableViewCell. 这个ViewController包含一个UITableView,它从我的TableViewCell实例获取单元格。 So the ViewController creates an instance of the TableCellView. 因此,ViewController将创建TableCellView的实例。 The TableViewCell contains a UITextView with enabled weblinks. TableViewCell包含具有启用的Web链接的UITextView。 Now I want to disable the function that safari opens. 现在我要禁用Safari打开的功能。 I did a subclass of the UITextView and: 我做了UITextView的子类,并且:

- (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. 现在,我希望通过单击Web链接来显示一个新的ViewController(WebViewController)。 The problem is that the TableViewCell can´t "open" a new ViewController. 问题在于TableViewCell无法“打开”新的ViewController。 So I tried this: In the TableViewCell: 所以我尝试了这个:在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: 并在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. 问题在于,他始终只是重新加载TimeLineViewController,但不加载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.) (我知道WebViewController在我发布的代码中没有Web链接,而且我知道该怎么做。当我知道如何解决问题时,这不应该是问题。)

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: 我用以下命令创建了一个WebViewTableCellDelegate.h:

@protocol WebViewTableCellDelegate
-(void)loadWeb;
@end

Then I created a instance variable of the WebViewDelegate in the TableViewCell: 然后,我在TableViewCell中创建了WebViewDelegate的实例变量:

__weak NSObject <WebViewTableCellDelegate> *_delegate;

and in the .m: 并在.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);
    [_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: 在我的TimeLineViewController中,我用<>实现了WebViewTableCellDelegate,并在创建单元的那一行中,将所有者设置为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). 创建一个WebViewTableCellDelegate协议(在Apple网站上查找如何创建协议)。 The protocol should include your loadWeb: method. 该协议应包含您的loadWeb:方法。
  2. Have your TimeLineViewController implement the protocol and give your UITextView subclass an instance variable of type <WebViewTableCellDelegate> . 让您的TimeLineViewController实现该协议,并为您的UITextView子类提供一个<WebViewTableCellDelegate>类型的实例变量。
  3. When you're creating the table view cell, set it's delegate to self (the TimeLineViewController). 创建表格视图单元格时,请将其委托设置为self (TimeLineViewController)。
  4. When someone taps a link, call [delegate loadWeb:request] 当有人点击链接时,请致电[delegate loadWeb:request]
  5. Have your loadWeb: method accept an NSURLRequest and load it into the web view. 让您的loadWeb:方法接受一个NSURLRequest并将其加载到Web视图中。

That should do the trick. 这应该够了吧。

I don't think it is a good idea to put a UIWebView in every cell. 我认为在每个单元格中放置UIWebView不是一个好主意。 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. 考虑一个实现,在该实现中,您可以简单地利用UITableViewDelegate协议中的tableView:didSelectRowAtIndexPath:方法来显示特定的单元格时显示的UIWebView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM