Change Label Text - iphone

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

Related

Each button click adds text to current label objective-c

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.

XCode - Dynamically created labels, when i change the text it changes it for the last one only

So i have a bunch of dynamically loaded labels..
Each of them has the same name because there is no telling how many there will be..
I have another method (not the one that created the labels) changing the text for one of the labels, but when i run it only the last label that was created will change..
I need it to change the one that has a certain tag or something..
Help is much appreciated, this website is yet to let me down.
self.myLabel cannot be connected to multiple labels, so it will contain the reference of last created label, you will have to create new label every time, and you can't track them by class properties, you have to access label by their tag.
you can set tag for each label, below is sample code,
for(int i=0; i< numberOfLabels; i++)
{
UILabel *label = [[UILabel alloc] init];
label.tag = i; // do not use tag 0 here.. u can use i+1, or i+100.. something like this.
[self.view addSubview:label];
}
to access labels,
UILabel *label = (UILabel*)[self.view viewWithTag: labelTag];
Okay since you dont have any code to show i guess i have to speculate.
What i understood is that you are creating Dynamic UILabels in ur code and you want to access them. Since you have same name for all the UILabels you might me loosing the previous UILabel when every time you create a new UILabel. So in order to keep track of how many UILabel you created you must add them in an Array. Declare an NSMutableArray in your viewController.h file and make sure in the viewDidLoad u allocate it like
arrForLabels = [[NSMutableArray alloc]init];
Since it is an NSMutableArray you can add object to it.
So when u create a UILabel make sure you add the same UILabel in the Array as well
for Instance
[arrForLabels addObject:yourLabel];
you can try to NSLog your Array to see its content.
Now all youu got to do is to Create a weak link like that
UILabel *tempLabel = [arrForLabels objectAtIndex:1];
now tempLabel will be the UILabel to change text
tempLabel.text = #"My New Text";
It will work fine.
Feel free to ask for any issues in it.

How to make tableview cell "detailLabel" editable?

I am trying to implement a suggestion offered by already answered question, but I am stuck on getting it to work, so looking for some help.
Here is the post I am trying to implement: http://stackoverflow.com/a/3067579/589310
I am trying to use the solution offered by "ridale" on how to make a "detailLabel" editable as part of a TableView. I hope it will allow me to directly edit a cell and enter a number. It doesn't seem too common as a UI, but "SmartRecord" does it and I want to emulate it.
Here is the only line that gives me an error:
UITextField *tmpView = [self detailLabel:indexPath];
I get this error:
Instance method -detailLabel: not found (return type defaults to 'id')
I assume it is because my self is different than the original poster.
I added a TableView directly to my existing controller. It is not a TableViewController directly:
#interface EditViewController : UIViewController
{
IBOutlet UITableView *tableSettings;
}
I can fill the table and interact with it, so I know it works (at some level anyway).
I have tried changing self to my table control or the cell directly:
UITextField *tmpView = [tableSettings detailLabel:indexPath];
I can't find anything that responds to the "detailLabel" method.
I am also not sure if the proposed solution is complete or uses more code not shown.
This is the only error I get, so I am hopeful once I solve it, it will work ;-)
-(UITextField*)detailLabel:(NSIndexPath*)indexPath is not present in UITableView or any other classes provided by Apple.
The frame position is just for sample. Modify that such that it would come in place of your detailLabel.
You have to write your own method which returns a UITextField, something as following
-(UITextField*)detailLabel:(NSIndexPath*)indexPath
{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
textField.delegate = self;
//assuming myList is array of NSString
NSString* str = [myList objectAtIndex:indexPath.row];
textField.text = str;
return [textField autorelease];
}

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;

Access switch and textfield in tableview from another method

I'm having a tableview with a couple of cells with 4 uiswitches and one textfield inside. Simple question. I need a way to read all the positions of the switches and the string inside the textview by clicking a button. Tableview, textfield, switches and button is working fine. Just need to access the values of the switches and textfield from another method. By tagging the switches or something.
So how can I access the values from another method. Something like in tableview method:
...
UISwitch *switchView = [[option1 alloc] initWithFrame:CGRectZero];
switchView.tag = 3000;
cell.accessoryView = switchView;
...
And something like this in the button method:
...
BOOL status = [self.view viewWithTag:3000].on; //*Not a working method*
You are in the same class, nop ?
So maybe you could put your switch & co as class variable (declared in the header file). It would be easier ^^