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

时间:2011-06-15 21:58:37

标签: iphone objective-c delegates uiviewcontroller

我收到关于将*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]

3 个答案:

答案 0 :(得分:152)

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

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

    @interface MyClass : NSObject <ProductsViewControllerDelegate>;
    
  • 通过更改以下内容来取消警告:

    controller.delegate = self;
    

    到此:

    controller.delegate = (id)self;
    

委托属性的类型为id<ProductsViewControllerDelegate>。但自我不是。在ARC下,您必须明确表达,以便类型正式同意。 (我相信这是因为ARC可以绝对确定它有足够的信息来做出正确的内存管理决策。)

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

如果您只想切换视图,可能需要尝试以下代码。它对我有用。

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

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

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

至于“* const_string to type id”,虽然我不知道你要对你的应用做什么,但我认为问题是你的视图控制器中的id <ProductsViewControllerDelegate> delegate

相关问题