• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Simple Cocoa App Not Working
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple Cocoa App Not Working


  • Subject: Re: Simple Cocoa App Not Working
  • From: "Hank Heijink (Mailinglists)" <email@hidden>
  • Date: Tue, 9 Feb 2010 13:15:03 -0500

On Feb 9, 2010, at 9:21 AM, Donald Klett wrote:

> 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.

You're actually breaking the MVC pattern here. The model should not have a reference to the View - that's the controller's business. The model should be completely agnostic of how it's displayed.

>  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.

At which point in your code did you print the value? What was in the text field when you saw zero being printed? Set a breakpoint in your viewAction and print the value of the text field. What does that tell you?

[snip]

> - (void) showValue;

That's the method that should be in the controller, not in the model. Also, the View instance variable should be in the controller.

> @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

You're missing a dealloc method - you'll leak view. Also, your init method isn't according to the standard. You should always check if [super init] returns nil. Check the Cocoa Fundamentals Guide. Same for your other classes.

> ------
>
> #import <Cocoa/Cocoa.h>
>
>
> @interface View : NSObject {
>
> 	IBOutlet NSTextField*	number;
> 	IBOutlet NSTextField*	result;
>
> }

Pet peeve: don't name text fields with names that suggest they're model objects. Something like numberField would be less confusing to others.

> - (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;
>
> }

This is where your View instance variable goes.

> - (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];
> }

And in this method, you ask the model for its value and you have the view display it.

Welcome to Cocoa, and good luck!
Hank

_______________________________________________

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

References: 
 >Simple Cocoa App Not Working (From: Donald Klett <email@hidden>)

  • Prev by Date: Simple Cocoa App Not Working
  • Next by Date: iPhone: UIWebView scroll position
  • Previous by thread: Simple Cocoa App Not Working
  • Next by thread: Re: Simple Cocoa App Not Working
  • Index(es):
    • Date
    • Thread