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];
Related
I have two text views beside each other with multiple lines of text.
How can I make them both scroll together based on scrolling either one of them at the same time?
First
#interface ViewController : UIViewController <UITextViewDelegate>
second make both textViews delagates
tv1.delegate = self
tv2.delegate = self
Implement
-(void)scrollViewDidScroll:(UIScrollView *)inScrollView {
self.tv1.contentOffset = inScrollView.contentOffset;
self.tv2.contentOffset = inScrollView.contentOffset;
}
Here is a demo txView
Edit
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextViewDelegate>
#property (weak, nonatomic) IBOutlet UITextView *tv1;
#property (weak, nonatomic) IBOutlet UITextView *tv2;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tv1.delegate = self;
self.tv2.delegate = self;
}
- (void)scrollViewDidScroll:(UIScrollView *)inScrollView {
self.tv1.contentOffset = inScrollView.contentOffset;
self.tv2.contentOffset = inScrollView.contentOffset;
}
The goal of my app is to calculate two fields that will return in a text box. Unfortunately I'm receiving an error regarding with unknown type field referring to my TotalField.text field. I have two views. One is the home screen which refers to the second view through a button and the second view contains these fields and labels that I'm trying to target.
This is my code.
ViewController.h
// SalaryCalcApp
//
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *Total;
#property (strong, nonatomic) IBOutlet UILabel *HourlyRate;
#property (strong, nonatomic) IBOutlet UILabel *NumberOfHours;
#property (nonatomic, strong) UITextField *TotalField;
#property (nonatomic, strong) UITextField *HourlyRateField;
#property (nonatomic, strong) UITextField *NumberOfHoursField;
#end
ViewController.m
// SalaryCalcApp
//
//
//
#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.
}
#synthesize TotalField;
#synthesize Total;
#synthesize HourlyRate;
#synthesize NumberOfHours;
#synthesize HourlyRateField;
#synthesize NumberOfHoursField;
TotalField.text = [ NSString stringWthFormat: #"%i", (HourlyRateField.text.intValue * NumberOfHoursField.text.intValue) ];
#end
The error resides in this code:
TotalField.text = [ NSString stringWthFormat: #"%i", (HourlyRateField.text.intValue * NumberOfHoursField.text.intValue)];
Error Message:
Unknown type name 'TotalField' and Expected identifier or '('
Thank you everyone for your contributions.
Why is that code not nested inside a function?
Put it inside your viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TotalField.text = [ NSString stringWthFormat: #"%i", (HourlyRateField.text.intValue * NumberOfHoursField.text.intValue) ];
}
UPDATE Just resetted al Simulators Data and now it works fine.
I'll appreciate any help :)
My app writes data to a Data.plist file in a subfolder of Documents.
I added an NSLog to find out the path so I can check it. I finded and the plist is good.
I tried it in a iPad Air. Another page has a "Load" button for loading the plist data and placing it in a textfield. In the iPad Air the info is loaded totally fine.
BUT! I tried it in an iPad 2. And the 2 strings are not loaded. The keys and objects are:
proyectName string example
revisarCimiento bool 1
tipoCodigoEsfuerzosAdmisibles string Canada
tipoUnidadMamposteria string Block
mamposteriaConfinada bool 0
When the data is loaded, the proyect name doesn't depend on the plist.
The bools are fine but the other 2 strings are not loaded.
Here is the code
.h
//
// factoresCombinacionesCargaViewController.h
// calcMurosLosas
//
// Created by Brian Matus on 15/12/14.
// Copyright (c) 2014 Matus. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface factoresCombinacionesCargaViewController : UIViewController <UITextFieldDelegate>
- (IBAction)loadData:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *proyectName;
#property (weak, nonatomic) IBOutlet UITextField *levels;
#property (weak, nonatomic) IBOutlet UITextField *revisarCimiento;
#property (weak, nonatomic) IBOutlet UITextField *tipoCodigoEsfuerzosAdmisibles;
#property (weak, nonatomic) IBOutlet UITextField *tipoUnidadMamposteria;
#property (weak, nonatomic) IBOutlet UITextField *mamposteriaConfinada;
+ (void)proyectPath:(NSString *)theString;
#end
.m
#import "factoresCombinacionesCargaViewController.h"
#import "infoGeneralViewController.h"
#interface factoresCombinacionesCargaViewController ()
#end
NSString *proyectPath;
#implementation factoresCombinacionesCargaViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)loadData:(id)sender {
[infoGeneralViewController getProyectNameFromPlist]; //This calls a method that returns a string tho the proyectPath method below.
[self performSelector:#selector(setLabels) withObject:nil afterDelay:0.1];
}
+ (void)proyectPath:(NSString *)theString {
proyectPath = theString;
}
-(void)setLabels {
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:[proyectPath stringByAppendingPathComponent:#"Data.plist"]];
_proyectName.text = [proyectPath lastPathComponent];
int leves = [[plist objectForKey:#"numeroNiveles"] integerValue];
_levels.text = [NSString stringWithFormat:#"%d", leves];
bool revisarCimiento = [[plist objectForKey:#"revisarCimiento"] boolValue];
_revisarCimiento.text = [NSString stringWithFormat:#"%s", revisarCimiento ? "Si" : "No"];
_tipoCodigoEsfuerzosAdmisibles.text = [plist objectForKey:#"tipoCodigoEsfuerzosAdmisibles"];
_tipoUnidadMamposteria.text = [plist objectForKey:#"tipoUnidadMamposteria"];
bool mamposteriaConfinada = [[plist objectForKey:#"mamposteriaConfinada"]boolValue];
_mamposteriaConfinada.text = [NSString stringWithFormat:#"%s", mamposteriaConfinada ? "Si" : "No"];
}
#end
I don't why in the iPad Air the strings are loaded fine but in the iPad 2 not.
Thanks for your attention.
I'm working on a app in Xcode and have run into a problem.
My app needs to collect input from a TextField (where the user enters information) an put it into my equation & eventually give the user a result.
For example - a
user enters their Weight, Height and Age.
My app then needs to take these inputs and place them into the following equation:
Men: RMR = 66,473 + (13,751*WEIGHT) + (5,0033*HEIGHT) - (6,755*AGE)
But how do I code this? What I have made so far:
My .h file is as follows:
#import <UIKit/UIKit.h>
IBOutlet UITextField *Weight;
IBOutlet UITextField *Height;
IBOutlet UITextField *Age;
IBOutlet UILabel *rectResult;
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *Weight;
#property (weak, nonatomic) IBOutlet UITextField *Height;
#property (weak, nonatomic) IBOutlet UITextField *Age;
#property (weak, nonatomic) IBOutlet UILabel *rectResult;
-(IBAction)calculate:(id)sender;
#end
and my .m file:
#implementation ViewController
-(IBAction)calculate:(id)sender {
float floatRectResult=[Weight.text floatValue]*
[Age.text floatValue];
NSString *stringRectResult=[[NSString alloc]
initWithFormat:#"%1.2f",floatRectResult];
rectResult.text=stringRectResult;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
PS.: Sorry for my English - Can anybody help me? :)
Try something like this:
-(IBAction)calculate:(id)sender {
//66,473 + (13,751*WEIGHT) + (5,0033*HEIGHT) - (6,755*AGE)
float result = (66473 + (13751*[Weight.text floatValue]) + (50033*[Height.text floatValue]) - (6755*[Age.text floatValue]));
rectResult.text = [NSString stringWithFormat:#"%.2f", result];
}
Since you appear to be using some constants, it may be better to #define them so that you can easily change them later. Example:
//Above your implementation
#define kWeightConst 13751.0 //Can be used later as just kWeightConst
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)];