2013-10-14 45 views
-1

我有1個錯誤,我看不出爲什麼? 我會發布所有的代碼,因爲我一直在過去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(模型)

// 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 

我上面列出我的錯誤。我放棄了,因爲我沒有看到出了什麼問題。

+0

你能告訴我什麼是錯誤信息嗎? –

+0

@KyokookHwang錯誤: [self.delegate assignmentSaved:self.assignmentInfo]; 沒有已知的選擇器'assignmentSaved'的實例方法:' – AbdelElrafa

回答

1

您正在使用AddAssignmentDelegate,但從我看到您將代表定義爲ViewControllerDelegate。

因此AddAssignmentDelegate基本上沒有定義,並且沒有委託功能

+0

Thant是它謝謝你! – AbdelElrafa

0

@interface AssignmentListViewController : UITableViewController 

應該成爲

@interface AssignmentListViewController : UITableViewController <AddAssignmentDelegate> 

,這是我能看到的唯一問題,但你只需要交相關的代碼和錯誤消息將是很好。

+0

感謝捕獲,但我添加它後,它說找不到'AddAssignmentDelegate'的協議定義 而另一個錯誤仍然存​​在:[self.delegate assignmentSaved:self .assignmentInfo];沒有已知的實例方法選擇器'assignmentSaved:' – AbdelElrafa

相關問題