reading numbers from UIbutton - ios

I'm new in Objective-c and Xcode. I'm trying to get large numbers from certain buttons but all I got is only one number. I'm using button tag for that.
For instance: if I want to add two numbers 2+3, it works well. but when I want to add 230+32, it doesn't.
Interface :
- (IBAction)getnumber:(id)sender;
#property (strong, nonatomic) IBOutlet UILabel *Result;
int number;
Implementation part:
-(IBAction)getnumber:(id)sender {
number = [sender tag];
Result.text = [NSString stringWithFormat:#"%i", number];
}
Is there any way to get a large number from button's tag, if I tapped more than one button?
Thank you in advance .

Your problem comes from how you are defining your variables. If I understand your setup right, you have something like a calculator interface, and you are only setting button's tags to single digit numbers like 1, 2, 3, ... to indicate the next digit to display?
In that case your line number = [sender tag] will set a global variable to that button's number (remember when your CS prof told you never to use globals? Here's a reason why!) Since you just overwrote number with this button's tag when you go to set your result string in the next line, number only holds the value of the last button pressed. Instead you should do something like this.
#interface MyClass : UIViewController
#property (nonatomic, strong) IBOutlet UITextView * resultLabel;
- (IBAction)getNumber:(id)sender;
#end
and
- (IBAction)getNumber:(id)sender;
{
self.resultLabel.text = [NSString stringWithFormat:#"%#%d", self.resultLabel.text, [sender tag]];
}
In this way, every time getNumber: is called, it takes whatever text the label is currently displaying and append's this button's value. As a side note, its conventional to start Objective-C property names with a lowercase letter.

I think you have to attach a variable type to number. It should be -
- (IBAction)getnumber:(id)sender
{
NSInteger number = [sender tag];
Result.text = [NSString stringWithFormat:#"%i", number];
}
I

You need to change your logic.
number = 0;
if button with number is clicked
number = number * 10 + button.tag;
else
if operation (not =) is pressed
store the number and operator
if = operatior is clicked
perform the operation on stored number and recently entered number.

Related

Get the text from a Text Field / Text View

I'm actually in big troubles on how to get the Text from a TextField (and optionally a TextView ).
I'll try to make it simple: I have my PostIt.xib which is composed of 2 labels (which I don't really care about) and also one TextField and one TextView. Here is how I tried to get the text from these:
First, in my PostIt.h :
#interface PostIt : UIView {
IBOutlet UITextField *titre;
IBOutlet UITextView *commentaire; }
Then secondly, in my PostIt.m : (the real action of this method is that it close a view and normally throw back the information I want to get to another view, here: parent )
-(IBAction)doubleTap:(UITapGestureRecognizer *)recognizer{
[_parent setTitre:titre.text];
[_parent setCommentaire:commentaire.text];
[_parent setIsEdited:true];
[self removeFromSuperview]; }
My problem here is, when I call a NSLog (for example) to show me the Strings which are caught (probably a mistake here? sorry) it show me every time : (null)
I have been looking and trying a lot of answer i found but no one seems to be able to solve my problem...
If someone could help me it will be really nice, thanks in advance :)
Is there any more code pertaining to the UITextField?
Based on what I see here you need you first convert the input from the UITextField
into a string and then you can set the string where you want.
Updated to add the conversion code,
NSString *stringFromTextField = [yourTextField text];
Here is some more details,
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *yourTextField;
#end
Your Action,
- (IBAction)yourAction:(id)sender {
//Converting UItextfields into strings
NSString *stringFromTextField = [self.yourTextField text];
}
Here is a sample project I made for you on GitHub -
stringFromTextView
You should try to input to your code an object part if that's not already done, where you could directly pick up the data that you need and that's also from here that you should update your view.

How to increase UILabel value on every click of UIButton?

I am working with a UILabel and assign some string value on it, now I want to update its value every time when I click on UIButton. See me IBAction code.
- (IBAction)IncreasePack:(id)sender {
int increseprice = [value intValue];
int updateprice = [price intValue];
NSString *newprice= [NSString stringWithFormat:#"%d", increseprice+updateprice];
_TotalPrice.text = newprice;
NSLog(#"newprice %#",newprice);
}
When I click button the first time, my logic executes fine. But when I tap again the code is not executed but its print every time on every click.
Try this:
in .h file
#property (strong, nonatomic) int labelValue;
in .m file
- (void)viewWillAppear{
self.labelValue = 0;
}
- (IBAction)buttonClicked:(UIButton *)sender{
self.labelValue++;
[self.label setText:[NSString stringWithFormat:#"%d", self.labelValue]];
}
Your code does not make sense. Your IBAction calculates newprice from increseprice+updateprice. If increseprice and updateprice don't change, the sum of those values won't change either.
You need an instance variable that hold the current value, and when the user clicks your button you need to add the increase value to that previous value and update the total
Instead you always say
newprice = increseprice + updateprice
Nowhere do you save the new total. Click the button again and neither increseprice update price will have changed, so newprice will still be the same value.

how to insert number like in stack and delete like pop

I am using buttons and I assigned tag 0 to 10 . Then I made an action to get the clicked button's tag, and now I want to display the tag in a label . Also I have a cancel button C. If user wants to delete any number, he can click C button that I want to remove number from the label .
This is my screenshot to touch the number
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Ezywire";
addnum=[[NSMutableArray alloc]init];
numbber=[[NSString alloc]init];
}
- (IBAction)NumberAction:(id)sender {
NSInteger tagvalue = [sender tag];
NSString *current=[NSString stringWithFormat:#"%ld", (long)tagvalue];
[addnum addObject:current];
NSString *temp;
for ( int i=0; i<[addnum count]; i++) {
numbber=[numbber stringByAppendingString:[addnum objectAtIndex:i]];
}
NSLog(#"data===%#",numbber);
ValueLable.text= numbber;
}
But in the label I am getting repeated number like this. How to implement this.
For example if user enters 2 then in the label
2
then he enters 7 then in the label
27
then he entered 9 then in the label
279
........ like this .
If user clicks C, then it remove from label last value is (last value removed)
27
The problem in your code is that numbber is initialized when the view is loaded, and never gets cleared again. However, each time a button is pressed, the whole addnum of digits gets appended to num again, creating repeated digits.
Fix this by removing num as an instance variable, making it a local to NumberAction: method, and setting it to an empty string every time the number is pressed.
Since you are planning to support the clearing action as well, you should make a private method that combines the digits from addnum array into a string. This way your NumberAction: and ClearAction would share the code that formats the array and sets the label. Your NumberAction: method would append a number and call FormatAndSetLabel, while the ClearAction method would remove the last digit if it is available, and call FormatAndSetLabel as well:
- (IBAction)NumberAction:(id)sender {
NSInteger tagvalue = [sender tag];
NSString *current=[NSString stringWithFormat:#"%ld", (long)tagvalue];
[addnum addObject:current];
[self FormatAndSetLabel];
}
- (IBAction)ClearAction:(id)sender {
if (!addnum.count) return;
[addnum removeLastObject];
[self FormatAndSetLabel];
}
-(void)FormatAndSetLabel {
NSMutableString *temp = [NSMutableString string];
for ( int i=0; i<[addnum count]; i++) {
[temp appendString:addnum[i]];
}
ValueLable.text= temp;
}
Also it might be interesting for you to have a look at Paul's Hegarty Stanford iOS development course (iPad and iPhone Application Development, Fall 2011)
https://itunes.apple.com/ru/itunes-u/ipad-iphone-application-development/id473757255?mt=10
Calculator app is used here as an example. Must see for the beginners.

Get a float value when there is no getter method for WKInterfaceLabel

I want to get a float value from a WKInterfaceLabel. As there is no getter method for WKInterfaceLabel, using float x = ([_someText.text floatValue]); will just give me no getter method error. Any ideas?
I tried here Compare WKInterfaceLabel Text to Another NSString to find a solution, using self.label.accessibilityValue simply returns nil when acessing the accessibilityValue
A few different solutions / ideas:
I would make your WKInterfaceLabel a property and synthesize it, then you are able to retrieve float values from your label just like you would with a regular UILabel. I've successfully done this with no issues. I checked out the post that you linked and they said there is currently no way to retrieve the text values... I don't know if something was updated or not because I can retrieve float values, text values etc in WatchKit just fine. For retrieving or settingy code looks exactly like this:
(Declared privately in the .m)
#property (weak, nonatomic) IBOutlet WKInterfaceLabel *tempoLabel;
//
- (void)setTempoFromInteger {
self.tempoLabel.text = #"";
if (tempo > 0 && tempo < 999) {
NSString *tempoString = [NSString stringWithFormat:#"%i", tempo];
self.tempoLabel.text = tempoString;
}
}
Now if you're trying to just receive a random float value rather than create, you had to of retrieved or set that float value SOMEWHERE in your code or if retrieved from a server, somewhere in the data. Save that, and use it.

UILabel Doesn't populate random strings chosen from NSMutable Array

I am trying to populate a UILabel with strings randomly chosen from an array. For some reason, the random choosing from array doesn't occur (the label always displays the first element of the array).
//.h file has declaration as follows
NSMutableArray *array;
//.m file
- (void)viewDidLoad
{
array = [[NSMutableArray alloc] initWithObjects: #"abc", #"def", nil];
}
and then, I have a method called orientationPopulate that does this:
-(void)labelPopulate;
{
int randomArrayIndex = arc4random() % array.count;
//Setting the label's text
_label.text = array[randomArrayIndex];
}
Based on the label displayed randomly, I would click buttons on the screen. So the label must keep changing each time. I do not know if I'm missing some link here. Can somebody help?
randomArrayIndex is 0 or 1, it may not change every time you click the button

Resources