iphone - 处理应用程序委托(delegate)并在 View 之间切换

标签 iphone objective-c delegates uiviewcontroller

我收到有关将 *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/

相关文章:

ios - 通过协议(protocol)更改委托(delegate)的属性

ios - Swift 泛型类作为委托(delegate)

asp.net-mvc - 如何创建和调用异步版本的缓存 GetOrSet 方法?

css - iphone 邮件无法加载谷歌字体 Oswald

iphone - 在没有支持变量的情况下在 Xcode 调试器中查看合成属性的值

iphone - IOS设置 View 中的配置

iOS Typhoon DI 框架替代 objective-c 中的单例

objective-c - 使用 Sparkle Framework 更新时显示 .dmg EULA

ios - NSUserDefaults 值定期丢失

iphone - 为 Xcode 4 创建/使用 Singleton NSMutableArray 的正确方法