import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
// この変数を他のクラスからアクセスする
// データ受け渡しグローバルフィールド。空要素(nil)を追加する
var globalStrings01:String? = nil
var userId:Int = 0
func application(application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(application: UIApplication) {
}
func applicationDidEnterBackground(application: UIApplication) {
}
func applicationWillEnterForeground(application: UIApplication) {
}
func applicationDidBecomeActive(application: UIApplication) {
}
func applicationWillTerminate(application: UIApplication) {
}
}
import UIKit
class DelegateAViewController: UIViewController , UITextFieldDelegate {
@IBOutlet weak var globalStrings01: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// textFiel の情報を受け取るための delegate を設定
globalStrings01?.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Second(sender: UIButton) {
// AppDelegateクラスのメンバー変数を参照する
var app:AppDelegate =
(UIApplication.sharedApplication().delegate as! AppDelegate)
println(app.userId)
println(app.globalStrings01)
app.globalStrings01 = globalStrings01.text
// キーボードを閉じる
globalStrings01.resignFirstResponder()
}
}
import UIKit
class DelegateBViewController: UIViewController {
@IBOutlet weak var localLabel01: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(animated: Bool) {
// AppDelegateクラスのメンバー変数を参照する
var app:AppDelegate =
(UIApplication.sharedApplication().delegate as! AppDelegate)
println(app.userId)
println(app.globalStrings01)
self.localLabel01.text = app.globalStrings01
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
GitHub AppDelegateSwift
コメントをお書きください