LoginSignup
1
1

More than 5 years have passed since last update.

xcodeでViewController間で値のやり取りをする方法

Posted at

appDelegate.swiftに変数を作成し、アプリ全体で使えるグローバル変数にする。

AppDelegate.swift
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var count:Int = 0
}
ViewController.swift

    //delegateオブジェクトを作る
    let delegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    delegate.count = 10 //このようにして、データを代入したり、やり取りを行ったりする

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1