Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Tom Coomer
1,331 PointsAdding the value of a text field to a label with a string.
I would like to add the value in a text field to a label that already has a string inside it. For example if the
label = Hello textlabel = treehouse
once the enter button is clicked label = Hello treehouse
Thanks
1 Answer
Michael Jones
Python Development Techdegree Graduate 38,554 PointsHey Tom,
I've given it a go and this is what I have so far.
-
(IBAction)myButton:(id)sender { NSString *textFieldString = _myTextField.text;
self.myLabel.text = [_myLabel.text stringByAppendingString:textFieldString]; }
This button method will create a new NSString called 'textFieldString' from the TextField.
It will then add the text to the Label created.
Hope this helps in some way.
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsOkay. Ill try it now. Thanks.
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsIt worked! Thanks!
Michael Jones
Python Development Techdegree Graduate 38,554 PointsMichael Jones
Python Development Techdegree Graduate 38,554 PointsYou're welcome buddy. Keep up the good work!!
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsSorry to bother you again. Do you know how to keep a button in its selected state after it has been pressed? As if it is a light switch button of some kind in the real world.
Michael Jones
Python Development Techdegree Graduate 38,554 PointsMichael Jones
Python Development Techdegree Graduate 38,554 PointsI'm not too sure to be honest, have you thought about using a UISwitch instead of a UIButton?
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsOkay. I'll look at that instead. Thanks again.
Michael Jones
Python Development Techdegree Graduate 38,554 PointsMichael Jones
Python Development Techdegree Graduate 38,554 PointsNo worries, I'll carry on working on it though. Determined to find a way.
Derek Medlin
6,151 PointsDerek Medlin
6,151 PointsIn your header file add:
In your implementation file add:
Then connect the button with "TouchUpInside". Don't forget to set the selected image/color, etc to whatever you want in the Attributes Inspector. Hope this helps.
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsThanks a lot!
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsHow do I set the image for the selected state?
Derek Medlin
6,151 PointsDerek Medlin
6,151 PointsOr you could just set these in the Attributes Inspector in Xcode. Go to "State Config" on the Attributes Inspector tab. Make sure "Default" is selected. Then go to either image or background image and change it for your "off image" and change the title or whatever else you would like to change. Then go to "State Config" again and change the drop down to "Selected" and then make any changes you want to for that state ( image/title etc). Either way you choose to do this is fine just make sure button type is "Custom".
Derek Medlin
6,151 PointsDerek Medlin
6,151 PointsSorry, I don't know markdown, maybe this time it will be more readable on the code....
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsBrilliant! Thanks!