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

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 @interface statement, 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.)

    这篇关于处理应用程序委托和在视图之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
    How to stop UIBarButtonItem text from truncating?(如何阻止UIBarButtonItem文本被截断?)
    java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
    Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
    iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
    Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)