Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

iphone - Handling app delegates and switching between views

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.

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];
}

And for ProductsViewController.h:

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

ProductsViewController.m contains:

@synthesize delegate;

But the views do not switch... Thoughts?

EDIT: 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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  • 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;
    

    to this:

    controller.delegate = (id)self;
    

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...