0

I am fairly new to programming and am working with Objective-C in Xcode 5. I'm presently making an OSX application in Xcode that uses Cramer's Rule (this matrix math method to calculate the intersecting point of three lines).

I really need some help with this one concept- I need to be able to take the user's input from multiple text boxes (assign them all a variable), put them through cramer's rule, and feed the answer out through a label.

I've made the storyboard and assigned one of the 12 text boxes (to test it) as an outlet and the label as an outlet and a button as an action, and tried a few different ways to just take the user input and (unaltered) feed it back out through the label so I know what I'm working with before I get into the math, and it's been unsuccessful. Having major syntax problems.

I have attached my code below:

//
//  NewClass.h
//  Cramer's Rule

#import <Foundation/Foundation.h>


@interface NewClass : NSViewController <NSTextFieldDelegate> {

IBOutlet NSTextField *box_a;
IBOutlet NSTextField *coord;
NSString *string;
}


@property (nonatomic, retain) IBOutlet NSTextField *box_a;
@property (nonatomic, retain) IBOutlet NSTextField *coord;


- (IBAction)calculate:(id)sender;

@end

AND

//
//  NewClass.m
//  Cramer's Rule

#import "NewClass.h"

@implementation NewClass
@synthesize box_a;
@synthesize coord;


- (IBAction)calculate:(id)sender {

    NSTextField * input=box_a;
    coord =input;
}
@end
2
  • 2
    I didn't think you could use storyboards for OSX projects. Interesting... ...oh... you can't... Commented May 2, 2014 at 2:53
  • Which NSTextField action have you connected to your IBAction method? In your IBAction method, sender will be the object (NSTextField) that triggered the action. It will also be accessible as self.box_a as that is the property you have bound in Image Builder. You need to use the a method, such as the stringValue method to access the content of an NSTextField. So you probably want [self.coord setStringValue:[(NSTextField *)sender stringValue]; Commented May 2, 2014 at 3:01

3 Answers 3

1

As far as I know, I have the most up to date version of Xcode, and there is no option for creating a storyboard for an OSX project. Storyboards are for iOS projects. And that would explain the reason why you're unable to hook any thing up from the storyboard to your code.

This isn't to say that a storyboard can't be put in an OSX project--it can't. But it can't be selected from the Cocoa section of new files to create--only the Cocoa Touch section, which is iOS stuff--not OSX.

Sign up to request clarification or add additional context in comments.

2 Comments

I presume that the OP is confused between Storyboards and xib files and means "Interface Builder" rather than storyboard
@nhgrif Sorry he ^ is right, I meant interface builder.
1

You have to use NSTextFieldDelegate, it have callback methods like in iOS:

- (void)textDidBeginEditing:(NSNotification *)notification;
- (void)textDidEndEditing:(NSNotification *)notification;
- (void)textDidChange:(NSNotification *)notification;
- (BOOL)acceptsFirstResponder;

For example:

- (void)textDidChange:(NSNotification *)notification{
if ([notification object]== box_a)
{
// ...
}else if ([notification object]== box_b)
{
// ...
}
}

Comments

0

Your problem is more fundamental than syntactical, you need to go and study up on what various things are and how they behave, this includes: variables, properties, objects and object references.

To briefly introduce why you're going wrong: Think of an object as a building. What is "in" the building may change over time, but the address of the building (usually!) does not. An address refers you to a building, and that is what an object reference does.

A variable is a box which holds a value of some type, that value can change over time, but the box does not.

When you declare:

NSTextField *input;

You are requesting that a variable be created for you which can hold references to objects - it does not hold an object anymore than address is a building, it just tells you where to find an object.

When you then assign a value to your variable:

NSTextField *input = box_a;

You are requesting the the value in box_a be copied and placed (stored) in input. That value is an object reference, it is not an object. Whatever object was referenced by box_a is not altered in anyway by this statement - what is in the house doesn't change, you just write the house's address down somewhere else.

When you then do:

coord = input;

you are doing the same thing - copying addresses. No objects are altered. The objects you are referring to are of type NSTextField, they have a visual representation on the screen, copying their addresses doesn't alter that visual representation anymore than copying the address of a building changes what is in the building.

When it comes to properties your code suggests a confusion between a property, which is a piece of code which does something, and its backing variable, a variable which that piece of code operates on.

Understanding these concepts is vital. You need to go an study up some more on programming.

HTH

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.