Handling app delegates and switching between views(处理应用程序委托和在视图之间切换)
问题描述
我收到有关将 *const _strong 传递给类型 id 的语义问题的警告,无论我如何更改似乎都无法修复它.
I'm getting a warning about a semantic issue pertaining to passing a *const _strong to type id and cannot seem to fix it no matter what I change.
我目前有两个视图,并且已经编写了这段代码.在 iPadSpeckViewController.m 中,这里是应该在视图之间切换的方法:
I have two views at the moment, and have written this code. In iPadSpeckViewController.m, here is the method that should switch between views:
-(IBAction) touchProducts {
ProductsViewController *controller = [[ProductsViewController alloc]
initWithNibName:@"Products" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
对于 ProductsViewController.h:
And for ProductsViewController.h:
@interface ProductsViewController : UIViewController {
id<ProductsViewControllerDelegate> delegate;
}
@property(nonatomic, retain)
IBOutlet id<ProductsViewControllerDelegate> delegate;
ProductsViewController.m 包含:
ProductsViewController.m contains:
@synthesize delegate;
但是观点并没有改变……有什么想法吗?
But the views do not switch... Thoughts?
这是确切的警告,因为它出现在controller.delegate = self;"行上在 iPadSpeckViewController.m 中:
Here is the exact warning, as it appears on the line "controller.delegate = self;" in 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协议.要消除警告,您有两种选择:
This warning is oddly worded, but it is actually just a way of telling you that the class of self (whatever that class is) fails to conform to the ProductsViewControllerDelegate protocol. To get rid of the warning, you have two choices:
在其
@interface语句中声明 self 的类(无论该类是什么),以符合 ProductsViewControllerDelegate 协议:
Declare the class of self (whatever that class is), in its
@interfacestatement, to conform to the protocol ProductsViewControllerDelegate:
@interface MyClass : NSObject <ProductsViewControllerDelegate>;
通过改变这个来抑制警告:
Suppress the warning by changing this:
controller.delegate = self;
到这里:
controller.delegate = (id)self;
委托属性的类型为 id.但自我不是.在 ARC 下,您必须明确强制转换,以便类型正式一致.(我相信这是为了让 ARC 可以绝对确定它有足够的信息来做出正确的内存管理决策.)
The delegate property is typed as id<ProductsViewControllerDelegate>. But self is not. Under ARC you must make the cast explicit, so that the types formally agree. (I believe this is so that ARC can make absolutely certain it has sufficient information to make correct memory management decisions.)
这篇关于处理应用程序委托和在视图之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:处理应用程序委托和在视图之间切换
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
