본문 바로가기

iOS Programming

Window와 View

iPhone App는 Window와 View를 통하여 컨텐츠를 표시하고 사용자의 입력을 받습니다.
오늘은 이 Window와 View의 개념에 대해 공부하였습니다.

[ Window ]
1. Window는 시각적인 요소를 제공하지 않으며 이벤트의 연결 통로로만 사용됩니다.
    (시각적인 요소는  View를 통해서만 구현합니다.)
2. 하나의 Application은 하나의 Window만 가져야 합니다.
    View를 변경하는 방식으로 다중 컨텐츠를 제공해야 합니다.
3. Window는 상속될 수 없으며 override 함수 또한 구현될 수 없습니다.
    delegate를 이용해야 합니다.


[ View ]
1. View 는 실제 컨텐츠들을 표시하고 사용자의 입력을 받는 사각 공간입니다.
2. View 는 SubView를 가질 수 있습니다. 그  SubView는 다시 SubView를 가질 수 있습니다.
    (웹페이지의 iframe 태그 개념과 비슷합니다.)
3. Window를 포함한 모든 컨트롤들은 이 View에서 상속되었습니다.


아래의 그림은 iPod의 시계를 통하여 본 윈도우와 뷰 구성입니다.
여기에서 하나의 윈도우에 네비게이션바, 탭바,뷰가 들어가 있음을 알 수 있습니다.
물론 NavigationBar와 TabBar 또한 View에서 상속된 클래스입니다.

Layered views in the Clock application


아래의 샘플 소스는 Interface Builder를 이용하지 않고 Window를 하나 만들고 붉은색 바탕의 뷰어 파란색 바탕의 뷰를 만드는 예제입니다.

 - (void)applicationDidFinishLaunching:(UIApplication *)application {
    // Create the window object and assign it to the
    // window instance variable of the application delegate.
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.backgroundColor = [UIColor whiteColor];
 
    // Create a simple red square
    CGRect redFrame = CGRectMake(10, 10, 100, 100);
    UIView *redView = [[UIView alloc] initWithFrame:redFrame];
    redView.backgroundColor = [UIColor redColor];
 
    // Create a simple blue square
    CGRect blueFrame = CGRectMake(10, 150, 100, 100);
    UIView *blueView = [[UIView alloc] initWithFrame:blueFrame];
    blueView.backgroundColor = [UIColor blueColor];
 
    // Add the square views to the window
    [window addSubview:redView];
    [window addSubview:blueView];
 
    // Once added to the window, release the views to avoid the
    // extra retain count on each of them.
    [redView release];
    [blueView release];
 
    // Show the window.
    [window makeKeyAndVisible];
}

 

'iOS Programming' 카테고리의 다른 글

Xcode Console Debugging  (0) 2009.05.27
ObjectiveC 초간단 훑어보기  (0) 2009.04.21
Hello iPod 2 (레이블과 버튼)  (3) 2009.04.20
Hello iPod  (0) 2009.04.20
iPhone / iPod 개발 관련 사이트  (0) 2009.04.20