NSLog打印(null)对象?

时间:2013-10-22 19:38:02

标签: iphone ios objective-c nslog

每当按下Save按钮时,我都会使用委托将一个对象从AssignmentViewController传递给我的TableViewController。

我正在尝试打印出我应该刚刚保存并传递的对象的内容,但NSLog显示(null)。有人可以帮我找出原因吗?

AssignmentViewController.h

@protocol AssignmentDelegate <NSObject>

-(void)newAssignment:(Homework *)assignment;

@end

@interface Assignment : UIViewController <UITextViewDelegate> 

@property (nonatomic,weak)id<AssignmentDelegate> delegate;
@property(nonatomic) IBOutlet UITextField *ClassNameField;
@property(nonatomic) IBOutlet UILabel *ClassNameLabel;
@property(nonatomic) IBOutlet UITextField *AssignmentTitleField;
@property(nonatomic) IBOutlet UILabel *AssignmentTitleLabel;


@property (nonatomic, strong) Homework *homeworkAssignment;


- (IBAction)Save:(UIButton *)sender;

AssignmentViewController.m

- (IBAction)Save:(UIButton *)sender {

self.homeworkAssignment = [[Homework alloc] init];
self.homeworkAssignment.className = self.ClassNameField.text;
self.homeworkAssignment.assignmentTitle = self.AssignmentTitleField.text;
self.homeworkAssignment.assignmentDiscription = self.DiscriptionTextView.text;

[self.delegate newAssignment:self.homeworkAssignment];

 NSLog(@"%@",self.delegate);   //PRINTS (null)*******************************
[self.navigationController popViewControllerAnimated:YES];
}

这是我的TableViewController.h问题出在这里

@interface AssignmentTableController : UITableViewController
<
AssignmentDelegate
>

@property (strong, nonatomic) NSMutableArray *alist;


@end

TableViewController.m

-(NSMutableArray *)alist
{
if (!_alist) {
    _alist = [[NSMutableArray alloc]init];
}
return _alist;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"to2"]) {
    Assignment *assignmentViewController = segue.destinationViewController;
    assignmentViewController.delegate = self;
}
}

-(void)newAssignment:(Homework *)assignment
{


[self.alist addObject:assignment];
NSString *filePath = [self dataFilePath];
[NSKeyedArchiver archiveRootObject:self.alist toFile:filePath];
[self.tableView reloadData];
}

1 个答案:

答案 0 :(得分:0)

通过验证segue id并在方法中的if语句中放置一个beakpoint,确保- (void) prepareForSegue正在运行。