从不兼容的类型“MainViewController *”分配给“id<MFMessageComposeViewControllerDelegate>”

Posted

技术标签:

【中文标题】从不兼容的类型“MainViewController *”分配给“id<MFMessageComposeViewControllerDelegate>”【英文标题】:Assigning to 'id<MFMessageComposeViewControllerDelegate>' from incompatible type 'MainViewController *' 【发布时间】:2012-06-20 07:38:58 【问题描述】:

我的代码中有以下警告 (XCode 4.3 / ios 4/5) -

从不兼容的类型'MainViewController *'分配给'id'

在本节提出警告-

- (IBAction)sendInAppSMS:(id)sender

MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])

    controller.body = @"A test message from http://www.macoscoders.com";
    controller.recipients = [NSArray arrayWithObjects:@"9880182343",nil];
    controller.messageComposeDelegate = self;
    [self presentModalViewController:controller animated:YES];


尤其是这一行 -

controller.messageComposeDelegate = self;

对于我的代码有什么问题有点困惑。谷歌搜索警告我找到了一些参考资料,但我很难理解答案。

任何指针/解释将不胜感激。

最好的问候

抢劫

我的完整 .h 文件 -

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <MessageUI/MessageUI.h>
#import "EasyTracker.h"

@interface MainViewController : TrackedUIViewController <MFMailComposeViewControllerDelegate> 

IBOutlet UIView *volumeSlider;  
AVPlayer *radiosound;

IBOutlet UIButton *playpausebutton;

IBOutlet UIActivityIndicatorView *activityIndicator;
NSTimer *timer;



@property(nonatomic, retain) AVPlayer                   *radiosound;
@property(nonatomic, retain) UIButton                   *playpausebutton;

- (void)updatebuttonstatus;

- (void)playCurrentTrack;
- (void)pauseCurrentTrack;
- (IBAction)playbutton;
- (IBAction)openMail:(id)sender;
- (IBAction)sendInAppSMS:(id)sender;

@end

我的 .m 文件中的亮点 -

#import "MainViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <MediaPlayer/MediaPlayer.h>
#import "radio99AppDelegate.h"

@implementation MainViewController



- (IBAction)sendInAppSMS:(id)sender

MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])

    controller.body = @"A test message from http://www.macoscoders.com";
    controller.recipients = [NSArray arrayWithObjects:@"9880182343",nil];
    controller.messageComposeDelegate = self;
    [self presentModalViewController:controller animated:YES];



- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result

switch (result) 
    case MessageComposeResultCancelled:
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SMSTester" message:@"User cancelled sending the SMS"
                                                      delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    
        break;
    case MessageComposeResultFailed:
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SMSTester" message:@"Error occured while sending the SMS"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    
        break;
    case MessageComposeResultSent:
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SMSTester" message:@"SMS sent successfully..!"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    
        break;
    default:
        break;


[self dismissModalViewControllerAnimated:YES];


- (IBAction)openMail:(id)sender 

if ([MFMailComposeViewController canSendMail])

    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

    mailer.mailComposeDelegate = self;

    [mailer setSubject:@"A Message from MobileTuts+"];

    NSArray *toRecipients = [NSArray arrayWithObjects:@"fisrtMail@example.com", @"secondMail@example.com", nil];
    [mailer setToRecipients:toRecipients];

    UIImage *myImage = [UIImage imageNamed:@"mobiletuts-logo.png"];
    NSData *imageData = UIImagePNGRepresentation(myImage);
    [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"mobiletutsImage"]; 

    NSString *emailBody = @"Have you seen the MobileTuts+ web site?";
    [mailer setMessageBody:emailBody ishtml:NO];

    [self presentModalViewController:mailer animated:YES];

    [mailer release];

else

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" 
                                                    message:@"Your device doesn't support the composer sheet" 
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles: nil];
    [alert show];
    [alert release];




#pragma mark - MFMailComposeController delegate

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
   
switch (result)

    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved: you saved the email message in the Drafts folder");
        break;
    case MFMailComposeResultSent:
        NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send the next time the user connects to email");
        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail failed: the email message was nog saved or queued, possibly due to an error");
        break;
    default:
        NSLog(@"Mail not sent");
        break;


[self dismissModalViewControllerAnimated:YES];


@end

【问题讨论】:

@interface MainViewController : TrackedUIViewController 以上解决了我的问题,正如下面的海报所建议的那样,非常感谢。复制和粘贴应用内短信和电子邮件的不同示例有点迷失了。再次感谢 - 抢 【参考方案1】:

您正在使用:

MFMailComposeViewControllerDelegate

它应该在哪里:

MFMessageComposeViewControllerDelegate

在此处更改:

@interface MainViewController : TrackedUIViewController <MFMessageComposeViewControllerDelegate> 

【讨论】:

我相信他也应该保留MFMailComposeViewControllerDelegate,因为他在- (IBAction)openMail:(id)sender下面有mailer.mailComposeDelegate = self; 在这种情况下,他应该同时使用两个,而不是切换。顺便说一句,很好,我没有阅读他的所有代码,只是 .h 文件。【参考方案2】:

在你的头文件中实现 UINavigationControllerDelegate。

【讨论】:

【参考方案3】:

不适用于此库,但正如我所见,您的 MainViewController 是 MF*Mail*ComposeViewControllerDelegate,但您将其设置为 MF*Message*ComposeViewControllerDelegate .

【讨论】:

以上是关于从不兼容的类型“MainViewController *”分配给“id<MFMessageComposeViewControllerDelegate>”的主要内容,如果未能解决你的问题,请参考以下文章

我收到错误消息,“从不兼容的类型“gameViewController”分配给“id avaudioplayerdelegate””[关闭]

指针问题:从不兼容的类型“int”分配给“int *”

从不兼容的类型“MainViewController *”分配给“id<MFMessageComposeViewControllerDelegate>”

iOS SKSpriteNode 错误:从不兼容类型“CGRect”(又名“struct CGRect”)分配给“skspritenode *const __strong”

错误:从不兼容的指针类型[-Werror = incompatible-pointer-types]初始化。读取= dev_read,Linux

指针类型不兼容??奇怪的