【问题标题】:Handling app delegates and switching between views处理应用程序委托和在视图之间切换
【发布时间】:2011-09-15 22:24:14
【问题描述】:

我收到有关将*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];
}

对于 ProductsViewController.h:

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

ProductsViewController.m 包含:

@synthesize delegate;

但是观点并没有改变……有什么想法吗?

编辑: 这是确切的警告,因为它出现在“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]

【问题讨论】:

  • 您确定您的 ProductsViewController.h 文件“了解” ProductsViewControllerDelegate 协议吗?换句话说,你的头文件开头有#import "ProductsViewControllerDelegate.h" 语句吗?
  • 是的。 iPadSpeckViewController.h 和 ProductsViewController.m 都导入了 ProductsViewController.h
  • 请发布您遇到的错误的确切文本。另外,标记错误发生的行,这样我们就可以准确地看到编译器在抱怨什么。

标签: iphone objective-c delegates uiviewcontroller


【解决方案1】:

这个警告措辞奇怪,但它实际上只是告诉你 self 的类(无论那个类是什么)不符合 ProductsViewControllerDelegate 协议的一种方式。要消除警告,您有两种选择:

  • 在其@interface 语句中声明 self 的类(无论该类是什么),以符合 ProductsViewControllerDelegate 协议:

    @interface MyClass : NSObject <ProductsViewControllerDelegate>;
    
  • 通过改变这个来抑制警告:

    controller.delegate = self;
    

    到这里:

    controller.delegate = (id)self;
    

委托属性的类型为id&lt;ProductsViewControllerDelegate&gt;。但自我不是。在 ARC 下,您必须明确强制转换,以便类型正式一致。 (我相信这是为了让 ARC 可以绝对确定它有足够的信息来做出正确的内存管理决策。)

【讨论】:

  • 谢谢,这是我不知道的 ARC 问题。它修复了将 self 分配为委托时的类似警告。
  • (id)self 帮助了我,但奇怪的是对于其他一些代表来说,这个警告没有弹出,即使在同一个实现文件中也是如此。无论如何,现在我知道如果出现警告该怎么办!
  • @Heartinpiece - 查看代表的声明。一些代表只是简单地输入 id,所以他们可以接受任何东西。一些委托的类型为id&lt;SomeProtocol&gt;,因此参数必须采用 SomeProtocol(或者您可以通过转换为id 来放弃类型检查)。
【解决方案2】:

当我尝试将 UINavigationController 的委托设置为实现错误协议的对象(UINavigationBarDelegate 而不是 UINavigationControllerDelegate)时,遇到了同样的错误。这可能是一个简单的错字。

【讨论】:

    【解决方案3】:

    如果您只想切换视图,您不妨试试下面的代码。它对我有用。

    ProductsViewController *controller = [[ProductsViewController alloc] initWithNibName:@"Products" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
    

    我用它来让我的应用程序中的主菜单切换到游戏。

    如果你想要一个特殊的动画(我想我看到了 Cross Dissolve?),但是,我不知道。我将尝试挖掘文档以查看,我会告诉你我发现了什么。

    至于“*const_string to type id”,虽然我不知道你想用你的应用程序做什么,但我认为问题在于你的视图控制器中的id &lt;ProductsViewControllerDelegate&gt; delegate

    【讨论】:

    • 我没有使用navigationController(或者只是没有)所以它不明白
    猜你喜欢
    • 1970-01-01
    • 2011-09-27
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多