没有调用委托方法/ ChildViewController以某种方式结束为零

时间:2014-02-28 00:00:12

标签: ios iphone objective-c uitableview delegates

我是代表团的新手,所以我一直在寻找答案,我看了method delegates doesn't get calledDelegate method not being called,看看我的回答是否存在。我还看了How do I set up a simple delegate to communicate between two view controllers?对代表的简要介绍,并尽力正确地遵循它。我一直在检查我的代码,我认为我做得对,但在我的调试中,我已经知道我显然没有。

我想要做的是当用户切换回父ViewController(用户刚刚向字典添加了新值)时,从文件“Data.plist”(存储在Documents目录中)重新加载tableView。应用程序在重新打开时正确加载新输入的内容,因此我知道该文件正在正确保存。

这是我的ParentViewController.h文件。

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

@interface ParentViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, ChildViewControllerDelegate>

@end

这是我的ParentViewController.m文件。

#import "ParentViewController.h"

@interface ParentViewController () 
@end

@implementation ParentViewController
-(void)addEvent {
    NSLog(@"We are before [self loadNewEvent];");
    [self loadNewEvent];
    NSLog(@"We are after [self loadNewEvent];");
}

-(void)loadNewEvent {
    [self.tableView scrollToRowAtIndexPath:0 atScrollPosition:UITableViewScrollPositionNone animated:YES];
    [self.tableView reloadRowsAtIndexPaths:0 withRowAnimation:UITableViewRowAnimationLeft];
    //[self.tableView reloadData];//This doesn't seem to do anything but leave the tableview blank when this line is in viewDidLoad and viewWillAppear.
    NSLog(@"WHY IS THIS NOT WORKING?!?!");
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //stuff that loads data initially into tableView
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //cell formatting stuff
    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"addEvent"]) {
        [(ChildViewController *)[[segue destinationViewController] topViewController] setFilePath:filePath];
        ChildViewController *detailViewController = [[ChildViewController alloc] init];
        [detailViewController setDelegate:self];
        //[self.navigationController pushViewController:detailViewController animated:YES];
    }
}
@end

这是我的ChildViewController.h

#import <UIKit/UIKit.h>

@protocol ChildViewControllerDelegate;

@interface ChildViewController : UIViewController
@property (nonatomic, weak) id<ChildViewControllerDelegate> delegate;
- (IBAction)dismissAndSave:(id)sender;//in implementation
- (IBAction)dismissCancel:(id)sender;//in implementation
@end

@protocol ChildViewControllerDelegate <NSObject>
-(void)addEvent;
@end

最后是我的ChildViewController.m文件。

#import "ChildViewController.h"
@interface ChildViewController ()
@end

@implementation ChildViewController
- (IBAction)dismissAndSave:(id)sender {
    //perform file-saving operations
    if ([self.delegate respondsToSelector:@selector(addEvent)]) {
        [self.delegate addEvent];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

我已经完成并调试了,并且ChildViewController不是nil的最后一个点是父的prepareForSegue方法中的[detailViewController setDelegate:self];行。

我知道我的委托方法没有被调用的原因是因为NSLog(@"We are before [self loadNewEvent];");没有发生。

除了未调用的方法之外,当我尝试在[self.tableView reloadData];委托方法中执行addEvent时,所有内容都显示为空白。

提前感谢您的帮助;你正在为一个自学iOS程序和Objective-C的学生提供优质的服务!

2 个答案:

答案 0 :(得分:0)

扩展我的评论,我相信您需要在.h文件中声明tableview属性:

e.g。

@property (strong) IBOutlet UITableView *myTableView;

,一旦连接到Storyboard或XIB中的实际表视图,您就可以在.m文件中引用,如下所示:

[self.myTableView reloadData];

答案 1 :(得分:0)

看起来你正在分配一个新的视图控制器,设置它的委托,然后不用它做任何事情。一旦超出范围,它就会被释放。

[(ChildViewController *)[[segue destinationViewController] topViewController] setFilePath:filePath];
ChildViewController *detailViewController = [[ChildViewController alloc] init];
[detailViewController setDelegate:self];

尝试这样的事情......

ChildViewController * detailViewController = (id)[segue destinationViewController];
[detailViewController setDelegate:self];