본문 바로가기
ios 뽀개기/실전

커스텀 버튼 및 데이터 전달하기

by 인생여희 2018. 1. 2.
반응형

커스텀 버튼 및 데이터 전달하기



//

//  ViewController.swift

//  CustomButton2

//

//  Created by MacBookPro on 2017. 12. 31..

//  Copyright © 2017년 MacBookPro. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    var paramEmail: UITextField! //이메일 입력 필드

    var paramUpate: UISwitch! //스위치

    var paramInterval: UIStepper! // 스테퍼

    

    //스위치 값을 표현해줄 레이블

    var textUpdate: UILabel!

    //스테퍼 값을 표현해줄 레이블

    var textSteper: UILabel!

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

      

        //내비게이션 바 타이틀을 입력한다.

        self.navigationItem.title = "설정"

    

        //이메일 레이블 객체를 생성하고, 기본 문구를 설정한다.

        let lblEmail = UILabel()

        lblEmail.frame = CGRect(x:30 ,y:100, width:100, height:30)

        lblEmail.text = "이메일"

        //레이블의 폰트를 설정한다.

        lblEmail.font = UIFont.systemFont(ofSize: 14)

        

        /* 참고

        폰트의 크기와 볼드 설정을 동시에하기

        let font = UIFont.boldSystemFont(ofSize: 12)

        

        폰트종류까지 저장하기

        let font = UIFont(name:"chalkboard", size:14)

        

        let fonts = UIFont.familyNames

        for f in fonts {

            print("\(f)")

        }

         

         let myfont = UIFont.fontNames(forFamilyName: "Georgia")

         for f in myfont {

         print("\(f)")

         }

        */

    

        // 레이블을 루트뷰에 추가한다.

        self.view.addSubview(lblEmail)

        

        

        //레이블 객체를 생성하고, 기본 문구를 설정한다.

        let lblUpdate = UILabel()

        lblUpdate.frame = CGRect(x:30 ,y:150, width:100, height:30)

        lblUpdate.text = "자동갱신"

        //레이블의 폰트를 설정한다.

        lblUpdate.font = UIFont.systemFont(ofSize: 14)

        self.view.addSubview(lblUpdate)

        

        //레이블 객체를 생성하고, 기본 문구를 설정한다.

        let lblInterval = UILabel()

        lblInterval.frame = CGRect(x:30 ,y:200, width:100, height:30)

        lblInterval.text = "갱신주기"

        //레이블의 폰트를 설정한다.

        lblInterval.font = UIFont.systemFont(ofSize: 14)

        self.view.addSubview(lblInterval)

        

        

        

        

        

        //이메일 입력을 위한 텍스트 필드를 추가한다.

        self.paramEmail = UITextField()

        self.paramEmail.frame = CGRect(x:120, y:100, width:220, height:30)

        self.paramEmail.font = UIFont.systemFont(ofSize: 13)

        self.paramEmail.borderStyle = UITextBorderStyle.roundedRect //모서리 둥글게

        self.paramEmail.autocapitalizationType = .none  //대문자 자동 변환 기능을 해제하는 구문

        

        //self.paramEmail.textAlignment = NSTextAlignment.rigth // 오른쪽 정렬

        //self.paramEmail.adjustsFontSizeToFitWidth = true      //텍스트 너비에 따라 폰트 사이즈 자동 조절

        //self.paramEmail.placeholder = "이메일 예) abcnt@naver.com"

        self.view.addSubview(self.paramEmail)

        

        

        

        //스위치 객체를 위한 코드

        self.paramUpate = UISwitch()

        self.paramUpate.frame = CGRect(x:120, y:150, width:50, height:30)

        self.paramUpate.setOn(true, animated: true) //on 이 되어 있는 상태를 기본 값

        self.view.addSubview(paramUpate)

        

        

        //스테퍼 객체를 위한 코드

        self.paramInterval = UIStepper()

        self.paramInterval.frame = CGRect(x:120 ,y:200, width:50, height:30)

        self.paramInterval.minimumValue = 0 //스테퍼가 가질 수 있는 최소 값

        self.paramInterval.maximumValue = 100 // 스테퍼가 가질 수 있는 최대 값

        self.paramInterval.stepValue = 1    //하나씩 증가

        self.paramInterval.value = 0    //초기 값 0

        self.view.addSubview(self.paramInterval)

        

        

        //스위치 값을 표시할 레이블

        self.textUpdate = UILabel()

        self.textUpdate.frame = CGRect(x:250,y:150, width:100, height:30)

        self.textUpdate.text = "갱신함"

        //레이블의 폰트를 설정한다.

        self.textUpdate.font = UIFont.systemFont(ofSize: 12)

        self.textUpdate.textColor = UIColor.red //빨간색 글자로

        self.view.addSubview(textUpdate)

        

        

        //스테퍼 값을 표시할 레이블

        self.textSteper = UILabel()

        self.textSteper.frame = CGRect(x:250,y:200, width:100, height:30)

        self.textSteper.text = "0분마다"

        self.textSteper.textColor = UIColor.red

        self.textSteper.font = UIFont.systemFont(ofSize: 12)

         self.view.addSubview(textSteper)

        

        

        //스위치와 스테퍼 컨트롤의 value changed 이벤트를 각각 액션 메소드에 연결한다.

        self.paramUpate.addTarget(self, action: #selector(presentUpdateValue(_:)), for: .valueChanged)

        self.paramInterval.addTarget(self, action: #selector(presentIntervalValue(_:)), for: .valueChanged)

        

        let SubmitBtn = UIBarButtonItem(barButtonSystemItem: .compose,

                                        target: self,

                                        action: #selector(submit(_:)))

        self.navigationItem.rightBarButtonItem = SubmitBtn

    }


    

    //스위치와 상호 반응할 메서드

    @objc func presentUpdateValue(_ sender: UISwitch){

        self.textUpdate.text = (sender.isOn  == true ? "갱신함" : "갱신하지 않음")

    }

    

    //스테퍼와 상호 반응할 메서드

    @objc func presentIntervalValue(_ sender: UIStepper){

        self.textSteper.text = ("\(Int(sender.value)) 분마다")

    }


    

    //전송버튼과 상호반응할 메서드 : 값을 상세화면으로 넘긴다.

    @objc func submit(_ sender: Any){

        let rvc = ReadViewController()

        rvc.pEmail = self.paramEmail.text

        rvc.pUpdate = self.paramUpate.isOn

        rvc.pInterval = self.paramInterval.value

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

    }

}




//

//  ReadViewController.swift

//  CustomButton2

//

//  Created by MacBookPro on 2017. 12. 31..

//  Copyright © 2017년 MacBookPro. All rights reserved.

//


import UIKit


class ReadViewController: UIViewController {

    

    var pEmail: String?

    var pUpdate: Bool?

    var pInterval: Double?

    

    

    override func viewDidLoad() {

        super.viewDidLoad()


        self.view.backgroundColor = UIColor.green

        

        //레이블 객체를 설정한다.

        let email = UILabel()

        let update = UILabel()

        let interval = UILabel()

    

        //위치를 지정한다.

        email.frame = CGRect(x:50, y:100, width:300, height:30)

        update.frame = CGRect(x:50, y:150, width:300, height:30)

        interval.frame = CGRect(x:50, y:200, width:300, height:30)

    

        //전달받은 값을 레이블에 표시한다.

        email.text = "전달받은 이메일\(self.pEmail!)"

        update.text = "\(self.pUpdate == true ? "업데이트 함" : "업데이트 안 함")"

        interval.text = "\(Int(self.pInterval!)) 분마다"

        

        //레이블을 루트 뷰에 추가한다.

        self.view.addSubview(email)

        self.view.addSubview(update)

        self.view.addSubview(interval)

        

    }

    

    





}





반응형

'ios 뽀개기 > 실전' 카테고리의 다른 글

메모장 - 커스텀(x)  (0) 2018.01.03
커스텀 내비게이션 바  (0) 2018.01.03
탭바 커스텀하기  (0) 2018.01.02
커스터마이징 버튼  (0) 2017.12.27
버튼 커스터마이징  (0) 2017.12.27

댓글