I need the user in the interface builder text field to enter a number and i want to read it as number i think the system read it as string and no output
here is my code if you can kindly assist
in the interface
{
IBOutlet UITextField *arr_values;}
#property (nonatomic ,retain) IBOutlet UITextField *arr_values;
and in the .m file
int inputNumber = [arr_values.text intValue];
NSLog(#"%#",inputNumber);
but no output when i enter # in the Interface builder
with thanks
You can get value from textField like this.
float val=[txtField.text floatValue];
replace the %# by %d, as for an integer we have %d:
NSLog(#"%d",inputNumber);
As for the custom keyboard, in the viewDidLoad method add the line:
[inputNumber setKeyboardType: UIKeyboardTypeNumberPad];
Related
I wanted to give a string value to UIButton tag for unique identities.
I tried this code.But it doesnt work.
NSString *A;
NSString *B;
NSString *C;
NSString *D;
firstOptionABtn.tag=[A integerValue];
secondOptonBbtn.tag=[B integerValue];
thirdOptionCbtn.tag=[C integerValue];
fourthOptionDbtn.tag=[D integerValue];
- (IBAction)BtnClicked:(UIButton *)sender {
NSLog(#"%ld",(long)sender.tag);
}
I don't like this way but it will print 0 every time. Where am I going wrong?
Please help. Thanks
It returns zero as you did not initialize string, and it has null value when you convert it into integer it returns you zero. And it seems that you create your button from NIB so you can set their tag value from there and then get there value by implementing.
-( IBAction )buttonClicked:(id)sender
{
UIButton *btn= (UIButton *)sender;
NSLog(#"%d",btn.tag);
}
but if u want the output A,B,C, D then there is also a way
-(void)viewDidLoad{
btn1.accessibilityLabel=#"A";
btn2.accessibilityLabel=#"B";
btn3.accessibilityLabel=#"C";
btn4.accessibilityLabel=#"D";
}
-(IBAction)buttonClicked:(id)sender {
UIButton *btn= (UIButton *)sender;
NSLog(#"%#",btn.accessibilityLabel);
}
output is A, B, C, D
What need to use string for set tag value of button... why not set tag of Button statically simply set like :-
firstOptionABtn.tag=1;
First time seen this setting tag with string to int convert
In your queston First is you are not seting the value of you string
NSString *A;
NSString * B;
NSString * C;
NSString * D;
if you set value of string like
A=#"2"
then your code working and you got the value at the button click event
firstOptionABtn.tag=[A integerValue];
its output is
sender tag 2
Into you String value consider any Number value then the covert string to Intiger cosider only Number not alphabet character like
for example
If you set value of string like A=#"2aaabbb" then the output of button tag is 2 But if you set string value like A=#"bb2aaabbb" then output of button tag is Always 0 consider.
UPDATE
if you wish to set character to int
you can convert NSString to ASCII Like Bellow
A=#"A"
int ASCIINumber = [A characterAtIndex:0];
button.tag=ASCIINumber;
OUTPUT IS:- 65 for Upper case if lower case OUTPUT IS 97
But as using above method i suggest to you use simply set tag=intNumber instead of doing above.
It's not possible dear to set string in tag. Tag only takes int value as you said you know.
Create a custom view which is composed of UIButton and a NSString property or an ivar which will serve as tag.
#interface MyButton: UIView
#property(nonatomic, strong) UIButton *button;
#property(nonatomic, strong) NSString *uniqueTag;
#end
Hope that helps!
we can make a category for tag string in UIButton.
#interface UIButton (setTag)
#property(nonatomic, retain) NSString *tagString;
#end
And any where in your file you can set the tag string to your UIButton.
e.g
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTagString:#"Hello"];
you can always create a custom class containing a string property to go along with said classes
for example : create a custom class for UIButton
and then at the Header File put :
#property (nonatomic, strong)NSString *labelTag;
- (id)initWithFrame:(CGRect)frame withLabelTag:(NSString *)str;
and then, at the implementation create a super method of init with frame so it looked like this :
- (id)initWithFrame:(CGRect)frame withLabelTag:(NSString *)str{
self = [super initWithFrame:frame];
if (self) {
//Init code
_labelTag = str;
}
return self;
}
after that, whenever you want to set or call the string tag of the button you only need to declare it and call it like this :
MyCustomButton *button = [[MyCustomButton alloc]initWithFrame:frame withLabelTag:#"this is the title"];
//and to call it :
NSLog (#"the button's tag is :%#", button.labelTag)
or, you could always just use the button's title property and if you don't want it to show on your button, just set its HIDDEN prop to YES
There are two scenarios.
Case 1: If your tag string contains only numbers - (Eg: tagStringValue = "1433")
Use myButton.tag = [tagStringValue intValue];
Get back the button using
UIButton *myButton = ([[self.view viewWithTag:[tagStringValue intValue]] isKindOfClass:[UIButton class]])?(UIButton *)[self.view viewWithTag:[tagStringValue intValue]]:nil;
Case 2: If your tag string contains numbers and strings - (Eg: tagStringValue = "ss1433kk")
Subclass the UIButton and add a property like:
#property (nonatomic, strong) NSString *stringTag; and use this instead of default Tag.
I'm new to the Objective-c language. I'm trying to create an app that has a button and a label. The button should display some text which I did already. The only problem is that when I click the button, it only adds the specified text once. I want it to keep adding the same text to the label each I time I press the button.
Here is my .h file
{
IBOutlet UILabel *label;
}
-(IBAction)btnClcik:(id)sender;
Here is the .m file
-(IBAction)btnClcik:(id)sender
{
label.text=#"test";
}
To append to the existing text, use the string's concatenation method...
label.text = [label.text stringByAppendingString:#"test"];
You need to append to the string?
Then do
label.text = [label.text stringByAppendingFormat:#"%#", textToAdd];
where textToAdd is a NSString or some other valid object where %# is the correct format specifier.
I can't format a label, here's my code:
ct.text = [NSString stringWithFormat:#"%i:%i", currentTimeMin, currentTime];
so ct is the label and it won't display the value.
ct in the .h file: #property (nonatomic, retain) IBOutlet UILabel *ct;
I used (strong, nonatomic) as well but that didn't change anything
currentTimeMin is set to 0 and currentTime to self.audioPlayer.currentTime I think the problem could be there.
I have also tried to put ct.text = [NSString stringWithFormat:#"hey"]; didn't work either
Please help me! I am getting so sick of this. Thanks!
**EDIT**
And a weird thing is that I set up a label in another ViewController and then it worked but it's just in that single view that it won't work. Somehow, someway.
Judging from your comments and tests...
Either
(1) You are not referring to the label correctly. It should be self.ct
Or
(2) You have not wired up the label correctly. Please check it again. If you mean to assign content to a label.text but have not connected it to the property you will get a null result (as far as the program is concerned you have not alloc/init'ed the property).
Hey
Just wondering how do you..
Iv created some code that automatically creates a certain amount of UITextField input by the user.
Each UITextfield has a set tag automatically created.
The user then inputs his value into each of the UITextFields.
I want to retrieve the input text from the UITextFields which correspond to the tag.
I thought I nearly had it with:
NSString * tmpV = (NSString*)[choiceTextField.text viewWithTag:result];
where result is a increment, choiceTextField is the UITextField. With this I get a problem not defining instance, which I can't do as the UITextFields are generated in code not on the xib.
So to sum up, basically want the retrieve the text from a UITextField with specific tag.
Thanks
Dan
UITextField *yourTextField = (UITextField *)[self.view viewWithTag:result];
NSString *getText = myTextField.text;
Hope this helps
Your textfields must be a subview of a parent view. You need to write the following in your view controller of the parent view:
NSString *text = ((UITextField*)[self.view viewWithTag:result]).text;
Just go with Hetal Vora's solution: looks much cleaner!
-viewWithTag can be called on any view and searches all its subviews for one that matches the tag. Thus, you will find your UITextField by calling it on any view that is above it in the hierarchy. For example, you could call it on the view controller's view that contains your text field, as follows:
NSString *tmpV = [[myViewController.view viewWithTag:result] text];
If you are already in the view controller's class you could use:
NSString *tmpV = [[self.view viewWithTag:result] text];
On second thought, the following may be more correct & will avoid any compiler errors:
UITextField *myTextField = (UITextField *)[self.view viewWithTag:result];
NSString *tmpV = myTextField.text;
I want a counter that shows in a UILabel how many charachters the UITextView has.
I tried this:
IBOutlet UITextView *twittermessage;
UIActionSheet * loadingActionSheet;
IBOutlet UILabel * anzahl;
Here the .m:
- (void)textViewDidChange:(UITextView *)twittermessage{
int count = [twittermessage.text length];
anzahl.text = (NSString *)count;
}
Thanks for your help and sorry for my bad English.
You can't cast an integer to a NSString using C-style casts. You have to apply a string formatter to count to get a string representation of count.
Your code should look like:
- (void)textViewDidChange:(UITextView *)theTwitterMessage{
int count = [theTwitterMessage.text length];
anzahl.text = [NSString stringWithFormat:#"%d",count];
}