質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

4727閲覧

異なるView Controllerでの値(変数)の共有

souroppy

総合スコア44

Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2017/02/05 08:29

この画像の二つのViewControllerで値を共有したいのですが、ViewController2のラベルに表示されるはずの100が表示されず、消えるはずのラベルが表示されたままです。
画像

順番としては、
1.AppDelegate.swiftでtastを宣言する
2.ViewControllerで、appDelegate.testに100を入れる
3.ViewController2でtestをtest1で受け取り、ラベルに表示させる
としたつもりです。

少し方法が違ってても全然良いので、異なるView Controllerで値を共有する方法を教えてください。

Swift

1import UIKit 2 3@UIApplicationMain 4class AppDelegate: UIResponder, UIApplicationDelegate { 5 6 var window: UIWindow? 7 var test:String? 8 9 10 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 11 // Override point for customization after application launch. 12 return true 13 } 14 15 func applicationWillResignActive(_ application: UIApplication) { 16 // 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. 17 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 18 } 19 20 func applicationDidEnterBackground(_ application: UIApplication) { 21 // 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. 22 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 23 } 24 25 func applicationWillEnterForeground(_ application: UIApplication) { 26 // 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. 27 } 28 29 func applicationDidBecomeActive(_ application: UIApplication) { 30 // 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. 31 } 32 33 func applicationWillTerminate(_ application: UIApplication) { 34 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 35 } 36 37 38} 39

Swift

1import UIKit 2 3class ViewController: UIViewController { 4 @IBOutlet weak var B: UIButton! 5 @IBOutlet weak var L: UILabel! 6 @IBAction func BA(_ sender: AnyObject) { 7 let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate 8 appDelegate.test = String(100) 9 } 10 11 12 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 // Do any additional setup after loading the view, typically from a nib. 17 } 18 19 override func didReceiveMemoryWarning() { 20 super.didReceiveMemoryWarning() 21 // Dispose of any resources that can be recreated. 22 } 23 24 25}

Swift

1import UIKit 2 3class ViewController2: UIViewController { 4 var test1 = String() 5 6 @IBOutlet weak var tetststs: UILabel! 7 @IBOutlet weak var fsg: UIButton! 8 @IBOutlet weak var erg: UILabel! 9 10 @IBAction func sjrsm(_ sender: AnyObject) { 11 tetststs.text = String(test1) 12 print(test1) 13 if test1 == String(100) { 14 erg.isHidden = true 15 16 } 17 } 18 override func viewDidLoad() { 19 super.viewDidLoad() 20 let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate 21 22 var test1 = appDelegate.test 23 24 25 // Do any additional setup after loading the view. 26 } 27 28 override func didReceiveMemoryWarning() { 29 super.didReceiveMemoryWarning() 30 // Dispose of any resources that can be recreated. 31 } 32 33 34 /* 35 // MARK: - Navigation 36 37 // In a storyboard-based application, you will often want to do a little preparation before navigation 38 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 39 // Get the new view controller using segue.destinationViewController. 40 // Pass the selected object to the new view controller. 41 } 42 */ 43 44} 45

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ローカル変数ではなくて、プロパティに設定しないと反映されませんViewController2viewDidLoadを以下のように変更してみてください。

swift

1override func viewDidLoad() { 2 super.viewDidLoad() 3 let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate 4 5 if let test = appDelegate.test { 6 test1 = test 7 } 8}

投稿2017/02/05 23:43

編集2017/02/06 00:22
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

souroppy

2017/02/06 12:11

ありがとうございます!解決しました。 まだ、プログラムを始めて半年も経っていないので初歩的なことを質問してしまうかもしれませんが、助けていただければ嬉しいです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問