委托错误,我不知道为什么

时间:2013-10-14 02:40:27

标签: ios objective-c delegates

我有一个错误,我不明白为什么? 我将发布所有代码,因为我在过去的3个小时里一直在看这个并放弃了。

// // AssignmentListViewController.h

//  AssignmentAppTwo
//
//  Created by Abdel Elrafa on 10/11/13.
//  Copyright (c) 2013 Abdel Elrafa. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AddEditViewController.h"

@interface AssignmentListViewController : UITableViewController

@property(strong,nonatomic) AddEditViewController *vc;



@end

// // AssignmentListViewController.m

//  AssignmentAppTwo
//
//  Created by Abdel Elrafa on 10/11/13.
//  Copyright (c) 2013 Abdel Elrafa. All rights reserved.
//

#import "AssignmentListViewController.h"
#import "AssignmentInfo.h"
#import "AddEditViewController.h"

@interface AssignmentListViewController (){
    AssignmentInfo *classObj2;

}

@end

@implementation AssignmentListViewController
@synthesize vc = _vc;





- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)assignmentSaved:(AssignmentInfo *)classObj
{
    // [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"%@",classObj.description);
    classObj2 = classObj;
    [self.tableView reloadData];
}



-(AddEditViewController *)vc
{
    if (!_vc) {
        _vc = [[AddEditViewController alloc]init];
    }

    return _vc;
}


- (void)viewDidLoad
{
    [super viewDidLoad];







    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([segue.identifier isEqualToString:@"addAssignment"]) {
        self.vc.delegate = (id)self;
    }
}


@end

// // AddEditViewController.h

//  AssignmentAppTwo
//
//  Created by Abdel Elrafa on 10/11/13.
//  Copyright (c) 2013 Abdel Elrafa. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AssignmentInfo.h"

@protocol AddAssignmentDelegate;


@interface AddEditViewController : UITableViewController

<

UITextFieldDelegate

>
{
    IBOutlet UIDatePicker *dateTimePicker;
}

@property (nonatomic, strong) IBOutlet UITextField *className;
@property (nonatomic, strong) IBOutlet UITextField *assignmentTitle;
@property (nonatomic, strong) IBOutlet UITextField *assignmentDescription;
@property (nonatomic, strong) IBOutlet UISwitch *procrastinationNotificationSwitch;
@property (nonatomic,strong)AssignmentInfo *assignmentInfo;
@property (nonatomic, weak)  id <AddAssignmentDelegate> delegate;


@end

@protocol ViewControllerDelegate <NSObject>

-(void)assignmentSaved:(AssignmentInfo *)classObj;


@end

// // AddEditViewController.m

//  AssignmentAppTwo
//
//  Created by Abdel Elrafa on 10/11/13.
//  Copyright (c) 2013 Abdel Elrafa. All rights reserved.
//

#import "AddEditViewController.h"
#import "AssignmentInfo.h"
#import "AssignmentListViewController.h"

@interface AddEditViewController ()

@end

@implementation AddEditViewController
@synthesize delegate = _delegate;





-(void)dismissKeyboard {
    [self.assignmentDescription resignFirstResponder];
    [self.assignmentTitle resignFirstResponder];
    [self.className resignFirstResponder];

}
- (IBAction)addAssignmentCancelButtonPressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void) presentMessage:(NSString *)totalStrings {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Class Stuff" message:totalStrings delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];

}

-(void) procrastinationNotificationSwitchOnOrOff {

    if (_procrastinationNotificationSwitch.on) {
        self.assignmentInfo.notifcationStatus = YES;

    }
    else {
        self.assignmentInfo.notifcationStatus = NO;

    }
}


- (NSString*)dataFilePath
{
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [docDir stringByAppendingPathComponent:@"AssignmentInfo.data"];
    NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath:filePath];

    if (!file) {
        NSLog(@"Attempting to create the file");
        if (![[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]) {
            NSLog(@"Failed to create file");
        }
        else
            file = [NSFileHandle fileHandleForWritingAtPath:filePath];
        NSLog(@"Pass to create file");

    }

    return filePath;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSUInteger index = textField.tag;

    if (index == 2) { // Last textField
        [textField resignFirstResponder];
    }else{

        UITextField *nextTextField = (UITextField*)[self.view viewWithTag:index+1];
        [nextTextField becomeFirstResponder];

    }
    return NO;
}




- (IBAction)addTheInfo:(id)sender {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
    dateFormatter.timeStyle = NSDateFormatterShortStyle;
    dateFormatter.dateStyle = NSDateFormatterShortStyle;
    NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date];
    self.assignmentInfo.className = self.className.text;
    self.assignmentInfo.assignmentTitle = self.assignmentTitle.text;
    self.assignmentInfo.assignmentDescription = self.assignmentDescription.text;
    self.assignmentInfo.dateTimeString = dateTimeString;

    [self presentMessage:self.assignmentInfo.description];
    [self dismissViewControllerAnimated:YES completion:nil];

    NSString *filePath = [self dataFilePath];
    [NSKeyedArchiver archiveRootObject:self.assignmentInfo toFile:filePath];

\\我的错误在这里vvvv

    [self.delegate assignmentSaved:self.assignmentInfo];
    NSLog(@"%@",self.delegate);

//错误^^^^^^^
    }

-(AssignmentInfo *)assignmentInfo
{
    if (!_assignmentInfo) {
        _assignmentInfo = [[AssignmentInfo alloc]init];
    }
    return _assignmentInfo;


}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    [_procrastinationNotificationSwitch addTarget:self action:@selector(procrastinationNotificationSwitchOnOrOff) forControlEvents:UIControlEventValueChanged];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];   // Do any additional setup after loading the view, typically from a nib.

    self.assignmentInfo.notifcationStatus = YES;


    self.className.tag = 0;
    self.className.delegate = self;

    self.assignmentTitle.tag = 1;
    self.assignmentTitle.delegate = self;

    self.assignmentDescription.tag = 2;
    self.assignmentDescription.delegate = self;


    AssignmentInfo *classUnarchived = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]];

    NSLog(@"%@" @"log of archive",classUnarchived.description);




    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

// // AssignmentInfo.h(MODEL)

//  AssignmentAppTwo
//
//  Created by Abdel Elrafa on 10/11/13.
//  Copyright (c) 2013 Abdel Elrafa. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface AssignmentInfo : NSObject

@property (nonatomic,strong)NSString *className;
@property (nonatomic,strong)NSString *assignmentDescription;
@property (nonatomic,strong)NSString *assignmentTitle;
@property (nonatomic,strong)NSString *dateTimeString;

@property (nonatomic)bool notifcationStatus;
-(id)initWithCoder:(NSCoder *)aDecoder;
-(void)encodeWithCoder:(NSCoder *)aCoder;

@end

// // AssignmentInfo.m(MODEL)

//  AssignmentAppTwo
//
//  Created by Abdel Elrafa on 10/11/13.
//  Copyright (c) 2013 Abdel Elrafa. All rights reserved.
//

#import "AssignmentInfo.h"

@implementation AssignmentInfo


-(NSString *)description
{

    return [NSString stringWithFormat:@"Class: %@\r Assignment Title: %@ \rAssignment Description: %@ \rDue: %@ \r%s", self.className, self.assignmentTitle, self.assignmentDescription, self.dateTimeString,self.notifcationStatus ? "Notification On" : "Notification Off"];
}


-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.className = [aDecoder decodeObjectForKey:@"className"];
    self.assignmentTitle = [aDecoder decodeObjectForKey:@"assignmentTitle"];
    self.assignmentDescription = [aDecoder decodeObjectForKey:@"assignmentDescription"];
    self.dateTimeString = [aDecoder decodeObjectForKey:@"dateTimeString"];
    self.notifcationStatus = [aDecoder decodeObjectForKey:@"notifcationStatus"];

    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.className forKey:@"className"];
    [aCoder encodeObject:self.assignmentTitle forKey:@"assignmentTitle"];
    [aCoder encodeObject:self.assignmentDescription forKey:@"assignmentDescription"];
    [aCoder encodeObject:self.dateTimeString forKey:@"dateTimeString"];
    [aCoder encodeBool:self.notifcationStatus forKey:@"notificationStatus"];
}



@end

我概述了上面的错误。我放弃了,因为我没有看到出了什么问题。

2 个答案:

答案 0 :(得分:1)

您正在使用AddAssignmentDelegate,但是从我看到您将委托定义为ViewControllerDelegate。

因此AddAssignmentDelegate基本上是未定义的,没有委托函数

答案 1 :(得分:0)

这个

@interface AssignmentListViewController : UITableViewController

应该成为

@interface AssignmentListViewController : UITableViewController <AddAssignmentDelegate>

这是我能看到的唯一问题,但你只需要发布相关代码,错误消息就会很好。