处理应用程序代理和在视图之间切换

Chr*_*ris 29 iphone delegates objective-c uiviewcontroller

我收到一个关于传递*const _strong到类型的语义问题的警告,id无论我改变什么,似乎无法修复它.

我目前有两个观点,并编写了这段代码.在iPadSpeckViewController.m中,这是应该在视图之间切换的方法:

-(IBAction) touchProducts {
    ProductsViewController *controller = [[ProductsViewController alloc]
            initWithNibName:@"Products" bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    controller.delegate = self;
    [self presentModalViewController:controller animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

对于ProductsViewController.h:

@interface ProductsViewController : UIViewController {
    id<ProductsViewControllerDelegate> delegate;
}
@property(nonatomic, retain)
    IBOutlet id<ProductsViewControllerDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)

ProductsViewController.m包含:

@synthesize delegate;
Run Code Online (Sandbox Code Playgroud)

但观点不转换......思考?

编辑:这是确切的警告,因为它显示在"controller.delegate = self;"行上 在iPadSpeckViewController.m中:

/Developer/iPadSpeckApp/iPadSpeckApp/iPadSpeckAppViewController.m:17:27:{17:27-17:31}: warning: passing 'iPadSpeckAppViewController *const __strong' to parameter of incompatible type 'id<ProductsViewControllerDelegate>' [3]
Run Code Online (Sandbox Code Playgroud)

mat*_*att 152

这个警告措辞奇怪,但它实际上只是告诉你自我类(无论该类是什么)无法符合ProductsViewControllerDelegate协议的一种方式.要摆脱警告,您有两种选择:

委托属性的类型为id<ProductsViewControllerDelegate>.但自我不是.在ARC下,您必须明确表达,以便类型正式同意.(我相信这是因为ARC可以绝对肯定它有足够的信息来做出正确的内存管理决策.)

  • 谢谢,这是我不知道的ARC问题.在将self指定为委托时,它修复了类似的警告. (4认同)
  • +1这是问题的正确答案. (4认同)