프로그래밍 노트

[Xcode] 간단한 버튼 만들기 본문

Xcode

[Xcode] 간단한 버튼 만들기

떡잎 2013. 9. 11. 01:42

버튼을 눌렀을 때와 뗐을 때 라벨의 내용을 변경

//

//  ViewController.m

//  ButtonTest

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController

{

    UILabel *lb;

    UIButton *bt;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    lb = [[UILabel alloc] init];

    lb.text = @"버튼 테스트";

    [lb sizeToFit];

    

    bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [bt setTitle:@"테스트" forState:UIControlStateNormal];

    bt.frame = CGRectMake(0, 0, 100, 100);

    bt.center = self.view.center;

    

    [bt addTarget:self action:@selector(bt_down:) 

        forControlEvents:UIControlEventTouchDown];

    [bt addTarget:self action:@selector(bt_up:) 

        forControlEvents:UIControlEventTouchUpInside];

    

    self.view.backgroundColor = [UIColor whiteColor];

    

    [self.view addSubview:lb];

    [self.view addSubview:bt];

    

}

- (IBAction)bt_down:(UIButton *)sender

{

    lb.text = @"눌렸다";

    [lb sizeToFit];

}

- (IBAction)bt_up:(UIButton *)sender

{

    lb.text = @"뗐다";

    [lb sizeToFit];

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}


@end

Comments