블로그 이미지
윤영식
Full Stacker, Application Architecter, KnowHow Dispenser and Bike Rider

Publication

Category

Recent Post

2014. 11. 27. 17:37 Languages/Go

스위프트 심화과정을 진행해 보자. 기본 문법을 배웠다면 프로젝트를 생성하고 이제 iOS 앱을 만들어 본다. 물론 앱개발시 기본 언어는 Object-C가 아니라 Swift이다. 




프로토콜


  - 특정 임무나 기능의 일부에 적합하도록 하기 위한 목적으로 작성된 메소드, 속성 등 요구사항에 대한 설계

  - 인터페이스 : 속성 변수와 메소드 포함, 대부분이 인터페이스를 통해 다형성을 보장하는 방식으로 프로그래밍함 

  - 클래스에서 구현 

protocol <pName> {

    func <fName>() {

    }

}


// 예

protocol Sample {

    func execute(name: String) -> String

}


class SampleImpl : Sample {

    func execute(name: String) -> String {

        return "hi \(name)"

    }

}


var s = SampleImpl()

s.execute("Peter")


  - 여러 인터페이스 구현은 콤마(,)로 구분함 




익스텐션 (Extension)


  - 자바스크립트에서 prototype을 통한 객체 자체의 메소드 확장기능과 유사함 

  - 가급적 최상위 클래스 변경은 하지 말자

extension <eName> {

   <확장 대상>

}


// 예

protocol NamedProtocol {

}


extension Int : NamedProtocol {

    var description : String {

        return "current value is \(self)"

    }

    

    func desc() -> String {

        return "current value is \(self)"

    }

    

    func getFirstName(name: String) -> String {

        return "\(self) name \(name)"

    }

}


7.description

7.desc()

7.getFirstName("dowon")




앱 프레임워크


  - 스토리 보드

    + 안드로이드의 XML 환경 설정과 유사함. 대신 비쥬얼하게 인터렉티브 UI 디자인이 가능하다 

  - iOS 4계층 프레임워크 

    + Core OS -> Core Services -> Media -> Cocoa Touch

    + Cocoa Touch : 파운데이션 (Foundation), UIKit 등이 포함,  접두어 : NS - 파운데이션, UI - UIKit

    + Core Services : 코어 파운데이션

  - Cocoa Framework 

    + 맥북의 애플리케이션 개발

    + Cocoa Touch F/W은 iOS 앱 개발 

    + 80% 가량이 Cocoa F/W 과 Cocoa Touch F/W 이 겹친다. 

  - Cocoa Touch F/W = Foundation F/W (NS*) + UIKit F/W (UI*)




스플래시


  - 최초에 뜨는 스플래시 화면 제어하기 

  - AppDelegate.swift 파일이 앱의 LifeCycle을 관리한다?

  - LaunchScreen.xib를 추가할 수 있다. => 프로젝트 Deployment Info의 Main Interface에서 최초 페이지를 선택할 수 있다. 




네비케이션 뷰 컨트롤러 


  - 뷰를 스택으로 관리한다

  - 기본이 되는 Root View Controller가 있다

  - push 하면 스택에 쌓여 위에 노출되고, pop 하면 화면이 제거됨 : pushViewController

  - 상단 메뉴 : Editor -> Embed in -> Navigation Controller를 추가한다 

class NextView : UIViewController {

    

    func nextView(ctrlName: String, transition: UIModalTransitionStyle) {

        // 두번째 화면의 인스턴스를 만든다

        var uvc: UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier(ctrlName) as UIViewController

        

        // 화면 전환 스타일을 지정한다

        uvc.modalTransitionStyle = transition

        

        // 화면을 호출한다

        //self.presentViewController(uvc, animated: true, completion: nil)

        self.navigationController?.pushViewController(uvc, animated: true)

    }

    

    func dismissView() {

        //self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)

        self.navigationController?.popViewControllerAnimated(true)

    }

}




세그 (Segue)


  - 화면 연결 처리 전용 객체 

  - Source : 자신이 있는 곳

  - Destination : 목적지 

  - 연결 방법

    + 첫번째 화면(Source) 버튼을 Ctrl 누른 상태에서 두번째 화면(Destination)으로 드래그 앤 드롭을 하면 Segue 연결 팝업창이 나옴

    + 두번째 화면을 선택하고 오른쪽 상단의 마지막 아이콘 (->) 을 선택하고 Presenting Segues 에서 Present modally를 선택하고 첫번째 화면으로 드래그 앤 드롭해 연결

  - 값 전달하기 

    + 직접 전달 : 두번째 화면 컨트롤러로 캐스팅

    + segue를 연결해서 전달 : prepareForSegue 재정의 내역에 segue. destinationViewController를 두번째 화면 컨트롤러로 캐스팅

// 첫번째 화면 컨트롤러 

class FirstViewController : UIViewController {

    

    // source 화면에서 버튼을 추가하고 destination 화면으로 이동할 때 값을 전달하는 방법 

    @IBAction func nextPresentation(sender: AnyObject) {

        var uvc : SecondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as SecondViewController

        

        uvc.param = "Direct Parameter"

        

        self.presentViewController(uvc, animated: true, completion: nil)

    }

    

    // segue로 연결해서 값을 전달하는 경우 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        var uvc : SecondViewController = segue.destinationViewController as SecondViewController

        

        uvc.param = "Segue Parameter"

    }

}


// 두번째 화면 컨트롤러 

class SecondViewController : UIViewController {

    

    var param : String = ""

    

    override func viewDidLoad() {

        NSLog("Excute Second View Loaded value is %@", param)

    }

}


  - 세그를 커스터마이징할 수 있다. : 이동 시간, 이동 효과등을 설정할 수 있다

    + 스토리 보드에서 custom segue를 선택하고 CustomSegue를 선택

import UIKit


// 반드시 상속 

class CustomSegue : UIStoryboardSegue {

    // 반드시 구현 

    override func perform() {

        let src = self.sourceViewController as UIViewController

        let dest = self.destinationViewController as UIViewController

        

        UIView.transitionFromView(src.view, toView: dest.view, duration: 1.0, options: UIViewAnimationOptions.TransitionCurlDown, completion: nil)

    }

}




접두어 


  - NS : 파운데이션 

  - CF : Core 파운데이션 

  - IB : @IBOutlet 같은 어노테이션이 컨트롤러에 있으면 스토리보드에서 자동 인식하는 것이다. 




UIAlertView


  - iOS 메세지 전달은 델리게이트 패턴을 쓴다 

  - 이벤트 처리 담당자가 따로 있어서 발생시 현재 떠 있는 창에 알려주는 구조 

  - UIAlertViewDelegate 프로토콜(protocol)을 구현하면 Alert View Controller를 제어할 수 있다



참조 


  - 옵셔널 체인 : 코드에서 ? 사용하기

'Languages > Go' 카테고리의 다른 글

[Swift] 스위프트 배우기 - 1일차  (0) 2014.11.26
posted by 윤영식