swift submitvalue present push 스위프트 데이터 전달 




//

//  ViewController.swift

//  SubmitValue

//

//  Created by stayfoolish on 23/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    // 이메일 주소를 입력받는 텍스트필드

    @IBOutlet var email: UITextField!

    

    // 자동 갱신 여부를 설정하는 스위치

    @IBOutlet var isUpdate: UISwitch!

    

    // 갱신 주기를 설정하는 스테퍼

    @IBOutlet var interval: UIStepper!

    

    // 자동갱신 여부를 표시하는 레이블

    @IBOutlet var isUpdateText: UILabel!

    

    // 갱신주기를 텍스트로 표시하는 레이블

    @IBOutlet var intervalText: UILabel!


    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

    }


    // 자동 갱신 여부가 바뀔 때마다 호출되는 메소드

    @IBAction func onSwitch(_ sender: UISwitch) {

        if (sender.isOn == true) {

            self.isUpdateText.text = "갱신함"

        } else {

            self.isUpdateText.text = "갱신하지 않음"

        }

        


    }

    

    // 갱신주기가 바뀔 때마다 호출되는 메소드

    @IBAction func onStepper(_ sender: UIStepper) {

        let value = Int(sender.value)

        self.intervalText.text = "\(value)분 마다"

    }

    

    

    // 프레젠테이션 방식으로 화면 전환하면서 값을 전달하기

    @IBAction func onSubmit(_ sender: Any) {

        //VC2의 인스턴스 생성

        guard let rvc = self.storyboard?.instantiateViewController(withIdentifier: "RVC") as? ResultViewController else {

            return

        }

        

        // 값을 전달

        rvc.paramEmail = self.email.text! // 이메일

        rvc.paramUpdate = self.isUpdate.isOn // 자동갱신 여부

        rvc.paramInterval = self.interval.value // 갱신주기

        

        // 화면이동

        self.present(rvc, animated: true)

        

    }

    

    

    // 내비게이션 컨트롤러를 통해 화면 전환하면서 값을 전달하기

    @IBAction func onSubmitBarButton(_ sender: UIBarButtonItem) {

        //VC2의 인스턴스 생성

        guard let rvc = self.storyboard?.instantiateViewController(withIdentifier: "RVC") as? ResultViewController else {

            return

        }

        

        // 값을 전달

        rvc.paramEmail = self.email.text! // 이메일

        rvc.paramUpdate = self.isUpdate.isOn // 자동갱신 여부

        rvc.paramInterval = self.interval.value // 갱신주기

        

        // 화면이동

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

        

    }

    

    


}





//

//  ResultViewController.swift

//  SubmitValue

//

//  Created by stayfoolish on 23/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class ResultViewController: UIViewController {

    

    // 화면에 값을 표시하는데 사용될 레이블

    @IBOutlet var resultEmail: UILabel! //이메일

    @IBOutlet var resultUpdate: UILabel! //자동갱신 여부

    @IBOutlet var resultInterval: UILabel! //갱신주기


    // email 값을 받을 변수

    var paramEmail: String = ""

    

    // update 값을 받을 변수

    var paramUpdate: Bool = false

    

    // Interval 값을 받을 변수

    var paramInterval: Double = 0

    


    

    override func viewDidLoad() {

        self.resultEmail.text = paramEmail

        // 3항 연산자 A? B: C A가 참이면 B를 , 거짓이면 C를 반환합니다.

        self.resultUpdate.text = (self.paramUpdate == true ? "자동갱신" : "자동갱신안함" )

        self.resultInterval.text = "\(Int(paramInterval))분 마다 갱신"

    }

    

    @IBAction func onBack(_ sender: UIButton) {

        self.presentingViewController?.dismiss(animated: true)

    }

    

}









swift alert actionsheet 스위프트 얼럿 액션시트 




//

//  ViewController.swift

//  SimpleAlert

//

//  Created by stayfoolish on 22/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

    }


    @IBAction func alertButton(_ sender: UIButton) {

        

        let alert: UIAlertController

        alert = UIAlertController(title: "얼럿입니다", message: "여기에 메세지를 남길 수 있어요 사용자에게 왜 이 얼럿을 보여줬는지 설명합니다", preferredStyle: UIAlertController.Style.alert)

        

     

        

        var cancelAction: UIAlertAction

        cancelAction = UIAlertAction(title: "취소", style: UIAlertAction.Style.cancel, handler: {( action: UIAlertAction) in

            print("취소 액션 선택함")

            

        })

        

        var defaultAction: UIAlertAction

        defaultAction = UIAlertAction(title: "확인", style: UIAlertAction.Style.default, handler: {( action: UIAlertAction) in

            print("확인 액션 선택함")

            

        })

        

        var destructiveAction: UIAlertAction

        destructiveAction = UIAlertAction(title: "destructive", style: UIAlertAction.Style.destructive, handler: {( action: UIAlertAction) in

            print("destructive 액션 선택함")

            

        })

        

        alert.addAction(cancelAction)

        alert.addAction(defaultAction)

        alert.addAction(destructiveAction)

        

        self.present(alert, animated: true ) {

            print("얼럿 보여짐")

        }

    }

    

    

    @IBAction func actionSheetButton(_ sender: UIButton) {

        let alert: UIAlertController

        alert = UIAlertController(title: "얼럿입니다", message: "여기에 메세지 남길 수 있어요", preferredStyle: UIAlertController.Style.actionSheet)

        

        var cancelAction: UIAlertAction

        cancelAction = UIAlertAction(title: "취소", style: UIAlertAction.Style.cancel, handler: { (action: UIAlertAction) in

            print("취소 액션시트 선택함")

        })

        

        var defaultAction: UIAlertAction

        defaultAction = UIAlertAction(title: "확인", style: UIAlertAction.Style.default, handler: { (action: UIAlertAction) in

            print("확인 액션시트 선택함")

        })

        

        var destructiveAction: UIAlertAction

        destructiveAction = UIAlertAction(title: "destructive", style: UIAlertAction.Style.destructive, handler: { (action: UIAlertAction) in

            print("destructive 액션시트 선택함")

        })

        

        alert.addAction(cancelAction)

        alert.addAction(defaultAction)

        alert.addAction(destructiveAction)

        

        

        self.present(alert,animated: true){

            print("얼럿 보여짐")

        }

    }

    

}















swift tableview JSONSerialization 스위프트 테이블뷰 제이슨



//

//  ViewController.swift

//  WeatherTable

//

//  Created by stayfoolish on 21/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class ViewController: UIViewController,UITableViewDataSource  {

    var datalist =  NSDictionary()


    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        let baseURL = URL(string: "https://raw.githubusercontent.com/ChoiJinYoung/iphonewithswift2/master/weather.json")

        

        do {

            self.datalist  = try JSONSerialization.jsonObject(with: Data(contentsOf: baseURL!) , options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary

        } catch  {

            print("Error loading Data")

        }

        

        print(self.datalist)

        

        let className = "\(type(of:(((datalist["weatherinfo"] as! NSDictionary)["local"]) as! NSArray).count))"

        print("className : \(className)")

        

   

    }

    


    

    


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return ((datalist["weatherinfo"] as! NSDictionary)["local"] as! NSArray).count

    }

    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! WeatherCell


        

        //print("indexPath row : \(indexPath.row)")

        let dicTemp = ((datalist["weatherinfo"] as! NSDictionary)["local"] as! NSArray)[indexPath.row] as! NSDictionary

        

        print("dicTemp : \(dicTemp)")

        

        cell.countryLabel.text = dicTemp["country"] as? String

        

        let weatherStr = dicTemp["weather"] as? String

        

        cell.weatherLabel.text = weatherStr

        cell.temperatureLabel.text = dicTemp["temperature"] as? String

        

        if weatherStr == "맑음" {

            cell.imgView.image = UIImage(named: "sunny.png")

        }else if weatherStr == "비" {

            cell.imgView.image = UIImage(named: "rainy.png")

        }else if weatherStr == "흐림" {

            cell.imgView.image = UIImage(named: "cloudy.png")

        }else if weatherStr == "눈" {

            cell.imgView.image = UIImage(named: "snow.png")

        }else{

            cell.imgView.image = UIImage(named: "blizzard.png")

        }

        

        return cell

    }

    

    

}




//

//  WeatherCell.swift

//  WeatherTable

//

//  Created by stayfoolish on 22/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class WeatherCell: UITableViewCell {


    @IBOutlet var countryLabel: UILabel!

    @IBOutlet var weatherLabel: UILabel!

    @IBOutlet var temperatureLabel: UILabel!

    @IBOutlet var imgView: UIImageView!

    

    override func awakeFromNib() {

        super.awakeFromNib()

        // Initialization code

    }


    override func setSelected(_ selected: Bool, animated: Bool) {

        super.setSelected(selected, animated: animated)


        // Configure the view for the selected state

    }


}



https://raw.githubusercontent.com/ChoiJinYoung/iphonewithswift2/master/weather.json


{
  "weatherinfo": {
    "local": [
      {
        "country": "한국",
        "weather": "비",
        "temperature": "20"
      },
      {
        "country": "일본",
        "weather": "맑음",
        "temperature": "19"
      },
      {
        "country": "중국",
        "weather": "눈",
        "temperature": "14"
      },
      {
        "country": "스페인",
        "weather": "우박",
        "temperature": "13"
      },
      {
        "country": "미국",
        "weather": "흐림",
        "temperature": "2"
      },
      {
        "country": "영국",
        "weather": "비",
        "temperature": "10"
      },
      {
        "country": "프랑스",
        "weather": "흐림",
        "temperature": "15"
      },
      {
        "country": "브라질",
        "weather": "흐림",
        "temperature": "35"
      },
      {
        "country": "스위스",
        "weather": "맑음",
        "temperature": "13"
      },
      {
        "country": "덴마크",
        "weather": "비",
        "temperature": "2"
      },
      {
        "country": "스웨덴",
        "weather": "눈",
        "temperature": "0"
      },
      {
        "country": "네덜란드",
        "weather": "비",
        "temperature": "12"
      },
      {
        "country": "크로아티아",
        "weather": "맑음",
        "temperature": "30"
      },
      {
        "country": "필리핀",
        "weather": "맑음",
        "temperature": "28"
      },
      {
        "country": "독일",
        "weather": "눈",
        "temperature": "3"
      },
      {
        "country": "헝가리",
        "weather": "비",
        "temperature": "13"
      },
      {
        "country": "벨기에",
        "weather": "흐림",
        "temperature": "8"
      },
      {
        "country": "핀란드",
        "weather": "우박",
        "temperature": "15"
      },
      {
        "country": "이탈리아",
        "weather": "맑음",
        "temperature": "23"
      }
    ]
  }
}






+ Recent posts