Changing label text – Objective C

Posted on Updated on

Just for fun (using the tutorial http://codewithchris.com/xcode-tutorial-user-interaction/ ):
Headerfile:

//
//  ViewController.h
//  HelloWorld
//
//  Created by Lo Benjamin on 2015/04/09.
//  Copyright (c) 2015年 Lo Benjamin. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *helloLabel;
@property (strong, nonatomic) IBOutlet UITextField *customTextField;
@property (strong, nonatomic) IBOutlet UIButton *clickyButton;


@end

Implementation file:

//
//  ViewController.m
//  HelloWorld
//
//  Created by Lo Benjamin on 2015/04/09.
//  Copyright (c) 2015年 Lo Benjamin. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
//    label.text = @"Hello again!";
//    [self.view addSubview:label];

    self.helloLabel.text = @"default text";
    [self.clickyButton setTitle:@"clicky" forState:(UIControlStateNormal)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)handleButtonClick:(id)sender {
    self.helloLabel.text = self.customTextField.text;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.customTextField resignFirstResponder];
}

@end

1) Hold control down (on a Mac) then hold down and drag a text/label/button from Main.storyboard into the header file

2) In our viewDidLoad method, add the lines below:

self.helloLabel.text = @"default text";
[self.clickyButton setTitle:@"clicky" forState:(UIControlStateNormal)];

This will set our label with the text as “default text” during initialization. We also set our button to have the name “clicky”.

3) Same with our label, we hold control while drag and drop the button from Main.storyboard but this time into our implementation file before “@end” then name this method handleButtonClick. xcode will automatically generate a method for us to implement. Next, we implement the method so that our helloLabel text(the default value) is changed to whatever is entered in our custom text field:

- (IBAction)handleButtonClick:(id)sender {
    self.helloLabel.text = self.customTextField.text;
}

By the way this method:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.customTextField resignFirstResponder];
}

is to handle button clicks.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s