UIViewControllerでUITableViewのdelegateを呼び出すのが上手くいきません。
xcode6
master-detail-appのテンプレート,coredata使用で始めました。
TableViewControllerで追加ボタンを押すと、右下のViewControllerに移動して
そのViewControllerの保存ボタンを押すと
デリゲートによって,insertNewObjectメソッドが呼ばれてTableViewセルを追加。
というふうにしたいのですが(イメージはアラームアプリの新規登録です)
うまくいきません。
ViewController.h
#import <UIKit/UIKit.h>
@protocol myProtocol <NSObject>
-(void)insertNewObject:(id)sender;
@end
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButtom;
@property(nonatomic,weak)id <myProtocol> delegate4486;
- (IBAction)saveData:(id)sender;
- (IBAction)cancel:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)saveData:(id)sender {
if([_delegate4486 respondsToSelector:@selector(insertNewObject:)]){
[_delegate4486 insertNewObject:_saveButtom];
}
[self dismissViewControllerAnimated:YES completion:nil]; }
- (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
上でif([_delegate4486 responds...])の中身をBreak Pointで設定しても
ビルドが通ってしまいます。
MasterViewController.h
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "ViewController.h"
@interface MasterViewController: UITableViewController <NSFetchedResultsControllerDelegate,myProtocol>
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@end
ViewController.m
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController ()
@end
@implementation MasterViewController
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
ViewController* viewC=[[ViewController alloc]init];
viewC.delegate4486 =self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void)insertNewObject:(id)sender {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
//以下省略
Break Pointをあえて出るようにして調べてみたところ
_delegate4486=(id)0x0
と出たので、これが原因かと思い
色々とネットで検索しても解決法が分からず
八方塞がりです。
なぜ上手くいかないのか教えていただけるとありがたいです。