无法将操作连接到视图控制器

时间:2014-05-20 19:11:04

标签: ios objective-c xcode delegates ibaction

我正在做iOS Apprentice Bundle并且我不是很好......

我正在完成这个教程,当他们完成了#34;完成"按钮停止工作。它附加到IBAction,然后调用self.delegate。我知道这是有效的,因为我添加了一个NSLog但是(在其目的地添加了一个NSLog之后)它从未到达过。这意味着将用户输入的数据添加到新的表格行中。

任何帮助都会真的赞赏!谢谢!

以下是代码:

ListDetailViewControllerTableViewController.h

#import <UIKit/UIKit.h>

@class ListDetailViewController;
@class Checklist;

@protocol ListDetailViewControllerDelegate <NSObject>
- (void)listDetailViewControllerDidCancel:(ListDetailViewController *)controller;
- (void)listDetailViewController:(ListDetailViewController *)controller didFinishAddingChecklist:(Checklist *)checklist;
- (void)listDetailViewController:(ListDetailViewController *)controller didFinishEditingChecklist:(Checklist *)checklist;
@end

@interface ListDetailViewController : UITableViewController <UITextFieldDelegate>

@property (nonatomic, strong) IBOutlet UITextField *textField;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *doneBarButton;
@property (nonatomic, weak) id <ListDetailViewControllerDelegate> delegate;
@property (nonatomic, strong) Checklist *checklistToEdit;

- (IBAction)cancel;
- (IBAction)done;

@end

ListDetailViewControllerTableViewController.m

    - (IBAction)done
    {

        if (self.checklistToEdit == nil) {
            Checklist *checklist = [[Checklist alloc] init];
            checklist.name = self.textField.text;
            [self.delegate listDetailViewController:self didFinishAddingChecklist:checklist];

        } else {
            self.checklistToEdit.name = self.textField.text;
            [self.delegate listDetailViewController:self didFinishEditingChecklist:self.checklistToEdit];
        }

        NSLog(@"Hello, I'm a 'Done' log");

    }

AllListsViewControllerTableViewController.m

- (void)listDetailViewController:(ListDetailViewController *)controller didFinishAddingChecklist:(Checklist *)checklist
{
    NSLog(@"Adding Checklist");

    controller.delegate = self;

    NSUInteger newRowIndex = [self.dataModel.lists count];
    [self.dataModel.lists addObject:checklist];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];

    [self dismissViewControllerAnimated:YES completion:nil];

    NSLog(@"finished adding checklist");
}

AllListsViewControllerTableViewController.h

#import <UIKit/UIKit.h>
#import "ListDetailViewControllerTableViewController.h"
#import "DataModel.h"

@interface AllListsViewController : UITableViewController <ListDetailViewControllerDelegate>

@property (nonatomic, strong) DataModel *dataModel;

@end

谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于您正在设置委托 - 您无法在委托协议方法(listDetailViewController:didFinishAddingChecklist :)的实现中设置它,因为只有在委托已经设置的情况下才会调用该方法。您需要在创建ListDetailViewControllerTableViewController类的实例时设置委托。