[UIView didCreateWorkout:Type:Distance:Time:Message:]:无法识别的选择器发送到实例

时间:2010-06-10 14:47:55

标签: iphone xcode

我收到了上述错误并且整天都在看着它,我没有快速到达。任何想法?我是iPhone开发的新手。代码如下:

... 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),然后单击“保存”按钮并获得上述错误。

由于 斯蒂芬

1 个答案:

答案 0 :(得分:0)

错误是说UiView不知道(没有实现)方法-(void)didCreateWorkout ... ....而且如果你看看你已经实现了这个方法,你会发现它在{{1} (WorkoutViewController.m),它不是UIView(我假设你的项目中只有WorkoutViewController的一个实现)。您应该仔细检查如何设置workoutDelegate属性。从您向我们展示的代码中,它应该是didCreateWorkout的实例。

顺便说一句,因为您在WorkoutViewController中也正在实施-(IBAction)saveWorkout,因此针对此特定问题的快速解决方法是将您的操作代码更改为:

WorkoutViewController

但是,此快速修复程序无法解决您可能想要的设计问题。您应该考虑谁应该实施-(IBAction)saveWorkout { [self didCreateWorkout: route.text Type: type.text Distance: distance.text Time: time.text Message: message.text]; } -(IBAction)cancelWorkout { [self didCancelWorkout]; } ,然后正确设置CreateWorkoutDelegate属性。

在另一个主题上,我注意到代码中有两件事你可以考虑改变:

  • 在您的dealloc方法中使用workoutDelegate而不是self.property=nil
  • [ivar release]属性应具有属性副本(以保护自己免受NSString个实例的攻击)
祝你好运! ;)