화면 전환 segue 


//

//  MainViewController.swift

//  CompanionAnimals

//

//  Created by stayfoolish on 02/10/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class MainViewController: UIViewController {


    private enum ButtonTag: Int {

        case dog = 101

        case cat, rabbit, hedgehog

    }

    

    override func viewDidLoad() {

        super.viewDidLoad()


        // Do any additional setup after loading the view.

    }


    private func animalInfo(for tag: ButtonTag) -> AnimalInfo? {

        let assetName: String

        

        switch tag{

        case ButtonTag.dog:

            assetName = "Dog"

        case ButtonTag.cat:

            assetName = "Cat"

        case ButtonTag.rabbit:

            assetName = "Rabbit"

        case ButtonTag.hedgehog:

            assetName = "Hedgehog"

        }

        

        return AnimalInfo.decode(from: assetName)

    }

    


    

    // MARK: - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destinationViewController.

        // Pass the selected object to the new view controller.

        

        guard let button: UIButton = sender as? UIButton else { return }

        

        guard let nextViewController: DescriptionViewController = segue.destination as? DescriptionViewController else { return }

        

        guard let tag: ButtonTag = ButtonTag(rawValue: button.tag) else {


            print("버튼의 태그를 enum으로 변경불가")

            return

        }

        

        guard let info: AnimalInfo = self.animalInfo(for: tag) else { return }

        

        nextViewController.animalInfo = info

    }

    


}


//

//  DescriptionViewController.swift

//  CompanionAnimals

//

//  Created by stayfoolish on 02/10/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class DescriptionViewController: UIViewController {


    var animalInfo: AnimalInfo!

    

    @IBOutlet var animalImageView: UIImageView!

    @IBOutlet var nameLabel: UILabel!

    @IBOutlet var descriptionTextView: UITextView!

    

    

    

    override func viewDidLoad() {

        super.viewDidLoad()


        // Do any additional setup after loading the view.

        self.navigationItem.title = self.animalInfo.name

        self.animalImageView.image = UIImage(named: self.animalInfo.imageName)

        self.nameLabel.text = self.animalInfo.name

        self.descriptionTextView.text = self.animalInfo.animalDescription

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    


    /*

    // MARK: - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destinationViewController.

        // Pass the selected object to the new view controller.

    }

    */


}


//

//  MakerViewController.swift

//  CompanionAnimals

//

//  Created by stayfoolish on 02/10/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class MakerViewController: UIViewController {


    @IBOutlet var descriptionTextView: UITextView!

    

    override func viewDidLoad() {

        super.viewDidLoad()


        self.descriptionTextView.text = """

        안녕하세요 반갑습니다.

        애플리케이션을 이용해주셔서 고맙습니다.

        https://developer.apple.com/kr/swift/


        """

        // Do any additional setup after loading the view.

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    @IBAction func touchUpInsideDismissButton(_ sender: UIButton){

        self.presentingViewController?.dismiss(animated: true, completion: nil)

    }


    /*

    // MARK: - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destinationViewController.

        // Pass the selected object to the new view controller.

    }

    */


}



//

//  AnimalInfo.swift

//  CompanionAnimals

//

//  Created by stayfoolish on 06/10/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


struct AnimalInfo: Codable {

    let name: String

    let animalDescription: String

    var imageName: String

    

    static func decode(from assetName: String) -> AnimalInfo? {

        guard let asset: NSDataAsset = NSDataAsset(name: assetName) else {

            print( "에셋 로드 실패")

            return nil

        }

        

        do {

            let decoder: PropertyListDecoder = PropertyListDecoder()

            return try decoder.decode(AnimalInfo.self, from: asset.data)

        } catch {

            print("데이터 디코딩 실패")

            print(error.localizedDescription)

            return nil

        }

    }

}
























+ Recent posts