简体   繁体   中英

Assigning incompatible type to a delegate

Environment: Xcode Version 6.0 (6A254o) running iOS 8.0 SDK

I'm attempting to assign the main UIViewController (presenting controller) as the delegate to the presented popup UIViewController:

#import "MainViewController.h"
#import "OrangeViewController.h"
#import "MessageViewController.h"

@interface MainViewController () <UIAdaptivePresentationControllerDelegate>{
    OrangeViewController *orangeVC;
    MessageViewController *msgVC;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *helpBarItem;

@end

    @implementation MainViewController
    ...    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIPopoverPresentationController *msgPC = [msgVC popoverPresentationController];
        msgPC.barButtonItem = self.helpBarItem;
        msgPC.delegate = self; <-- incompatible type
        msgPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
        msgVC.preferredContentSize = CGSizeMake(260.0, 240.0);
    }

However I'm getting an incompatible assignment for the delegate:

在此处输入图片说明

...MainViewController.m:40:20: Assigning to 'id' from incompatible type 'MainViewController *const __strong'

How do I change the MainViewController (self) from 'const-strong' to what's compatible with the UIPopover view controller (or via the best solution)?

I had accidentally used the wrong declaration.
I noticed this when I had edited by post with the added screen shot of the wrong statement.

The correct :

在此处输入图片说明

...verses the INCORRECT

<UIAdaptivePresentationControllerDelegate>

In your MainViewController 's interface file (or in an extension in your implementation file) you should declare that MainViewController implements the UIPopoverPresentationControllerDelegate protocol.

You can do something like this in your MainViewController.m:

@interface MainViewController () < UIPopoverPresentationControllerDelegate >
@end

or, in MainViewController.h, you can include that < UIPopoverPresentationControllerDelegate > after the main @interface MainViewController . I usually put the protocol declaration in the header only if I want external classes to know that my class implements the protocol.

Edit: In your updated question it looks like you already had the right idea with declaring that MainViewController implemented a protocol, but you didn't use the right one. MainViewController should implement UIPopoverPresentationControllerDelegate , not UIAdaptivePresentationControllerDelegate . UIPopoverPresentationControllerDelegate inherits from UIAdaptivePresentationControllerDelegate but that delegate property you're assigning wants UIPopoverPresentationControllerDelegate specifically, not UIAdaptivePresentationControllerDelegate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM