我正在尝试创建自己的自定义委托,以便将数据传递回上一个视图控制器,并且它给了我“找不到JournalrComposeViewControllerDelegate的协议声明”,即使它已经明确定义并包含头文件。请救命! 码: 正在尝试订阅JournalrComposeViewController的自定义委托的JournalrViewController .h文件:
// JournalrViewController.h
// Journalr
//
// Created by Kevin Turner on 7/16/13.
// Copyright (c) 2013 Kevin Turner. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "JournalrAppDelegate.h"
#import "JournalrComposeViewController.h"
@interface JournalrViewController : UIViewController <JournalrComposeViewControllerDelegate>
@property (nonatomic, readwrite) CGFloat cY;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic) BOOL returning;
@property (nonatomic, strong) IBOutlet UILabel *entryLabel;
@property (nonatomic) int totlal;
@end
JournalrViewController .m文件:
//
// JournalrViewController.m
// Journalr
//
// Created by Kevin Turner on 7/16/13.
// Copyright (c) 2013 Kevin Turner. All rights reserved.
//
#import "JournalrViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "JournalrComposeViewController.h"
@interface JournalrViewController ()
@end
@implementation JournalrViewController
@synthesize cY;
@synthesize scrollView;
@synthesize entryLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
cY = 50;
self.returning = NO;
[self fetchEntrys];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)deleteButtonPressed
{
NSLog(@"Button Pressed");
}
- (void)fetchEntrys
{
JournalrAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Entrys"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
//NSPredicate *pred =
//[NSPredicate predicateWithFormat:@"(photoName = %@)",
//@"1234"];
//[request setPredicate:pred];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request
error:&error];
if ([objects count] == 0) {
NSLog(@"No matches");
} else {
NSLog(@"Matches found");
matches = objects[1];
NSString *firstEntry = [matches valueForKey:@"entry"];
NSLog(@"First Entry: %@", firstEntry);
self.totlal = [objects count];
int i = [objects count] - 1;
NSLog(@"%i", i);
while ( i > -1) {
matches = objects[i];
NSString *entry = [matches valueForKey:@"entry"];
NSDate *date = [matches valueForKey:@"date"];
NSDateFormatter *formatDate = [[NSDateFormatter alloc]init];
[formatDate setDateFormat:@"MM/dd/YY"];
NSString *dateString = [formatDate stringFromDate:date];
NSLog(@"Date: %@", dateString);
NSLog(@"Entry: %@", entry);
UILabel *dateLabel = [[UILabel alloc] init];
dateLabel.text = dateString;
dateLabel.frame = CGRectMake(20, cY, 100, 30);
dateLabel.numberOfLines = 3;
dateLabel.font = [UIFont boldSystemFontOfSize:24.0];
UIButton *deleteButton = [[UIButton alloc]initWithFrame:CGRectMake(230, dateLabel.frame.origin.y, 70, 27)];
[deleteButton addTarget:self action:@selector(deleteButtonPressed) forControlEvents:UIControlEventTouchDown];
[deleteButton setBackgroundImage:[UIImage imageNamed:@"delete.jpg"] forState:UIControlStateNormal];
cY += 35;
UILabel *labelEntry = [[UILabel alloc]init];
labelEntry.numberOfLines = 0;
labelEntry.text = entry;
CGRect lblFrame = CGRectMake(20, cY, 280, 1000);
labelEntry.frame = lblFrame;
labelEntry.textAlignment = NSTextAlignmentLeft;
labelEntry.backgroundColor = [UIColor clearColor];
[labelEntry sizeToFit];
UILabel *outerLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, cY - 50, 300, labelEntry.frame.size.height + 80)];
[scrollView addSubview:outerLabel];
outerLabel.layer.borderColor = [UIColor grayColor].CGColor;
outerLabel.layer.borderWidth = 1.0;
//UIImageView *backgroundImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, cY, 312, labelEntry.frame.size.height)];
//backgroundImage.contentMode = UIViewContentModeScaleToFill;
//backgroundImage.image = [UIImage imageNamed:@"post.jpg"];
//NSLog(@"Image Height :%f", backgroundImage.frame.size.height);
//[scrollView addSubview:backgroundImage];
cY += labelEntry.frame.size.height;
NSLog(@"Label Height: %f", labelEntry.frame.size.height);
NSLog(@"currenty: %f", cY);
cY += 100.f;
scrollView.scrollEnabled = YES;
scrollView.showsVerticalScrollIndicator = YES;
[scrollView addSubview: labelEntry];
[scrollView addSubview:dateLabel];
[scrollView addSubview:deleteButton];
i--;
}
scrollView.contentSize = CGSizeMake(320, cY);
NSLog(@"cY: %f", cY);
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
JournalrComposeViewController* controller = [segue destinationViewController];
controller.delegate = self;
}
-(void)refresh {
NSLog(@"IT WORKED!");
}
@end
JournalrComposeViewController .h文件:
//
// JournalrComposeViewController.h
// Journalr
//
// Created by Kevin Turner on 7/17/13.
// Copyright (c) 2013 Kevin Turner. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "JournalrAppDelegate.h"
@class JournalrComposeViewController;
@protocol JournalrComposeViewControllerDelegate <NSObject>
-(void)refresh;
@end
@interface JournalrComposeViewController : UIViewController<UITextViewDelegate>
@property (strong, nonatomic) IBOutlet UITextView *textView;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *saveButton;
- (IBAction)save:(id)sender;
- (IBAction)cancel:(id)sender;
@property (nonatomic, assign) id <JournalrComposeViewControllerDelegate>delegate;
@end
最后是JournalrComposeViewController .m文件:
//
// JournalrComposeViewController.m
// Journalr
//
// Created by Kevin Turner on 7/17/13.
// Copyright (c) 2013 Kevin Turner. All rights reserved.
//
#import "JournalrComposeViewController.h"
#import "JournalrViewController.h"
@interface JournalrComposeViewController ()
@end
@implementation JournalrComposeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.textView becomeFirstResponder];
self.textView.delegate = self;
self.saveButton.enabled = self.textView.text.length > 0;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
- (void)textViewDidChange:(UITextView *)textView
{
self.saveButton.enabled = self.textView.text.length > 0;
}
- (IBAction)save:(id)sender {
NSDate *date = [NSDate date];
NSLog(@"%@", date);
JournalrAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSManagedObject *newEntry;
newEntry = [NSEntityDescription
insertNewObjectForEntityForName:@"Entrys"
inManagedObjectContext:context];
[newEntry setValue: self.textView.text forKey:@"entry"];
[newEntry setValue: date forKey:@"date"];
[newEntry setValue: @"1234" forKey:@"photoName"];
NSError *error;
[context save:&error];
JournalrViewController *journalr = [[JournalrViewController alloc]init];
journalr.returning = TRUE;
[self.delegate refresh];
[self dismissViewControllerAnimated:YES completion:^{
}];
}
- (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
}];
}
@end