Trouble with Method and Return Value Calculation - ios

I created a method to calculate a simple calculation.
I want the method to return an int and take 2 int parameters.
I created a class called calculation. I imported calculation into the viewController.m and created an action 2 textFields and a text Label. I am using this view for testing the method.
My method return 0 in the calculation. What am I doing wrong? It's so simple but I can't seem to figure out where I'm making my mistake. Here is the code.
Calculation.h
#import <Foundation/Foundation.h>
#interface Calculation : NSObject
#property (nonatomic) int odometerStart;
#property (nonatomic) int odometerEnd;
#property (nonatomic) int odometerTotal;
- (int)mileageStart:(int)start mileageEnd: (int)end;
#end
Calculation.m
#import "Calculation.h"
#implementation Calculation
#synthesize odometerEnd, odometerStart, odometerTotal;
- (int)mileageStart:(int)start mileageEnd:(int)end
{
odometerStart = start;
odometerEnd = end;
odometerTotal = end - start;
return odometerTotal;
}
#end
viewController.h
#import <UIKit/UIKit.h>
#interface MileageViewController : UIViewController
- (IBAction)calculateMileage:(id)sender;
#property (strong, nonatomic) IBOutlet UITextField *startLabel;
#property (strong, nonatomic) IBOutlet UITextField *endLabel;
#property (strong, nonatomic) IBOutlet UILabel *displayLabel;
#end
viewController.m
#import "MileageViewController.h"
#import "Calculation.h"
#implementation MileageViewController
#synthesize startLabel;
#synthesize endLabel;
#synthesize displayLabel;
- (void)viewDidUnload
{
[self setStartLabel:nil];
[self setEndLabel:nil];
[self setDisplayLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (IBAction)calculateMileage:(id)sender {
Calculation *mileage = [[Calculation alloc] init];
int odomStart = [[startLabel text] intValue];
int odomEnd = [[endLabel text ] intValue];
[mileage mileageStart:odomStart mileageEnd:odomEnd];
mileage.odometerTotal = displayLabel.text.intValue;
NSLog(#"THe total is %d", mileage.odometerTotal);
My total keeps equaling 0. The calculation isn't being calculated.

Your statement is backwards. You're trying to set the value of odometerTotal to the value of your label, but you want it the other way around. It should be:
displayLabel.text = [NSString stringWithFormat:#"%d",mileage.odometerTotal];
However, mileage returns an int value, so you don't really need the odometerTotal property in your Calculation class . You could do it like this:
displayLabel.text = [NSString stringWithFormat:#"%d",[mileage mileageStart:odomStart mileageEnd:odomEnd]];

I saw the whole program, and the assignment you did seemed wrong,
the bold pointed was as below:
mileage.odometerTotal = displayLabel.text.intValue;
you want to use the calculate class to calculate the total value. while the order should be opposite.
How about this:
displayLabel.text.iniValue = mileage.odometerTotal;
then use the NSLog method to print your expected result.

Related

How to use the UIStepper to control global variable?

I tried to control the num variable through UIStepper, but when I pressed the increase button,
It always start count from 0 not 10,
I am curious what did I miss in my code,
Thank you in advance for any comments
#import "ViewController.h"
int num = 10;
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *myLabel;
- (IBAction)myStepper:(UIStepper *)sender;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_myLabel.text = [NSString stringWithFormat:#"%i", num];
}
- (IBAction)myStepper:(UIStepper *)sender {
num = [sender value];
_myLabel.text = [NSString stringWithFormat:#"%i", num];
}
#end
You're never setting the value of the stepper to num. Either set it in Interface Builder's Attributes Inspector, or create an outlet to your stepper:
#property (weak, nonatomic) IBOutlet UIStepper myStepper;
And then in -viewDidLoad, set it like this:
self.myStepper.value = num
As an aside, I would recommend against using a global variable. I would make a property for num instead.

Simple math app: answer always equals 1

I'm new to programming so this is probably a result of my illiteracy, but I would appreciate a solution anyways.
I'm trying to make an app that takes the user input from 12 different textfields and feeds out an answer into a label by mathematically altering the user input using Cramer's rule.
So far, I've been trying to multiply the user input from one textfield by the user input from another and feed it out into a label, but whenever I type in my numbers, the product is always 1, and it doesn't print to the label.
Here is my code:
//
// ViewController.h
// Cramer
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
// Objects are given addresses:
#property (weak, nonatomic) IBOutlet UITextField *box_a;
#property (weak, nonatomic) IBOutlet UITextField *box_b;
#property (weak, nonatomic) IBOutlet UILabel *hiLabel;
#property (weak, nonatomic) IBOutlet UIButton *clickButton;
#end
AND
//
// ViewController.m
// Cramer
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// Math takes place:
- (IBAction)clickButton:(id)sender {
NSInteger number1 = [self.box_a.text integerValue];
NSInteger number2 = [self.box_b.text integerValue];
NSInteger prod = number1 * number2;
self.hiLabel.text = [NSString stringWithFormat:#"%#", #(prod)];
}
#end
Thanks
Use %ld as the format specifier in the last statement:
self.hiLabel.text = [NSString stringWithFormat:#"%ld", prod];

Simple math iOS app?

I'm pretty new to programming, and I just need to be pointed in the right direction with this.
I'm trying to make an iOS app in Xcode (using Objective-c) that takes in the user's input and uses a 3x3 cramer's rule matrix to feed back a coordinate. I've been learning about properties and methods, and I'm just confused as to how I can get the user's input (12 numbers), alter them mathematically, and print out the answer.
So far, I've been able to, using a textfield, a label, and a button, take the user's input and feed it out through the label. But I understand, from previous questions, that properties are like the addresses of certain objects, not the actual objects.
It would be awesome if someone could just show me how to take the user input from one textfield and multiply by the user input from another textfield, just so I can see how to do this.
Here are both files:
// ViewController.h
// Cramer
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
// Objects are given addresses:
#property (weak, nonatomic) IBOutlet UITextField *box_a;
#property (weak, nonatomic) IBOutlet UITextField *box_b;
#property (weak, nonatomic) IBOutlet UILabel *hiLabel;
#property (weak, nonatomic) IBOutlet UIButton *clickButton;
#end
AND
//
// ViewController.m
// Cramer
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// When button is clicked, user input from textfield is fed out through label:
- (IBAction)clickButton:(id)sender {
NSInteger number1 = [self.box_a.text integerValue];
NSInteger number2 = [self.box_b.text integerValue];
NSInteger prod = number1 * number2;
self.hiLabel.text = [NSString stringWithFormat:"%#", #(prod)];
}
#end
Both errors are on self.hiLabel line
Something like this:
NSInteger number1 = [self.textField1.text integerValue];
NSInteger number2 = [self.textField2.text integerValue];
NSInteger prod = number1 * number2;
To set prod value in UILabel or UITextField:
self.textField1.text = [NSString stringWithFormat:#"%#", #(prod)];

How to save UITextField Text to NSString?

In my main view controller, I have a UITextField, and I am trying to save the text input into it to a NSString in my Homework model(class).
Homework.h
#import <Foundation/Foundation.h>
#interface Homework : NSObject
#property (nonatomic, strong) NSString *className;
#property (nonatomic, strong) NSString *assignmentTitle;
#end
Homework.m
#import "Homework.h"
#implementation Homework
#synthesize className = _className;
#synthesize assignmentTitle = _assignmentTitle;
#end
In my assignmentViewController, I create an object of type Homework and try to set it equal to whatever is entered into the the UITextField when the Save Button is pressed.
Assignment View Controller
#import <UIKit/UIKit.h>
#import "Homework.h"
#interface Assignment : UIViewController {
}
#property(nonatomic) IBOutlet UITextField *ClassNameField;
#property(nonatomic) IBOutlet UILabel *ClassNameLabel;
#property (weak, nonatomic) IBOutlet UIButton *SaveButton;
#property (nonatomic, strong) Homework *homeworkAssignment;
- (IBAction)Save:(UIButton *)sender;
#end
AssignmentViewController.m
- (IBAction)Save:(UIButton *)sender {
self.homeworkAssignment.className = self.ClassNameField.text;
NSLog(#"SaveButtonPressed %#", self.homeworkAssignment.className);
}
The NSLog prints out that className is (null). Can anyone help me figure out what I am doing wrong? This is my first ever iOS app (besides Hello World).
Edit: This is using ARC
Edit: I tried changing
self.homeworkAssignment.className = self.ClassNameField.text; to
self.homeworkAssignment.className = #"TEST";
and the log still shows (Null).
Double check you properly linked ClassNameField outlet and that you're initializing homeworkAssignment. Something like.-
self.homeworkAssignment = [[Homework alloc] init];
By the way, you should consider using camelCase notation for your variable names :)
Well to be honest the first steps are always hard but you should learn it the right way, héhé
First of all synthesize this way:
#synthesize labelAssignmentTitle,labelClassName;
or
#synthesize labelAssignmentTitle;
#synthesize labelClassName;
there is no need to do the following:
#synthesize className = _className;
#synthesize assignmentTitle = _assignmentTitle;
Now if you initialize the right way from the the start you'll find it a lot easier later!
HomeWork.h
#interface HomeWork : NSObject
#property (nonatomic, strong) NSString *className;
#property (nonatomic, strong) NSString *assignmentTitle;
-(id)initWithClassName:(NSString *)newClassName andAssignmentTitle:(NSString*)newAssigmentTitle;
HomeWork.m
#implementation HomeWork
#synthesize assignmentTitle,className;
-(id)initWithClassName:(NSString *)newClassName andAssignmentTitle:(NSString*)newAssigmentTitle {
self = [super init];
if(self){
assignmentTitle = newAssigmentTitle;
className = newClass;
}
return self;
}
#end
ViewController.m
- (IBAction)saveIt:(id)sender {
HomeWork *newHomeWork = [[HomeWork alloc]initWithClassName:[labelClassName text]andAssignmentTitle:[labelAssignmentTitle text]];
}
Because of this, you directly make a newHomeWork object with the parameters given by your two UITextFields.
Now print it out in your logmessage and see what happends ;)

Unrecognized selector sent to instance (UIStepper)

Okay, I know there is a ton of these questions out there, because I've looked and tried some of the solutions. However, many of the ones I tried didn't work or the answer was too over my head for me to really grasp well - I'm a new developer and this is my first app. I learn by learning what not to do at this point.
I have the 'unrecognized selector sent to instance error' on a UIStepper stepperValueChanged setup. Here is the contents of the error message as it is given to me:
[DetailViewController stepperValueChanged]: unrecognized selector sent to instance 0x8637630
I will probably be ripped apart for this, but I can't really understand what's going on here - my only guess so far is to assume it has something to do with the only point in my code where stepperValueChanged exists - under the DetailViewController.h, as placed below:
#interface DetailViewController : UIViewController <UISplitViewControllerDelegate>
{
// Create GUI parameters for text fields, text labels, and the stepper:
IBOutlet UITextField *value1;
IBOutlet UITextField *value2;
IBOutlet UITextField *value3;
IBOutlet UISwitch *double_precision;
IBOutlet UILabel *value1_type;
IBOutlet UILabel *value2_type;
IBOutlet UILabel *value3_type;
IBOutlet UILabel *deriv_units;
IBOutlet UILabel *units;
IBOutlet UILabel *result;
IBOutlet UIStepper *stepper;
}
// Define properties of the above GUI parameters:
#property (nonatomic, retain) UITextField *value1;
#property (nonatomic, retain) UITextField *value2;
#property (nonatomic, retain) UITextField *value3;
#property (nonatomic, retain) UILabel *value1_type;
#property (nonatomic, retain) UILabel *value2_type;
#property (nonatomic, retain) UILabel *value3_type;
#property (nonatomic, retain) UILabel *deriv_units;
#property (nonatomic, retain) UILabel *units;
#property (nonatomic, retain) UILabel *result;
// Setup property as instance of UIStepper:
#property (nonatomic, strong) IBOutlet UIStepper *stepper;
// Setup NSString instance for segue linking:
#property (nonatomic, strong) NSString *equationName;
#property (strong, nonatomic) id detailItem;
#property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
// IBActions for the Calculate button and UIStepper instance:
- (IBAction)Calculate:(id)sender;
- (IBAction)stepperValueChanged:(id)sender;
- (IBAction)double_precision:(id)sender;
#end
Any ideas what is going on here? I don't have much of a clue, and if anyone can help explain to me what exactly is in play here while addressing it, I would be more than grateful.
If you need the contents of the implementation file, let me know; I'll edit it in.
Relevant areas of the .m file:
#interface DetailViewController ()
#property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
#end
#implementation DetailViewController
// Synthesize an instance of NSString for segue linking:
#synthesize equationName = _equationName;;
// Synthesize all other variables:
#synthesize value1 = _value1;
#synthesize value2 = _value2;
#synthesize value3 = _value3;
#synthesize value1_type = _value1_type;
#synthesize value2_type = _value2_type;
#synthesize value3_type = _value3_type;
#synthesize deriv_units = _deriv_units;
#synthesize result = _result;
#synthesize units = _units;
#synthesize stepper = _stepper;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
self.title = _equationName;
self.stepper.stepValue = 1;
self.stepper.autorepeat = NO;
self.stepper.continuous = YES;
self.stepper.wraps = YES;
int eqNum;
if ((_equationName = #"Energy-Frequency Relation"))
{
eqNum = 1;
self.stepper.minimumValue = 1;
self.stepper.maximumValue = 3;
}
else if ((_equationName = #"Energy-Frequency-Wavelength Relation"))
{
eqNum = 2;
self.stepper.minimumValue = 1;
self.stepper.maximumValue = 4;
}
// Take _equationName quantization and use it in a switch case to determine the formula that IBAction will use:
if (dflt)
{
switch (eqNum)
{
case 1:
if ((stepper.value = 1))
{
// Change deriv_units appropriately:
self.deriv_units.text = #"Energy (Joules)";
// This is a Planck's constant calculation, we hide the second variable as the constant
// is stored:
self.value2.hidden = YES;
self.value2_type.hidden = YES;
self.value3.hidden = YES;
self.value3_type.hidden = YES;
// Now we set up the parameters of the first entry variable:
self.value1_type.text = #"Frequency (in Hz)";
double frequency = [value1.text doubleValue];
double Planck = 6.626069e-34;
double energy = Planck * frequency;
// Now we set up the return field to return results:
NSString* resultIntermediate = [NSString stringWithFormat:#"%f", energy];
self.units.text = #"J";
}
// Identical function statements under ViewDidLoad truncated
}
bool dflt;
-(IBAction)KeyboardGoAway:(id)sender
{
[self.value1 resignFirstResponder];
[self.value1 resignFirstResponder];
[self.value1 resignFirstResponder];
}
-(IBAction)double_precision:(id)sender
{
// Sets double-float 'truth' value depending on state of UISwitch:
if (double_precision.on)
{
dflt = TRUE;
}
else
{
dflt = FALSE;
}
}
#pragma mark - Calculation runtime
-(IBAction)Calculate:(id)sender
{
// Assigns numerical information to _equationName data -
// switch case can only handle integer literals
// Also handles stepper incrementation and UILabel/UITextView hiding
NSString* resultIntermediate;
self.result.text = resultIntermediate;
}
The trailing colon makes the difference. Your action method is stepperValueChanged:,
but from the error message it seems that you connected the stepper to stepperValueChanged.
There are two reason for these kind of issues.
Probable case 1:
You first declared the function like - (IBAction)stepperValueChanged;
Connected the IBAction to stepper
Changed the method to - (IBAction)stepperValueChanged:(id)sender;
Solution:
Delete old connection in the interface builder and connect it again.
Probable case 2:
In your code you are calling the method using a selector where you written like: #selector(stepperValueChanged)
Solution:
Change the selector like: #selector(stepperValueChanged:)
Usually this means you are missing the method in your .m or you might of misspelled stepperValueChanged.
Edit: Actually, I believe it needs to be stepperValueChanged: with a semicolon.

Resources