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

iOS

Adding 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

Hey 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.

Okay. Ill try it now. Thanks.

It worked! Thanks!

You're welcome buddy. Keep up the good work!!

Sorry 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.

I'm not too sure to be honest, have you thought about using a UISwitch instead of a UIButton?

Okay. I'll look at that instead. Thanks again.

No worries, I'll carry on working on it though. Determined to find a way.

In your header file add:

  • (IBAction)SelectButton:(UIButton *)sender;

In your implementation file add:

  • (IBAction)SelectButton:(UIButton *)sender { if (sender.selected){ sender.selected = NO; NSLog(@"Button is not selected"); } else{ sender.selected = YES; NSLog(@"Button IS selected"); } }

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.

Thanks a lot!

How do I set the image for the selected state?

  • (IBAction)SelectButton:(UIButton *)sender { if (sender.selected){ // deselect button since its selected sender.selected = NO; // set background image to the off image (background image over image if you want to see the button title / text) [sender setBackgroundImage:[UIImage imageNamed:@"OffImage.png"] forState:UIControlStateNormal]; // set the button title [sender setTitle:@"OFF" forState:UIControlStateNormal]; NSLog(@"Button is not selected"); } else{ sender.selected = YES; [sender setBackgroundImage:[UIImage imageNamed:@"OnImage.png"] forState:UIControlStateSelected]; [sender setTitle:@"ON" forState:UIControlStateNormal]; NSLog(@"Button IS selected"); } }

Or 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".

Sorry, I don't know markdown, maybe this time it will be more readable on the code....

- (IBAction)SelectButton:(UIButton *)sender {
    if (sender.selected){ 
        // deselect button since its selected
        sender.selected = NO;
        // set background image to the off image (background image over image if you want to see the button title / text)
        [sender setBackgroundImage:[UIImage imageNamed:@"OffImage.png"] forState:UIControlStateNormal];
        // set the button title
        [sender setTitle:@"OFF" forState:UIControlStateNormal];
        NSLog(@"Button is not selected");
    }
    else{
        sender.selected = YES;
        [sender setBackgroundImage:[UIImage imageNamed:@"OnImage.png"] forState:UIControlStateSelected];
        [sender setTitle:@"ON" forState:UIControlStateNormal];
        NSLog(@"Button IS selected");
        }
    }

Brilliant! Thanks!