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.

    }

    */


}








+ Recent posts