Each button click adds text to current label objective-c - iphone

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.

Related

Recognizing all objects generated by a loop

This question is a bit related to one I posed previously entitled "Recognizing UIButton from a loop".
In this case, I have generated a bunch of UITextFields with a loop. When the value of one of them is changed, I'm able to recognize which textfield it was.
However, I want to then edit every textfield that was generated. Specifically, I get input from the one textfield that was edited, and I want to recalculate the other textfields based on the input and name of the recognized textfield.
How do I call for every one of the other generated, non-edited textfields to be modified?
/Vlad
Since you are already using tags, this is what viewWithTag is used for:
// Get a reference to the textfield with tag 3
UITextField *textField3 = (UITextField *)[self.view viewWithTag:3];
// Calculate your new value
float result = 4.32; // Calculate the value that you want the textfield with tag 3 to display
// Change the contents
textField3.text = [NSString stringWithFormat:#"%f", result];
store the text fields in an array, then when you want to change the value
[self.myTextFieldArray enumerateObjectsUsingBlock:^(UITextField *textField, NSUInteger idx, BOOL *stop){
if (![textField isEqual:theTextFieldThatWasEdited])
textField.text = #"whatever text you want";
}];

Change Label Text

How can I change a label's text? This is what I have tried.
labelCheckBox.text = #"This is the new label";
I connected the label in Interface builder for labelCheckBox and created an outlet for it. In the Didload the label's text changed, but in another method inside the class it does not have the new value.
-(void)settext:(NSString*)newLabelText{
labelCheckBox.text = newLabelText;
// labelCheckBox = [[UILabel alloc] init];
// labelCheckBox.text = #"This is the new label";
}
I am calling the method in another class as shown here:
[New settext:#"All Transaction"];
The value of 'NewLabelText' after printing it in NSLog is: " All Transaction ". But when I assign ' LabelCheckBox.Text = NewLabelText ', I print the value of 'LabelCheckBox.Text',
and it gives empty.
First of all if you use IB you need to create outlet and connection for your UILabel. The easiest way it's dragging from your label (control + drag) to your .h file of controller class. Then synthesize it in .m file and now you can change label text as you do this in question.
the 'View' should load before setting the label text.
iOS development sometimes gives you the silence treatment like Javascript to help prevent your app from crashing. With that said, if you have created the UILabel in Interface Builder and have set an IBOutlet for it, make sure you change the text only after the UILabel is created. (i.e. after -viewDidLoad). You can set a break point to where you set the text change and a breakpoint at -viewDidLoad in the UIViewController that contains the UILabel to confirm the order of calls that's taking place.
Below code will change text in label with tag value
In scrollview use this type to change the text in label
for(UIView *views in ScrollViewMain.subviews)
{
UILabel *label = [views viewWithTag:[sender tag]+1000];
label.text=str1;
}
It will really work.
thank you

Appending data in UITextField programmatically

I am creating a report & printing the same on a UITextView.My report has some titles and there respective subtitles. I am printing 1st title then its subtitle & then second title and its subtitle and so on. I want different font and font sizes for the titles and subtitles.
But the problem is the text field always prints the last subtitle.If any one know solution to this problem,let me know.
Here is what i have dome in my .m file
- (void) printData {
// data is UITextView object
[data setFont:[UIFont fontWithName:#"Helvetica" size:15]];
NSString *title = #"TITLE" ;
data.text = title;
[data setFont:[UIFont fontWithName:#"Arial" size:15]];
NSString *subtitle = #"SUBTITLE" ;
data.text = subtitle;
}
First of all I would Use UITextView instead of UITextField, as it is build for long text.
If you just keep calling the method text of either UITextField or UITextView, the currently residing text will be replaced with your last text. To avoid that you have to append new text to the text that is already residing in the UITextField or UITextView.
NSString *appendThisText = #"subtitle";
self.myTextView.text=[NSString stringWithFormat:#"%#%#", self.myTextView.text, appendThisText];
As for the fonts. The font property applies to the entire UITextField and UITextView content.
What you could do in your case is to use UIWebView and render your text using HTML.
Another way:
[myTextView setText:[myTextView.text stringByAppendingString:#"my string"]];
Or with a \n line break:
[myTextView setText:[myTextView.text stringByAppendingString:#"\nMy string"]];

Retrieve UITextField Text for a specific UITextField Tag

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;

how to print a variable into a text box

hi all
I have a one variable. i want to print that varible into a text box. how to do this.
You can only display the string data in text box (UITextView),if you have other than string data it require a conversion to NSString then use the text properity of UITextView to set the display text.
The below code is for your reference.
NSString* myData = #"dISPLAY me in text box";
UITextView* myTextView = [[UITextView alloc] initWithFrame:myframe];
myTextView.text = myData;
write
NSString * str=#"Text";
myTextField.text=str;
myTextField is the name of my textField(you can write the name of your text Field).
yourTextField.text = yourVariable;
If you want to have it as a string value you can make use of stringValue
You create a UILabel object, add it as a subview of your view, and call setText: on it.
Unless you are asking how to do GUI programming in general.