NSTextField - actions within *textField - objective-c

I need help using NSTextField and its inputs/outputs.
In a functioning code I have three different NSTextFields (in Cocoa + Obj-C) I know how calculate result from more integer inputs...
--- AppController.h ---
#interface AppController : NSObject {
IBOutlet NSTextField *firsTextField; // 1st Outlet
IBOutlet NSTextField *secondTextField; // 2nd Outlet
IBOutlet NSTextField *resultTextField; // 3rd Outlet
}
- (IBAction)result:(id)sender;
#end
--- AppController.m ---
#Implementation AppController
- (IBAction)result:(id)sender {
double first = [firstTextField doubleValue]; // set value 1st outlet
double second = [secondTextField doubleValue]; // set value 2nd outlet
double result = first + second; // count result
[resultTextField setDoubleValue:result]; // set value 3rd outlet
}
#end
But while I try do the same thing in only one NSTextField, I don't know how to do it... My idea is, that process should be following:
Set 1st integer (input)
*Choose math function "/, , -, +" (method/action)
Set 2nd integer (input)
Result (method/action) for calculating above mentioned inputs based on math function
But it is actually all what I am able to explain... problem is that I don't know how I can store 1st input value (before choosing math function) and how to count up result between 1st and 2nd input value.
Thank you in advance for every tip / link / reference / tutorial / etc.

As long as you have only one cycle, it is pretty easy. (BTW: The human math syntax (infix notation) is a little bit weird for computer programming, priority rules and brackets are needed, so some invented another algebraic syntax, the PN or reverse PN.)
When a operator is pressed, you store the operator and the first operand into a ivar. This is a state you enter and usually in GUI programming you try to prevent states. However it is, what users expect from a calculator.
First let us beautify your code with properties and generated ivars.
#interface AppController : NSObject
#property (weak, nonatomic) IBOutlet NSTextField *operandTextField;
#property (weak, nonatomic) IBOutlet NSTextField *resultTextField;
#end
Add properties for the first value and the operation:
#interface AppController : NSObject
#property (weak, nonatomic) IBOutlet NSTextField *operandTextField;
#property (weak, nonatomic) IBOutlet NSTextField *resultTextField;
#property (nonatomic) double operand;
#property (nonatomic) NSString *operator;
#end
Then you need the actions for the operations. Here is an example for one:
#interface AppController : NSObject
#property (weak, nonatomic) IBOutlet NSTextField *operandTextField;
#property (weak, nonatomic) IBOutlet NSTextField *resultTextField;
#property (nonatomic) double operand;
#property (nonatomic) NSString *operator;
- (IBAction)plus:(id)sender;
#end
The next thing you need is to add an action for "operation begin". You can connect it to the text field and make it its "enter" action.
#interface AppController : NSObject
#property (weak, nonatomic) IBOutlet NSTextField *operandTextField;
#property (weak, nonatomic) IBOutlet NSTextField *resultTextField;
#property (nonatomic) double operand;
#property (nonatomic) NSString *operator;
- (IBAction)plus:(id)sender;
- (IBAction)operate:(id)sender;
#end
Okay, done with the interface. Let's go to the implementation:
#implementation AppController
- (IBAction)plus:(id)sender
{
// Store operator
self.operator = [valueTextField doubleValue]; // Some checking?
// Store operation
self.operation = #"+"; // You can use int constant for that, but this is the easy way
}
- (IBAction)operate:(id)sender
{
// Check, whether a operation is already selected
if ([self.operation length]==0)
{
// No: Do nothing
return;
}
// Get the second operand
double operand = [valueTextField doubleValue];
// Do the calculation
double result;
if ([self.operation isEqualToString:#"+"])
{
result = self.operand + operand;
}
else if ([self.operation isEqualToString:#"-"])
{
result = self.operand - operand;
}
…
[self.resultTextField setDoubleValue:result];
self.operation = #"";
// If you do not set this, the next operand will start a new calculation with the first
// operand. Another feature would be to store the result we got and repeatedly perform
// the operation on the last result. But I would change the UI in that case.
}
#end
Typed in Safari.

after what I studying your code ... I'm finally got functional code ;-)
at first... here is how looks app:
and here is the code:
---------- CalcController.h -----------
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#interface CalcController : NSObject {
IBOutlet NSTextField *textField;
}
#property (weak) IBOutlet NSTextField *operandTextField;
#property (weak) IBOutlet NSTextField *resultTextField;
#property (nonatomic) double value1;
#property (nonatomic) double value2;
#property (nonatomic) double result;
#property (nonatomic) NSString *operator;
- (IBAction)plus:(id)sender;
- (IBAction)result:(id)sender;
#end
---------- CalcController.m -----------
#import "CalcController.h"
#implementation CalcController
#synthesize operandTextField;
#synthesize resultTextField;
#synthesize value1;
#synthesize value2;
#synthesize result;
#synthesize operator;
- (IBAction)plus:(id)sender {
value1 = [textField doubleValue];
NSLog(#"operand1 = %lf", value1); // for test and see value in console
[textField setStringValue:#""]; // for clear text after 1st input done
operator = #"+";
}
- (IBAction)result:(id)sender {
value2 = [textField doubleValue];
NSLog(#"operand2 = %lf", value2); // for test and see value in console
if ([operator isEqualToString:#"+"]) {
result = value1 + value2;
NSLog(#"value1: %lf + value2: %lf = result: %lf", value1, value2, result); // for tests and see values in console
}
[resultTextField setDoubleValue:result];
operator = #"";
}
#end
I Think that working good .... now it is time to add next functions, buttons with numbers etc...
Thanks for showing the right way to do this!

Related

Uncrustify obj-c: Newline before and after call to super, interface declaration and implementation declaration

How can I add newline after call to super with uncrustify
Current:
- (void)someFunction
{
[super someFunction];
more stuff;
and more stuff;
}
Desired:
- (void)someFunction
{
[super someFunction];
more stuff;
and more stuff;
}
And how can I add newline before and after interface declaration and implementation declaration
Current:
#interface SCLoginScreenViewController ()
#property (weak, nonatomic) IBOutlet UIView *someView;
#property (weak, nonatomic) IBOutlet UIView *anotherView;
#end
#implementation SCLoginScreenViewController
- (void)someFunction
{
}
Desired:
#interface SCLoginScreenViewController ()
#property (weak, nonatomic) IBOutlet UIView *someView;
#property (weak, nonatomic) IBOutlet UIView *anotherView;
#end
#implementation SCLoginScreenViewController
- (void)someFunction
{
}
I am using BBUncrustifyPlugin and UncrustifyX
Unfortunately Uncrustify doesn't support any of these features at the moment.1
The only "new line after" options that are shown below.
(Two not included in the screenshot are: Newline count after variable definition block and Newline count after variable definition block not at the top of a function body)
There are some other options in the "newlines" section. But again, none of them match your case:
(Newline after while missing from screenshots)
1. I'm using version2.1.1, I believe this is the latest

How to view data from NSTextField Correctly?

Hi i've recently completed a tutorial book on the basics of objective c. And now I'm "Attempting" to make a simple application. Right now I seem to be having the simplest issues which i cannot seem to fix, maybe you could tell me what i'm doing incorrectly.
AppDelegate.h
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
- (IBAction)saveData:(id)sender;
//Below is a simple IBOutlet which I try to retrieve data from when the IBAction saveData occurs
#property (weak) IBOutlet NSTextField *foodName;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
//Here is the saveData IBAction which should print out the value of the NSTextField if the user entered a value (if not returns null)
- (IBAction)saveData:(id)sender {
NSLog(#"%#", foodName.stringValue);
NSLog(#"Saved");
}
#end
The problem I seem to have is the build will fail and give me an error message on this line in AppDelegate.m:
NSLog(#"%#", foodName.stringValue);
the error message is: Use of undeclared identifier 'foodName'; did you mean '_foodName'?
Could someone please explain whats going on and how I can overcome this?
To address the getter method, use self.:
NSLog(#"%#", self.foodName.stringValue);
Also, a minor point: group all your #property declarations together at the start of the #interface statement, but after any instance variable declarations:
#interface MyClass : NSObject {
NSString *_str1;
int _i1;
}
#property (weak) IBOutlet NSString *something;
#property (strong, readonly) NSNumber *somethingElse;
- (int)aMethod:(NSString *)string;
- (void)anotherMethod;
#end

this class is not key value coding-compliant for the key XXX

I'm a beginner in everything programming and have been trying to implement some self learned stuff from the Big Nerd Ranch Books. But I'm really stumped by this problem, and yes, I have searched this and other forums for possible solutions with no success. Here is the code:
ANKYViewController.h:
#import <UIKit/UIKit.h>
#interface ANKYViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *weightFieldDeadlift;
#property (weak, nonatomic) IBOutlet UITextField *repFieldDeadlift;
#property (strong, nonatomic) IBOutlet UILabel *workingOneRMDeadlift;
#property (weak, nonatomic) IBOutlet UITextField *weightFieldBenchPress;
#property (weak, nonatomic) IBOutlet UITextField *repFieldBenchPress;
#property (strong, nonatomic) IBOutlet UILabel *workingOneRMBenchPress;
#property (weak, nonatomic) IBOutlet UITextField *weightFieldSquat;
#property (weak, nonatomic) IBOutlet UITextField *repFieldSquat;
#property (strong, nonatomic) IBOutlet UILabel *workingOneRMSquat;
#property (weak, nonatomic) IBOutlet UITextField *weightFieldMilitaryPress;
#property (weak, nonatomic) IBOutlet UITextField *repFieldMilitaryPress;
#property (strong, nonatomic) IBOutlet UILabel *workingOneRMMilitaryPress;
- (IBAction)calculateOneRM:(id)sender;
#end
ANKYViewController.m:
#import "ANKYViewController.h"
#interface ANKYViewController ()
#end
#implementation ANKYViewController
#synthesize weightFieldDeadlift;
#synthesize repFieldBenchPress;
#synthesize workingOneRMBenchPress;
#synthesize weightFieldSquat;
#synthesize repFieldSquat;
#synthesize workingOneRMSquat;
#synthesize weightFieldMilitaryPress;
#synthesize repFieldMilitaryPress;
#synthesize workingOneRMMilitaryPress;
#synthesize repFieldDeadlift;
#synthesize workingOneRMDeadlift;
#synthesize weightFieldBenchPress;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setWeightFieldDeadlift:nil];
[self setRepFieldDeadlift:nil];
[self setWorkingOneRMDeadlift:nil];
[self setWeightFieldDeadlift:nil];
[self setRepFieldBenchPress:nil];
[self setWeightFieldBenchPress:nil];
[self setWorkingOneRMBenchPress:nil];
[self setWeightFieldSquat:nil];
[self setRepFieldSquat:nil];
[self setWorkingOneRMSquat:nil];
[self setWeightFieldMilitaryPress:nil];
[self setRepFieldMilitaryPress:nil];
[self setWorkingOneRMMilitaryPress:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)calculateOneRM:(id)sender {
float dw = [[weightFieldDeadlift text]floatValue];
float dr = [[repFieldDeadlift text]floatValue];
float d = (dw * dr * 0.0333) + dw;
NSLog(#"Deadlift: %f", d);
NSString *deadlift = [[NSString alloc]initWithFormat:#"%f", d];
[workingOneRMDeadlift setText:deadlift];
float bpw = [[weightFieldBenchPress text]floatValue];
float bpr = [[repFieldBenchPress text]floatValue];
float bp = (bpw * bpr * 0.0333) + bpw;
NSLog(#"Bench Press: %f", bp);
NSString *benchPress = [[NSString alloc]initWithFormat:#"%f", bp];
[workingOneRMBenchPress setText:benchPress];
float sw = [[weightFieldSquat text]floatValue];
float sr = [[repFieldSquat text]floatValue];
float s = (sw * sr * 0.0333) + sw;
NSLog(#"Squat: %f", s);
NSString *squat = [[NSString alloc]initWithFormat:#"%f", s];
[workingOneRMSquat setText:squat];
float mpw = [[weightFieldMilitaryPress text]floatValue];
float mpr = [[repFieldMilitaryPress text]floatValue];
float mp = (mpw * mpr * 0.0333) + mpw;
NSLog(#"Military Press: %f", mp);
NSString *militaryPress = [[NSString alloc]initWithFormat:#"%f", mp];
[workingOneRMMilitaryPress setText:militaryPress];
}
#end
File's owner class is already stated as ANKYViewController. Linking the outlets etc was by control dragging instead of manually coding (I gave up after spending too many hours).
The error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0x6c22e70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key repsField.'
There's no copy and pasting of code, and it's certainly distressing that there is no mentioning of "repsField"(instead of repFieldxxx) anywhere.
I hope that this is enough information to help lead to a solution, as I've spent days looking through other people's solutions with no success.
Thanks.
Looks like the problem is that someone is accessing the UIApplication instance with a key (repsField) that is not KVC compliant.
Update I don't think you even need to subclass to add a breakpoint. Go to the breakpoint navigator and add a symbolic breakpoint for symbol -[UIApplication setValue:forUndefinedKey:] and then run the application.
I think you can debug it by subclassing UIApplication and set a breakpoint in a dummy override of the setValue:forUndefinedKey: method.
In the file where you call UIApplicationMain add this class:
#interface TestApplication : UIApplication
#end
#implementation TestApplication
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
[super setValue:value forUndefinedKey:key]; // set breakpoint here
}
#end
And then change the third argument to UIApplicationMain to #"TestApplication", for example:
UIApplicationMain(argc, argv, #"TestApplication", NSStringFromClass([AppDelegate class]));
Now run the application and it should break into to debugger and you should be able to see the call stack causing it.
When this error message appears it is very often a bad connection in a xib file or storyboard. I'll guess that this is an old example that has a MainWindow.xib file in it, with the File's Owner set as the UIApplication class. There is most likely a connection in that xib to an outlet called repsField in the File's Owner which, of course, does not match anything in the actual class.

create control with properties within it

I have control something like this
#import <Foundation/Foundation.h>
#import "AQLevelMeter.h"
#import "AQPlayer.h"
#import "AQRecorder.h"
#interface SpeakHereController : NSObject {
IBOutlet UIBarButtonItem* btn_record;
IBOutlet UIBarButtonItem* btn_play;
IBOutlet UILabel* fileDescription;
IBOutlet AQLevelMeter* lvlMeter_in;
AQPlayer* player;
AQRecorder* recorder;
BOOL playbackWasInterrupted;
BOOL playbackWasPaused;
}
#property (nonatomic, retain) UIBarButtonItem *btn_record;
#property (nonatomic, retain) UIBarButtonItem *btn_play;
#property (nonatomic, retain) UILabel *fileDescription;
#property (nonatomic, retain) AQLevelMeter *lvlMeter_in;
#property (readonly) AQPlayer *player;
#property (readonly) AQRecorder *recorder;
#property BOOL playbackWasInterrupted;
#property BOOL isReport;
#property CFStringRef recordFilePath;
- (IBAction)record: (id) sender;
- (IBAction)play: (id) sender;
-(void) InitializeThePlayer;
#end
as we can see I added many properties like
#property BOOL isReport;
#property CFStringRef recordFilePath;
then I created uiview contains this control
#import <UIKit/UIKit.h>
#class SpeakHereController;
#interface SpeakHereViewController : UIViewController {
IBOutlet SpeakHereController *controller;
}
Now I want to access the properties of the control object so I say
- (void)viewWillAppear:(BOOL)animated {
[self ReportDirectory];
[controller setIsReport:self.iSReport ];
//controller.isReport = self.iSReport ;
[controller setRecordFilePath:(CFStringRef) self.DICOMpath];
//controller.recordFilePath = ;
}
the problem is that at the lines
[controller setIsReport:self.iSReport ];
[controller setRecordFilePath:(CFStringRef) self.DICOMpath];
there is warning say that
warning: no '-setIsReport:' method found
I made
#synthesize isReport;
#synthesize recordFilePath;
also if I replaced #class SpeakHereController; by #import "SpeakHereController.h" it raise a lot of errors , U can download the sample code from apple
and if I said controller.isReport = self.iSReport ; it raise error request for member 'isReport' in something not a structure or union
My question is how to call the properties in this control , am I missing something
Best regards
I tried
At the top of SpeakHereViewController.m you will need
import "SpeakHereController.h"
Otherwise when SpeakHereViewController.m is compiled, it is completely unaware of what methods and properties your SpeakHereController class has
It should run fine even with the warning because the property does exist. However, I agree with you that the warning needs to be dealt with.
You should synthesize those properties in .m file.
The syntax is like:
#synthesize isReport, recordFilePath;

Getter for NSInteger not working

So I have a class with a NSInteger in it and now I want to return the NSInteger value. For some kind of reason, the code for that is not working. I have already declared the #property for the NSInteger class.
#property (readwrite, assign, nonatomic) NSInteger numberFun;
- (NSInteger)sampleMethod {
...
return sample.numberFun;
}
The compiler says "Return from pointer without a cast". I'm pretty sure that means that I'm using a C type for an objective-c method. I want to know the work around for this. (Though I don't want it to return a casted NSInteger as a NSNumber).
Thanks
The following code sample compiles fine. I suggest you present a more complete example of your problem so we can figure out what you are doing wrong.
#interface MyObject : NSObject
{ }
#property (readwrite, assign, nonatomic) NSInteger numberFun;
#end
#implementation MyObject
#synthesize numberFun;
#end
#interface MyObject2 : NSObject
{ }
#property (nonatomic, copy) MyObject* sample;
#end
#implementation MyObject2
#synthesize sample;
- (NSInteger)sampleMethod { return sample.numberFun; }
#end