- Xcode 를 이용하여 아이폰에 버튼을 넣고 Action을 주는 간단한 예제이다.
- 화면 디자인
오른쪽 하단에 Label 하나와 Round Rect Button 두개를 화면에 배치한다.
control 키를 누른후 ViewController.h 에 드레그엔 드롭 한다.
그 아래에 - (IBAction)gressBtn:(id)sender;(코드를 버튼 두개와 연결시켜주어야 된다!! 필수, 코드와 버튼을 드레그엔 드롭으로 간단하게 연결된다.) 코드를 삽입한다.
다음으로 ViewController.m 으로 이동한다. 아래를 작성..
-ViewController.m code
//
// ViewController.m
// BuutonExample01
//
// Created by App on 12. 7. 11..
// Copyright (c) 2012년 __MyCompanyName__. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize right;
@synthesize left;
@synthesize myLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setMyLabel:nil];
[self setRight:nil];
[self setLeft:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (IBAction)gressBtn:(id)sender { //버튼에 엑션이 이루어 졌을때 처리코드 작성.
if(sender == right)
[myLabel setText:@"라이트"];
else
[myLabel setText:@"래프트"];
}
@end
-실행화면
'C/Objective C > ios' 카테고리의 다른 글
아이폰 Switch 만들기 예제 따라하기. (0) | 2012.07.15 |
---|---|
아이폰 Text View 만들기 예제 따라하기 (0) | 2012.07.15 |
아이폰 Text Field 만들기 예제 따라하기 (0) | 2012.07.15 |
Xcode 4 Keyboard Shortcuts(Xcode 4 키보드 단축키) (0) | 2012.07.11 |
iOSApplicationLifeCycle (0) | 2012.07.11 |