代码之家  ›  专栏  ›  技术社区  ›  Chris

为什么我的自定义iPhone代理无法工作?

  •  1
  • Chris  · 技术社区  · 16 年前

    我只是尝试构建一个简单的委托,它可以成功编译,但现在似乎什么都做不了。 请查看下面的代码,FirstViewController和SecondViewController都有单独的xib文件,SecondViewController有一个连接到myMethod的按钮。

    几个月来,我一直在学习objective c和iPhone SDK,我一直在使用一本书和lynda.com教程来辅助,我实际上使用xcode模板复制了一个实用程序应用程序的大部分代码,我相信我理解这里发生了什么,但我是否遗漏了一些东西,因为按钮没有响应。

    如果您需要更多信息,请询问。

    克里斯

    #import <UIKit/UIKit.h>
    
    @protocol SecondViewControllerDelegate;
    
    
    @interface SecondViewController : UIViewController {
        id <SecondViewControllerDelegate> delegate;
    }
    
    @property (nonatomic, assign) id <SecondViewControllerDelegate> delegate;
    
    
    -(IBAction) myMethod;
    
    @end
    
    @protocol SecondViewControllerDelegate
    
    -(void)viewControllerDidFinish:(SecondViewController *)controller;
    
    @end
    

    @implementation SecondViewController
    
    @synthesize delegate;
    
    -(IBAction) myMethod
    {
        [self.delegate viewControllerDidFinish:self];
    }
    //
    @end
    

    @interface FirstViewController : UIViewController <SecondViewControllerDelegate>{
    //
    @end
    

    FirstViewController.m

    @implementation FirstViewController
    
    -(void)viewControllerDidFinish:(SecondViewControllerDelegate *)controller
    {
        NSLog(@"delegate is being used");
    }
    //
    @end
    

    编辑 -这是一篇老文章,但为了澄清,我只是忘记在FirstViewController中分配SecondViewController委托

    2 回复  |  直到 14 年前
        1
  •  1
  •   malaki1974    15 年前

    如何分配SecondViewController的委托属性? 这是我错过的最后一部分。。。

        2
  •  0
  •   Ben Zotto sberry    16 年前

    1. 你的IBAction方法 myMethod -(IBAction)myMethod:(id)sender 在责怪委托模式之前,请确保调用了myMethod(设置断点,请参阅)。

    2. 您的委托方法似乎被声明为将委托类型作为参数,而不是您希望传递给它的viewController。这可能会被id类型所掩盖,但我认为您可能需要重新整理声明。