我收到有关将 *const _strong
传递给类型 id
的语义问题的警告,无论我如何更改似乎都无法修复它。
我目前有两种看法,写了这段代码。在 iPadSpeckViewController.m 中,这里是应该在 View 之间切换的方法:
-(IBAction) touchProducts {
ProductsViewController *controller = [[ProductsViewController alloc]
initWithNibName:@"Products" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
对于 ProductsViewController.h:
@interface ProductsViewController : UIViewController {
id<ProductsViewControllerDelegate> delegate;
}
@property(nonatomic, retain)
IBOutlet id<ProductsViewControllerDelegate> delegate;
ProductsViewController.m 包含:
@synthesize delegate;
但 View 不会切换...想法?
编辑: 这是确切的警告,因为它出现在“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]
最佳答案
这个警告措辞奇怪,但它实际上只是告诉您 self 的类(无论该类是什么)不符合 ProductsViewControllerDelegate 协议(protocol)的一种方式。要消除警告,您有两种选择:
在其
@interface
中声明 self 的类(无论该类是什么)声明,以符合 ProductsViewControllerDelegate 协议(protocol):@interface MyClass : NSObject <ProductsViewControllerDelegate>;
通过改变这个来抑制警告:
controller.delegate = self;
为此:
controller.delegate = (id)self;
委托(delegate)属性的类型为 id<ProductsViewControllerDelegate>
.但 self 不是。在 ARC 下,您必须明确进行强制转换,以便类型正式一致。 (我相信这是为了让 ARC 可以绝对确定它有足够的信息来做出正确的内存管理决策。)
关于iphone - 处理应用程序委托(delegate)并在 View 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6364911/