开发者

Problems with creating and using of delegate-protocols

I have the problem that I have created a delegate protocol, but the necessary methods are not executed, although I have implemented the protocol in my header file. Here are the detailed explanation:

I created an instance of my ViewController (TimeLineViewController), which will be displayed. This ViewController contains a UITableView, which in turn receives the individual Cells / Rows from one instance of my TableViewCell. So the ViewController creates an instance of TableCellView. The TableViewCell contains a UITextView, which contains web links. Now I want, that not safari opens the links, but my own built-in browser. Unfortunately TableViewCell can not open a new ViewController with a WebView, so I decided to create a delegate protocol.

The whole thing looks like this:

WebViewTableCellDelegate.h:

@protocol WebViewTableCellDelegate
-(void)loadWeb;
@end

Then I created a instance WebViewDelegate in the TableViewCell:

  id <WebViewTableCellDelegate开发者_如何学Python> _delegate;

In the .m of the TableViewCell:

 @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
- (void)setDelegate:(id <WebViewTableCellDelegate>)delegate{
    _delegate = delegate;}

And in my TimeLineViewController I implemented the protocol with <> and the loadWeb-metode:

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

And when the instance of the TableViewCell will be created in the TimelineViewController:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";
    MyIdentifier = @"tableCell";
    TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if(cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
        cell = tableCell;}

    [cell setDelegate:self];
//…
}

It is the first time I created a own delegate-protocol, so maybe there are stupid mistakes. Also I´m learnung Objective-C and programming generally only for 4 weeks.

Thanks for your help!

EDIT: I think i found the problem, but I dont know how to resolve it. I try to use [_delegate loadWeb]; in the subclass of the UITextView (because that is the only way i can react on the weblinks) and the subclass can´t use [_delegate loadWeb];. I tried this in a other methode and it worked.


Your first problem is that:

id <WebViewTableCellDelegate> *_delegate;

should be:

id <WebViewTableCellDelegate> _delegate;

The id type is already a pointer reference to an instance.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜