Simple Cocoa App Not Working
Simple Cocoa App Not Working
- Subject: Simple Cocoa App Not Working
- From: Donald Klett <email@hidden>
- Date: Tue, 09 Feb 2010 09:21:02 -0500
I am trying to learn Cocoa and wrote a simple app using the MVC pattern. The view controls two text fields, the controller receives events from a single button, and the model receives the value entered into the first text field, and then requests the view to display that value in the second text field. I used IB to construct the UI and connect the view object with the text fields, and made the controller the target for the button. The build is successful, but the value from the first text field is not copied into the second. A printf shows the integer value from the first text field is zero. Here are the short files. Thanks in advance.
Don Klett
#import <Cocoa/Cocoa.h>
#import "View.h"
@interface Model : NSObject {
int value;
View* view;
}
@property (readwrite) int value;
- (void) showValue;
@end
--------
#import "Model.h"
#import "View.h"
@implementation Model
- (id) init {
self = [super init];
view = [[View alloc] init];
return self;
}
@synthesize value;
- (void) showValue {
self.value = [view getFieldValue];
[view setResult:self.value];
}
@end
------
#import <Cocoa/Cocoa.h>
@interface View : NSObject {
IBOutlet NSTextField* number;
IBOutlet NSTextField* result;
}
- (int) getFieldValue;
- (void) setResult: (int) value;
@end
------
#import "View.h"
@implementation View
- (int) getFieldValue {
return [number intValue];
}
- (void) setResult: (int) value {
[result setIntValue:value];
}
@end
------
#import <Cocoa/Cocoa.h>
#import "Model.h"
@interface Controller : NSObject {
Model* model;
}
- (IBAction) viewAction: (id) sender;
@end
------
#import "Controller.h"
@implementation Controller
- (id) init {
self = [super init];
model = [[Model alloc] init];
return self;
}
- (IBAction) viewAction: (id) sender {
[model showValue];
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden