swift submitvalue back appdelegate 스위프트 이전 화면 앱딜리게이트 데이터 전달




//

//  AppDelegate.swift

//  SubmitValueBack

//

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

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {


    var window: UIWindow?

    

    // 값을 저장할 변수를 정의

    

    var paramEmail : String? // 이메일 값을 전달받을 변수

    var paramUpdate: Bool? // 자동 갱신 여부를 전달받을 변수

    var paramInterval: Double? // 갱신주기 값을 전달받을 변수



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

        return true

    }


    func applicationWillResignActive(_ application: UIApplication) {

        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

    }


    func applicationDidEnterBackground(_ application: UIApplication) {

        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    }


    func applicationWillEnterForeground(_ application: UIApplication) {

        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

    }


    func applicationDidBecomeActive(_ application: UIApplication) {

        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    }


    func applicationWillTerminate(_ application: UIApplication) {

        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    }



}



//

//  ViewController.swift

//  SubmitValueBack

//

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

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class ViewController: UIViewController {

    

    // 값을 화면에 표시하기 위한 아울렛 변수들

    @IBOutlet var resultEmail: UILabel!

    @IBOutlet var resultUpdate: UILabel!

    @IBOutlet var resultInterval: UILabel!

    

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

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

    }


    // 화면에 표시될 때마다 실행되는 메소드

    override func viewWillAppear(_ animated: Bool) {

        // Appdelegate 객체의 인스턴스를 가져온다.

        let ad = UIApplication.shared.delegate as? AppDelegate

        

        if let email = ad?.paramEmail {

            // 이메일 표시

            resultEmail.text = email

        }

        if let update = ad?.paramUpdate {

            // 자동 갱신 여부 표시

            resultUpdate.text = update==true ? "자동갱신":"자동갱신안함"

        }

        if let interval = ad?.paramInterval {

            // 갱신 주기 표시

            resultInterval.text = "\(Int(interval))분마다"

        }

    }


}



//

//  FormViewController.swift

//  SubmitValueBack

//

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

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class FormViewController: 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)분 마다"

    }

    

    

    // Submit 버튼을 클릭했을 때 호출되는 메소드

    @IBAction func onSubmit(_ sender: Any){

        

        // AppDelegate 객체의 인스턴스를 가져온다.

        let ad = UIApplication.shared.delegate as? AppDelegate

        

        ad?.paramEmail = self.email.text

        ad?.paramUpdate = self.isUpdate.isOn

        ad?.paramInterval = self.interval.value

        

        

        // 이전 화면으로 복귀한다.

        self.presentingViewController?.dismiss(animated: true)

    }

    


    /*

    // 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.destination.

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

    }

    */


}








swift submitvalue back 스위프트 이전 화면으로 데이터 전달



//

//  ViewController.swift

//  SubmitValueBack

//

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

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class ViewController: UIViewController {

    

    // 값을 화면에 표시하기 위한 아울렛 변수들

    @IBOutlet var resultEmail: UILabel!

    @IBOutlet var resultUpdate: UILabel!

    @IBOutlet var resultInterval: UILabel!

    

    

    

    // 값을 직접 전달받을 프로퍼티들

    var paramEmail: String? // 이메일 값을 전달받을 속성

    var paramUpdate: Bool? // 자동 갱신 여부를 전달받을 속성

    var paramInterval: Double? // 갱신 주기 값을 전달받을 속성

    

    override func viewDidLoad() {

        super.viewDidLoad()

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

    }


    // 화면에 표시될 때마다 실행되는 메소드

    override func viewWillAppear(_ animated: Bool) {

        if let email = paramEmail {

            resultEmail.text = email

        }

        if let update = paramUpdate {

            resultUpdate.text = update==true ? "자동갱신":"자동갱신안함"

        }

        if let interval = paramInterval {

            resultInterval.text = "\(Int(interval))분마다"

        }

    }


}



//

//  FormViewController.swift

//  SubmitValueBack

//

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

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class FormViewController: 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)분 마다"

    }

    

    

    // Submit 버튼을 클릭했을 때 호출되는 메소드

    @IBAction func onSubmit(_ sender: Any){

        // presentingViewController 속성을 통해 이전 화면 객체를 읽어온 다음, ViewController 타입으로 캐스팅한다.

        let preVC = self.presentingViewController

        guard let vc = preVC as? ViewController else {

            return

        }

        

        // 값을 전달한다.

        vc.paramEmail = self.email.text

        vc.paramUpdate = self.isUpdate.isOn

        vc.paramInterval = self.interval.value

        

        // 이전 화면으로 복귀한다.

        self.presentingViewController?.dismiss(animated: true)

    }

    


    /*

    // 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.destination.

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

    }

    */


}











swift prepare sugue 스위프트 세그 데이터 전달



//

//  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)

        

    }

    

    @IBAction func onPerformSegue(_ sender: UIButton) {

        self.performSegue(withIdentifier: "manualSubmit", sender: self)

    }

    

    

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

        // 목적지 뷰 컨트롤러 인스턴스 읽어오기

        let dest = segue.destination

        

        guard let rvc = dest as? ResultViewController else {

            return

        }

        

        // 값 전달

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

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

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

    }


}



1. 메소드의 매개변수 segue의 속성 destination을 이용하여 목적지 뷰 컨트롤러의 인스턴스 참조를 가져옵니다.


2. 인스턴스의 타입을 UIViewController에서 ResultViewController 타입으로 캐스팅합니다. 실패하면 메소드 실행을 종료합니다.


3. 캐스팅된 인스턴스 상수 rvc를 이용하여 값을 전달합니다. 



//

//  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)

    }

    

}















+ Recent posts