【问题标题】:How can an instance of an UITableViewCell, which was created by an instance of my ViewControllers, tell the ViewController, to do something?由我的 ViewController 的实例创建的 UITableViewCell 的实例如何告诉 ViewController 做某事?
【发布时间】:2010-01-20 21:43:49
【问题描述】:

我创建了我的 ViewController(TimeLineViewController) 的一个实例,它将被展示。这个 ViewController 包含一个 UITableView,它从我的 TableViewCell 实例中获取单元格。因此 ViewController 创建了 TableCellView 的一个实例。 TableViewCell 包含一个启用了网络链接的 UITextView。现在我想禁用 safari 打开的功能。我做了一个 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
}

现在我希望通过单击网络链接出现一个新的 ViewController(WebViewController)。问题是 TableViewCell 不能“打开”一个新的 ViewController。所以我尝试了这个: 在 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];
}

在 ViewController 中:

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

问题是他总是只重新加载TimeLineViewController,而不加载WebViewController。为什么?我该如何解决? (我知道 WebViewController 没有在我发布的代码中获取网络链接,我知道该怎么做。这不应该是问题,当我知道如何解决我的问题时。)

感谢您的帮助!如果您有任何问题,尽管问 - 抱歉我的英语不好。

更新: 我做了弗兰克提到的,但我没有工作。我创建了一个 WebViewTableCellDelegate.h:

@protocol WebViewTableCellDelegate
-(void)loadWeb;
@end

然后我在 TableViewCell 中创建了 WebViewDelegate 的实例变量:

__weak NSObject <WebViewTableCellDelegate> *_delegate;

在 .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

在我的 TimeLineViewController 中,我使用 实现了 WebViewTableCellDelegate,在创建单元格的行中,我将所有者设置为 self:

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

为什么它不起作用?没有错误警告。

【问题讨论】:

    标签: iphone objective-c xcode uitableview


    【解决方案1】:

    我会做以下事情:

    1. 创建WebViewTableCellDelegate 协议(在 Apple 网站上查看如何创建协议)。该协议应包括您的loadWeb: 方法。
    2. 让您的 TimeLineViewController 实现协议并为您的 UITextView 子类提供一个 &lt;WebViewTableCellDelegate&gt; 类型的实例变量。
    3. 创建表格视图单元格时,将其委托设置为self(TimeLineViewController)。
    4. 当有人点击链接时,拨打[delegate loadWeb:request]
    5. 让您的loadWeb: 方法接受NSURLRequest 并将其加载到Web 视图中。

    这应该可以解决问题。

    【讨论】:

      【解决方案2】:

      我认为在每个单元格中都添加UIWebView 不是一个好主意。考虑一个实现,您只需利用UITableViewDelegate 协议中的tableView:didSelectRowAtIndexPath: 方法在点击特定单元格时呈现您的UIWebView

      【讨论】:

      • 我不想这样做,因为当有人按下一行时,会打开一个 uiactionsheet。当链接被按下时,我只想要一个 WebView。
      • 考虑在单元格中为 UIActionSheet 使用附件或 UIButton?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2017-03-07
      相关资源
      最近更新 更多