2012-06-20 52 views
4

我在我的代碼下面的警告(4.3的XCode/iOS的4/5) -分配到 'ID <MFMessageComposeViewControllerDelegate>' 從不兼容的類型 'MainViewController *'

分配到從不兼容的類型的 'id'「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]; 
} 
} 

特別是這一行 -

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:@"[email protected]", @"[email protected]", 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 
+1

@interface MainViewController:TrackedUIViewController

+0

上面解決了我的問題,如下面的海報建議,非常感謝。複製並粘貼應用內短信和電子郵件的不同示例稍微有點丟失。非常感謝 - rob –

回答

8

您正在使用:

MFMailComposeViewControllerDelegate 

,它應該是:

MFMessageComposeViewControllerDelegate 

更改位置:

@interface MainViewController : TrackedUIViewController <MFMessageComposeViewControllerDelegate> { 
+2

我相信他也應該保留'MFMailComposeViewControllerDelegate',因爲他有'mailer.mailComposeDelegate = self;'在下面的' - (IBAction)openMail:(id)sender' –

+0

在這種情況下,而不是**切換**一個換另一個,他應該**都使用**。順便說一句,很好,我沒有閱讀他的所有代碼,只是.h文件。 – Peres

+0

這幫了我,謝謝先生 – justinkoh

0

沒有與此庫的工作,但我所看到的,YOUE MainViewController是MF * 郵件 * ComposeViewControllerDelegate ,但是將其設置爲MF * 消息 * ComposeViewControllerDelegate。

3

在頭文件中實現UINavigationControllerDelegate。

相關問題