본문 바로가기

Objective-C/UIKit

[Objective-C] SubView 띄우기

반응형

01. 'http://ihayatesw.tistory.com/15' 에서 04번까지의 수행을 행한다.

 

02.  Xcode 상단의 File → New→ File을 선택한다.

 

03. 'UIViewController'를 선택하고 'Next'를 눌러 다음으로 넘어간다.

 

04. 'Class'의 TextBox에 이름을 정해서 입력한다.(여기서는 RootViewController라고 정의하였다.)

 

05. 'Create'를 눌러서 마무리 하면 'RootViewController'가 생성된다.

 

06. 'AppDelegate.h' 파일을 열고 아래와 같이 수정한다.

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate> {
   
    UIViewController *viewController;
    UIWindow *window;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UIViewController *viewController;

@end



07. 'AppDelegate.m' 파일을 열고 아래와 같이 수정한다.

#import "AppDelegate.h"
#import "RootViewController.h"

@implementation AppDelegate

@synthesize window = window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    // window를 직접 작성(인터페이스 빌더를 사용하지 않기 때문에)
    CGRect bounds = [[UIScreen mainScreen] bounds];
    window = [[UIWindow alloc] initWithFrame:bounds];
   
    // 애플리케이션 로딩이 끝난 후 수행되는 부분
    viewController = [[RootViewController alloc] init];
   
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
   
    return YES;
}

@end


08. 'RootViewController.m' 파일을 열고 아래와 같이 수정한다.

#import "RootViewController.h"

@implementation RootViewController

- (void)viewDidLoad {
   
    [super viewDidLoad];
   
    UILabel *label = [[UILabel alloc] init];
    [label setFrame:CGRectMake(110.0, 100.0, 200.0, 30.0)];
    [label setText:@"Hello Xcode"];
    [self.view addSubview:label];
   
    [self.view setBackgroundColor:[UIColor whiteColor]];
}


@end


09. 이제 'Run'을 눌러 에뮬레이터를 실행하면 'RootViewController'에 작성한 'Hello Xcode'가 화면에 띄어지는 것을 볼 수 있다.



반응형