Sorry in advance how I've written this..
In my Main.StoryBoard I have: UITextField (numeric), UILabel and UIButton.
I would like to:
Click UIButton
Take number from UITextField
Give corresponding 'word' and place in UILabel.
Lets say my numbers are in range from 1-9.
I'm having trouble linking the numbers with words and placing into the UILabel.
Is it best to use an NSArray or perhaps a Case Switch?
CODE
int num = [self.stringEntry.text intValue];
THEN... included NSArray of numbers and words correctly.
self.numberOneList = numberOneArray
self.wordOneList = wordOneArray
if (num <= 0 || num >= 10) {
self.wordLabel.text = #"Try a number between 1 and 9";
} else{
// what would I type here?
You can use NSDictionary in this case. Keys can be numbers (0-9) and values can be respected word. Then just take the value from the textfield and check it against the dictionary using valueForKey:.
Related
I have this code that uses DDUnitConverter for currency conversion.
#import "DDUnitConverter.h"
#import "DDCurrencyUnitConverter.h"
- (void) convertCurrency {
DDUnitConverter *converter = [DDUnitConverter currencyUnitConverter];
NSNumber *from = [NSNumber numberWithInt:42];
NSNumber *to = [converter convertNumber:from fromUnit:DDCurrencyUnitUKPoundSterling toUnit:DDCurrencyUnitUSDollar];
NSLog(#"new value: %#", to);
}
I want to set the fromUnit: and toUnit: arguments based on the user's selection. How should I do that?
DDCurrencyUnit is an enum (enumerated type). If you use a picker view for your user to pick a currency, you can use the selected row index for fromUnit and toUnit, as long as the rows in the picker are in the same order as they are in the enumeration.
For example, DDCurrencyUnitEuro is 0, DDCurrencyUnitJapaneseYen is 1, DDCurrencyUnitUKPoundSterling is 2 and so on. So if the first row of your picker view is "Euro", your second is "Japanese Yen", your third "UK Sterling" and so on, then the selected row index will correspond to the unit parameter.
Your example above is equivalent to
NSNumber *to = [converter convertNumber:from fromUnit:2 toUnit:3];
I have the following code:
int index = [self.colorlist count];
int random1 = (arc4random() % index);
NSString *color1 = [self.colorlist objectAtIndex:random1];
[Button1 setTitle:color1 forState:UIControlStateNormal];
I want to compare the Button 1 text to a different label with .isEqualTo but using
if button1.text isEqualToString: label
does not work. What is the proper syntax for calling button 1's text, rather than button1.text. Thanks!
There are two ways:
button.currentTitle
And
button.titleLabel.text
The first is a read-only reference to the current text on the button.
The latter is a readwrite reference to the text property of the label on the button. The label reference however (button.titleLabel) is itself readonly.
you can easily do it by following code
if([button.currentTitle isEqualToString(textField.text)])
{
//Succes part
}
else
{
//Failure Part
}
Hope this answer may help you
I'm fooling around in XCode, trying to learn a little about the iOS SDK and Objective-C.
I have this for loop below, and it should print out several values to the screen (depending on the amount of months chosen), but instead, it's only printing out the final value.
Can anybody point out why?
Thanks a bunch, in advance!
for (int i = 1; i <= myMonthsDouble; i++)
{
myPaymentAmount = (myBalanceDouble/10) + myInterestDouble;
myBalanceDouble -= myPaymentAmount;
//convert myPaymentAmount double into string named myPaymentAmountString
NSString *myPaymentAmountString = [NSString stringWithFormat:#"%f", myPaymentAmount];
NSString *paymentInformation = [[NSString alloc] initWithFormat:#"%# months, %# per month.", monthsString, myPaymentAmountString];
myInterestDouble = (myBalanceDouble * (myInterestDouble/100))/12;
self.label.text = paymentInformation;
}
It is only printing the last value to the screen because you only have one label. Each time you get to the end of the loop, you are setting that label's text which is overriding the last value. If you want to print all of them to the screen, you will need to have either multiple labels or you will have to append the strings together and put them in either a label or a UITextView that is formatted so that they can all be seen (most likely a text view but it can be done with a label.)
One example of doing this would be:
label.text = [label.text stringByAppendingString:newString];
numLines++; //this starts at 0;
and then at the end:
label.numberOfLines = numLines;
I've got a pop-up view that loads when a user clicks on a TableView with Core Data elements. On the pop-up view I have a label that represents an int value.
The pop-up view has two butons, one for decreasing the value of the label by 1 and one for increasing it by one. So + and -
What I want to do is to disable the minus button if the label's value is 0. What I've tried is:
-(void)viewDidLayoutSubviews{
NSString *daString = currentVal.text;
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:daString];
int number = [myNumber intValue];
if (number==0)
minus.enabled = NO;
else
minus.enabled = YES
}
The problem with my code is that the button stays disabled after I increase the label's value, and it's no longer equal to 0.
Any suggestions?
You should keep a reference to minus button e.g.
#property (strong, nonatomic) IBOutlet UIButton *minusButton;
Set it with a value of your minus button, or connect outlet in Interface Builder
in your action handler for plusButton, do something like that
-(IBAction)plusAction:(id)sender {
//Do your business logic
...
self.minusButton.enabled = YES;
}
//In your minusButton action handler
-(IBAction)minusAction:(id)sender {
//Do your business logic
...
NSString *daString = currentVal.text;
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:daString];
int number = [myNumber intValue];
if (number==0)
self.minusButton.enabled = NO;
else
self.minusButton.enabled = YES
}
It seems like you have things the other way around. I would take a totally different approach:
Keep an instance variable (which we'll call 'count') in this viewController which holds the number. it can be an NSInteger. now add a target (self) to both buttons with a #selector(buttonPressed:). now this is how this selector should look like:
- (void)buttonPressed:(id)sender{
if (sender==plusButton)
self.count++;
if (sender==minusButton)
self.count--;
label.text = [NSString stringWithFormat:#"%d",self.count];
minusButton.enabled = (self.count>0);
}
I would just do this with a UIStepper, instead of the 2 buttons. You can set properties right in your storyboard/IB file that specify the max and min, increments, and a bunch of other useful things too. There are a couple video tutorials posted on YouTube that probably cover everything you'll need to know to use it.
Also, I have noticed one thing that...
If the button in disabled state and you are trying to change the title of normal state, it wont work.
I had to change the state to enabled and then I could manipulate title and set back to disabled.
I currently use a UIStepper in a custom UITableViewCell hooked up to a UITextField to have people select how many items they want to add to their Shopping Cart.
To check if the object is in stock, I've got two numbers: The LocalStock and the TotalStock.
I want to do the following:
If the amount of objects falls into the local stock, I want the textfield the number is displayed in to turn green.
If the amount of objects falls into the supplier stock (so either there is no local stock, or the stepper value is higher than the local stock, so we need to get it from the supplier stock) turn the UITextField blue.
If neither the supplier stock nor local stock are sufficient, I want the textfield to turn yellow.
I got the following code:
- (IBAction)stepperValueChanged:(id)sender
NSLog(#"localstock: %#",localstock);
NSLog(#"TotalStock: %#",totalstock);
NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithString: self.textField.text];
if (value <= localstock)
{
self.aantalTextField.backgroundColor = [UIColor greenColor];
NSLog(#"Value %# <= Localstock %# >GREEN< Totalstock: %#",value,localstock, totalstock);
}
else if (value <= totalstock)
{
self.aantalTextField.backgroundColor = [UIColor blueColor];
NSLog(#"Value %# <= totalstock %# >BLUE< Localstock: %#",value,totalstock,localstock);
}
else
{
self.aantalTextField.backgroundColor = [UIColor yellowColor];
NSLog(#"Value: %# LocalStock: %# TotalStock %# >YELLOW<",value,localstock,totalstock);}}
And it's not making much sense when I run it... Sometimes it catches the GREEN-statement, other times the BLUE, and sometimes the same value returns YELLOW.
Anyone care to take a look at this and show me where the (logical) error is?
Thanks in advance!
You are comparing objects, not their values. As long they are both of type NSDecimalNumber...you need to compare similar to this.
[value integerValue ] <= [localstock integerValue]
As far as I understood from your code, localstock, totalstock and value are objects, not integers and you are comparing objects pointers, not values.
Instead, you should use the
- (NSComparisonResult)compare:(NSNumber *)decimalNumber
declared in NSDecimalNumber class.
Or convert all to integers using for example
[value intValue]