2010-06-10 5 views
0

위의 오류가 발생하여 하루 종일 계속보고 있습니다. 누구든지 어떤 아이디어? 나는 아이폰 개발을 처음 사용한다. 아래 코드 :[UIView didCreateWorkout : 유형 : 거리 : 시간 : 메시지 :] : 인스턴스로 전송 된 인식 할 수없는 선택자

WorkoutAppDelegate.h ... :

#import "WorkoutViewController.h" 

@interface WorkoutAppDelegate : NSObject <UIApplicationDelegate> { 

    NSManagedObjectModel *managedObjectModel; 
    NSManagedObjectContext *managedObjectContext;  
    NSPersistentStoreCoordinator *persistentStoreCoordinator; 

    UIWindow *window; 
    WorkoutViewController *viewController; 
} 

- (IBAction)saveAction:sender; 

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; 
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; 
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; 

@property (nonatomic, readonly) NSString *applicationDocumentsDirectory; 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet WorkoutViewController *viewController; 


@end 

WorkoutAppDelegate.m .... :

#import "WorkoutAppDelegate.h" 

@implementation WorkoutAppDelegate 

@synthesize window; 
@synthesize viewController; 


- (void)applicationDidFinishLaunching:(UIApplication *)application {  
    // Override point for customization after app launch  
    viewController.managedObjectContext = [self managedObjectContext]; 
    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 
} 


/** 
applicationWillTerminate: saves changes in the application's managed object context before the application terminates. 
*/ 
- (void)applicationWillTerminate:(UIApplication *)application { 

    NSError *error; 
    if (managedObjectContext != nil) { 
     if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { 
      // Handle error 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); // Fail 
     } 
    } 
} 


/** 
Performs the save action for the application, which is to send the save: 
message to the application's managed object context. 
*/ 
- (IBAction)saveAction:(id)sender { 

    NSError *error; 
    if (![[self managedObjectContext] save:&error]) { 
     // Handle error 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); // Fail 
    } 
} 


/** 
Returns the managed object context for the application. 
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. 
*/ 
- (NSManagedObjectContext *) managedObjectContext { 

    if (managedObjectContext != nil) { 
     return managedObjectContext; 
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (coordinator != nil) { 
     managedObjectContext = [[NSManagedObjectContext alloc] init]; 
     [managedObjectContext setPersistentStoreCoordinator: coordinator]; 
    } 
    return managedObjectContext; 
} 


/** 
Returns the managed object model for the application. 
If the model doesn't already exist, it is created by merging all of the models found in the application bundle. 
*/ 
- (NSManagedObjectModel *)managedObjectModel { 

    if (managedObjectModel != nil) { 
     return managedObjectModel; 
    } 
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];  
    return managedObjectModel; 
} 


/** 
Returns the persistent store coordinator for the application. 
If the coordinator doesn't already exist, it is created and the application's store added to it. 
*/ 
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"WorkoutCoreData.sqlite"]]; 

    NSError *error; 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { 
     // Handle error 
    }  

    return persistentStoreCoordinator; 
} 


/** 
Returns the path to the application's documents directory. 
*/ 
- (NSString *)applicationDocumentsDirectory { 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
    return basePath; 
} 


- (void)dealloc { 
    [managedObjectContext release]; 
    [managedObjectModel release]; 
    [persistentStoreCoordinator release]; 

    [viewController release]; 
    [window release]; 
    [super dealloc]; 
} 


@end 

WorkoutViewController.h ... :

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

@protocol CreateWorkoutDelegate <NSObject> 

-(void)didCancelWorkout; 
-(void)didCreateWorkout:(NSString *)thisRoute 
        Type:(NSString *)thisType 
       Distance:(NSString *)thisDistance 
        Time:(NSString *)thisTime 
       Message:(NSString *)thisMessage; 

@end 


@interface WorkoutViewController : UIViewController { 
// IBOutlet UIlabel *Speed; 
// IBOutlet UIlabel *Calories; 
    IBOutlet UILabel *DBContents; 
    IBOutlet UITextField *route; 
    IBOutlet UITextField *type; 
    IBOutlet UITextField *distance; 
    IBOutlet UITextField *time; 
    IBOutlet UITextField *message; 
    IBOutlet UIButton *saveWorkout; 
    IBOutlet UIButton *cancelWorkout; 
    NSMutableArray *workoutArray; 
    id workoutDelegate; 
    Workout *currentWorkout; 
    NSManagedObjectContext *managedObjectContext; 
} 


//@property (retain,nonatomic) UILabel *Speed; 
//@property (retain,nonatomic) UILabel *Calories; 
@property (retain,nonatomic) UILabel *DBContents; 
@property (retain,nonatomic) UITextField *route; 
@property (retain,nonatomic) UITextField *type; 
@property (retain,nonatomic) UITextField *distance; 
@property (retain,nonatomic) UITextField *time; 
@property (retain,nonatomic) UITextField *message; 
@property (retain,nonatomic) NSMutableArray *workoutArray; 
//@property (retain,nonatomic) UIButton *saveWorkout; 
//@property (retain,nonatomic) UIButton *cancelWorkout; 
@property (nonatomic, assign) id<CreateWorkoutDelegate> workoutDelegate; 
@property (nonatomic, assign) NSManagedObjectContext *managedObjectContext; 

-(IBAction)hideKeyboard; 
-(IBAction)saveWorkout; 
-(IBAction)cancelWorkout; 

@end 

WorkoutViewController.m ... :

#import "WorkoutViewController.h" 
#import "Workout.h" 

@implementation WorkoutViewController 

@synthesize workoutDelegate; 
//@synthesize Speed; 
//@synthesize Calories; 
@synthesize route; 
@synthesize type; 
@synthesize distance; 
@synthesize time; 
@synthesize message; 
@synthesize DBContents; 
@synthesize workoutArray; 
@synthesize managedObjectContext; 
//@synthesize saveWorkout; 
//@synthesize cancelWorkout; 


-(IBAction)hideKeyboard { 
} 


-(IBAction)saveWorkout { 
    [workoutDelegate didCreateWorkout: route.text 
             Type: type.text 
            Distance: distance.text 
             Time: time.text 
            Message: message.text]; 
} 


-(IBAction)cancelWorkout { 
    [self.workoutDelegate didCancelWorkout]; 
} 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 


-(void)viewDidLoad { 
    //Set images for Save & Cancel buttons. 
    UIImage *normalImage = [[UIImage imageNamed:@"whiteButton.png"] 
          stretchableImageWithLeftCapWidth:12.0 
          topCapHeight:0.0]; 
    [saveWorkout setBackgroundImage:normalImage forState:UIControlStateNormal]; 
    [cancelWorkout setBackgroundImage:normalImage forState:UIControlStateNormal]; 

    UIImage *pressedImage = [[UIImage imageNamed:@"blueButton.png"] 
          stretchableImageWithLeftCapWidth:12.0 
          topCapHeight:0.0]; 
    [saveWorkout setBackgroundImage:pressedImage forState:UIControlStateHighlighted]; 
    [cancelWorkout setBackgroundImage:pressedImage forState:UIControlStateHighlighted]; 

    //Fetch details from the database. 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Workout" inManagedObjectContext:managedObjectContext]; 
    [request setEntity:entity]; 
    NSError *error; 
    self.workoutArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 
    [request release]; 

    //self.workoutArray = [[NSMutableArray alloc] init]; 

    //self.DBContents.text = [self.workoutArray objectAtIndex:0]; 

    [super viewDidLoad]; 
} 


-(void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning];  
    // Release any cached data, images, etc that aren't in use. 
} 


-(void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


-(void) didCreateWorkout:(NSString *)thisRoute 
        Type:(NSString *)thisType 
       Distance:(NSString *)thisDistance 
        Time:(NSString *)thisTime 
       Message:(NSString *)thisMessage { 

    // Add the new workout. 
    Workout *newWorkout = [NSEntityDescription 
          insertNewObjectForEntityForName:@"Workout" 
          inManagedObjectContext:self.managedObjectContext]; 
    newWorkout.route = thisRoute; 
    newWorkout.type = thisType; 
    newWorkout.distance = thisDistance; 
    newWorkout.time = thisTime; 
    newWorkout.message = thisMessage; 

    [self.workoutArray addObject:newWorkout]; 
    //[self dismissModalViewControllerAnimated:YES]; 
} 


-(void)didCancelWorkout { 
    [self dismissModalViewControllerAnimated:YES]; 
} 


-(void)dealloc { 
// [Speed release]; 
// [Calories release]; 
    [route release]; 
    [type release]; 
    [distance release]; 
    [time release]; 
    [message release]; 
// [saveWorkout release]; 
// [cancelWorkout release]; 
    [workoutArray release]; 
    [managedObjectContext release]; 
    [super dealloc]; 
} 


@end 

화면 (WorkoutViewController.xib)에 입력 한 세부 정보를 저장하려고하는데 저장 버튼을 클릭하고 위의 오류가 발생합니다.

감사 스티븐

+0

실제로 workoutDelegate를 어떻게 설정 하시겠습니까? – Vagrant

+0

UIView의 하위 클래스 인 것처럼 들리므로 CreateWorkoutDelegate 프로토콜을 구현하지 않습니다. – Vagrant

+0

변기, 답장을 보내 주셔서 감사합니다. 좀 더 자세히 설명해 주시겠습니까? 나는 아주 새로운 iPhone 개발과 당신이 의미하는 것이 확실하지 않습니다. 감사합니다, 스티븐 – Stephen

답변

0

오류가 당신이이 방법을 구현 한 경우, 당신이에 볼 수 있습니다 당신이 보는 정말 있다면 ....있는 UIView 방법을 -(void)didCreateWorkout ... 모르는 (구현되지 않음) 것을 말하고있다 WorkoutViewController (WorkoutViewController.m)은 UIView가 아닙니다 (프로젝트에 didCreateWorkout의 구현이 하나만 있다고 가정합니다). workoutDelegate 속성을 설정하는 방법을 다시 한 번 확인해야합니다. 코드에서 우리에게 보여주는 것은 WorkoutViewController의 인스턴스 여야합니다. .

-(IBAction)saveWorkout { 
    [self didCreateWorkout: route.text 
            Type: type.text 
           Distance: distance.text 
            Time: time.text 
           Message: message.text]; 
} 
-(IBAction)cancelWorkout { 
    [self didCancelWorkout]; 
} 

그러나이 빠른 수정 것 : 당신은 또한 WorkoutViewController-(IBAction)saveWorkout의 implementaion을 가지고 있기 때문에

는, BTW이 특정 문제에 대한 빠른 수정으로 당신의 행동의 코드를 변경하는 것 의도 한 디자인으로 문제를 해결하지 마십시오. CreateWorkoutDelegate을 구현해야하는 사람을 생각한 다음 workoutDelegate 속성을 올바르게 설정해야합니다. 다른 주제에

나는 변경을 고려해야 할 당신이 수도 코드에 두 가지를 발견 :

  • 사용하여 할당 해제의 방법에 self.property=nil 대신 [ivar release] (의 속성 사본이 있어야합니다
  • NSString 특성 NSMutableString 인스턴스로부터 자신을 보호하기 위해

행운을 비네! ;)

관련 문제