Search code examples
iosobjective-cuitableviewmfmailcomposeviewcontroller

Send Mail TableView NSArray


This is my NSObject code;

Task.h

#import <Foundation/Foundation.h>

@interface Task : NSObject

@property (nonatomic,strong) NSString *name;
@property (nonatomic,assign) BOOL done;

-(id)initWithName:(NSString *)name done:(BOOL)done;

@end

Task.m

#import "Task.h"

@implementation Task

@synthesize name = _name;
@synthesize done = _done;

-(id)initWithName:(NSString *)name done:(BOOL)done {
    self = [super init];

    if (self) {
        self.name = name;
        self.done = done;
    }
    return self;
}

This is my send mail code

Task *task = [[Task alloc]init];
        MFMailComposeViewController *sendmail = [[MFMailComposeViewController alloc]init];
        [sendmail setMailComposeDelegate:self];
        NSString *message = [_tasks addObject:task]; // Error is here.
        [sendmail setMessageBody:message isHTML:NO];
        [sendmail setSubject:@"Test"];
        [self presentViewController:sendmail animated:YES completion:nil];

I don't know, How to do it. I just want to send the list with mail. Where is my mistake? And How can I fix this?


Tasklistviewcontroller.m

@synthesize tasks = _tasks;

I am transferring from the tasks table view.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *NotDoneCellIdentifier = @"NotDoneTaskCell";
    static NSString *DoneCellIdentifier = @"DoneTaskCell";

    Task *currentTask = [self.tasks objectAtIndex:indexPath.row];

    NSString *cellIdentifier = currentTask.done ? DoneCellIdentifier : NotDoneCellIdentifier;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = currentTask.name;

    return cell;
}

Solution

  • I don't know what is exactly wrong in your code since you don't supply an error and I'm not that proficient yet in Objective-C.

    I suspect it is because you reference to "_tasks" which I don't see other code for creating that Class.

    NSString *message = [_tasks addObject:task];
    

    Another problem is that you're using task as input object for the array, but it probably doesn't contains something.

    You probably wan't something like this:

     Task *task = [[Task alloc] initWithName:@"Task 1"];
     NSString *message = [[NSString alloc] initWithFormat:@"Task name is %@", task.name];
    

    Also I'm guessing you haven't posted your complete code.

    You also forgot to include the right framework for mailing in-app in your header file:

    #import <MessageUI/MFMailComposeViewController.h>
    

    Don't forget to also add the framework it to your project!

    By the way, you can remove the two lines with synthesize, the compiler does this automatically these days. Nice isn't it?