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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

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

Q&A

解決済

2回答

574閲覧

画面遷移をした時に、前のViewの状態を保持したい。

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

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

0グッド

1クリップ

投稿2017/08/23 13:35

現在脱出ゲームのアプリを作っています。
しかし、
・一つ目の画面で鍵をとる
・隣のページに進んでまた戻るとまた鍵が取れてしまう。

こうならないように、隣のページに行っても前の鍵をとった状態のままにするためにはどうすれば良いでしょうか。

swift

1import UIKit 2 3class ViewController: UIViewController { 4 5 6 7 8 var flg = 0 9 10 var serifu: UITextView! 11 var delate: UIButton! 12 var key: UIImageView! 13 14 15 16 17 override func viewDidLoad() { 18 19 20 var viewcon2 = SecondViewController() 21 22 23 //背景の設定 24 let bg = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)) 25 bg.image = UIImage(named: "sasa") 26 self.view.addSubview(bg) 27 28 //右ボタンの生成 29 let rightButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) 30 rightButton.backgroundColor = UIColor.cyan 31 rightButton.layer.masksToBounds = true 32 rightButton.layer.cornerRadius = 30.0 33 rightButton.setTitle("右", for: .normal) 34 rightButton.layer.position = CGPoint(x: self.view.bounds.width - 30, y: self.view.bounds.height/2) 35 rightButton.addTarget(self, action: #selector(clickMyButton(sender:)), for: .touchUpInside) 36 self.view.addSubview(rightButton) 37 38 // 左ボタンを作成. 39 let leftButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) 40 leftButton.backgroundColor = UIColor.red 41 leftButton.layer.masksToBounds = true 42 leftButton.setTitle("左", for: .normal) 43 leftButton.layer.cornerRadius = 25.0 44 leftButton.layer.position = CGPoint(x: self.view.bounds.width / 6, y:self.view.bounds.height/2) 45 leftButton.addTarget(self, action: #selector(onClickRightButton), for: .touchUpInside) 46 self.view.addSubview(leftButton) 47 48 49 //酒だなタッチ反応ボタンの作成 50 let touchyouso: UIButton = UIButton(frame: CGRect(x: 100, y: 120, width: 20, height: 80)) 51 touchyouso.addTarget(self, action: #selector(touchAction), for: .touchUpInside) 52 self.view.addSubview(touchyouso) 53 54 } 55 56 57 58 59 60 //ここからボタンイベント 61 62 //酒だなタッチ反応のaction 63 func touchAction(sender: UIButton) { 64 //セリフを呼び出す 65 if flg == 0 { 66 self.serifu = UITextView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height/5)) 67 serifu.backgroundColor = UIColor.black 68 serifu.alpha = 0.4 69 serifu.text = "酒の裏に鍵が隠されていた" 70 serifu.textColor = UIColor.white 71 self.view.addSubview(serifu) 72 73 self.key = UIImageView(frame: CGRect(x: self.view.frame.size.width/2 , y: self.view.frame.size.height/2, width: self.view.frame.size.width/5, height: self.view.frame.size.width/5)) 74 let keyImage: UIImage = UIImage(named: "key1")! 75 key.image = keyImage 76 self.view.addSubview(key) 77 78 79 self.delate = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)) 80 delate.addTarget(self, action: #selector(delateTalk), for: .touchUpInside) 81 delate.addTarget(self, action: #selector(delateButton), for: .touchUpInside) 82 delate.addTarget(self, action: #selector(delateItem), for: .touchUpInside) 83 self.view.addSubview(delate) 84 flg += 1 85 } 86 } 87 88 //セリフを消す 89 func delateTalk(sender: UIButton) { 90 print("getKey") 91 serifu.removeFromSuperview() 92 } 93 //ボタンを消す 94 func delateButton(sender: UIButton) { 95 delate.removeFromSuperview() 96 } 97 //アイテムを消す 98 func delateItem(sender: UIButton) { 99 key.removeFromSuperview() 100 } 101 102 func clickMyButton(sender: UIButton) { 103 104 //遷移するViewを定義する 105 let secondViewController: UIViewController = SecondViewController() 106 107 108 secondViewController.modalTransitionStyle = .coverVertical 109 self.present(secondViewController, animated: false, completion: nil) 110 } 111 112 internal func onClickRightButton(sender: UIButton){ 113 114 115 // 遷移するViewを定義. 116 let myViewController: UIViewController = fourthViewController() 117 // アニメーションを設定. 118 myViewController.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal 119 // Viewの移動. 120 self.present(myViewController, animated: true, completion: nil) 121 print("OK") 122 } 123} 124

secondViewController

import UIKit class SecondViewController: UIViewController { override func viewDidLoad() { let bg = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)) bg.image = UIImage(named: "IMG_8952") bg.layer.zPosition = -1 self.view.addSubview(bg) let label : UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) label.backgroundColor = UIColor.darkGray label.text = "second" self.view.addSubview(label) // 左ボタンを作成. let leftButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) leftButton.backgroundColor = UIColor.red leftButton.layer.masksToBounds = true leftButton.setTitle("左", for: .normal) leftButton.layer.cornerRadius = 25.0 leftButton.layer.position = CGPoint(x: self.view.bounds.width / 6, y:self.view.bounds.height/2) leftButton.addTarget(self, action: #selector(onClickMyButton), for: .touchUpInside) self.view.addSubview(leftButton); // 右ボタンを作成. let rightButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) rightButton.backgroundColor = UIColor.purple rightButton.layer.masksToBounds = true rightButton.setTitle("右", for: .normal) rightButton.layer.cornerRadius = 25.0 rightButton.layer.position = CGPoint(x: self.view.bounds.width - 30, y:self.view.bounds.height/2) rightButton.addTarget(self, action: #selector(onClickRightButton), for: .touchUpInside) self.view.addSubview(rightButton); } /* ボタンイベント. */ internal func onClickMyButton(sender: UIButton){ // 遷移するViewを定義. let myViewController: UIViewController = ViewController() // アニメーションを設定. myViewController.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal // Viewの移動. self.present(myViewController, animated: true, completion: nil) } internal func onClickRightButton(sender: UIButton){ // 遷移するViewを定義. let myViewController: UIViewController = TirdeViewController() // アニメーションを設定. myViewController.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal // Viewの移動. self.present(myViewController, animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

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

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

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

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

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

guest

回答2

0

ベストアンサー

シングルトンパターンを利用するのが楽だと思います。

swift3でシングルトンクラスを作ってみる

Swift

1////////////////////////////// 2/// ゲームステータスクラス 3////////////////////////////// 4class GameStatus: NSObject { 5 static let sharedInstance = GameStatus() 6 var isKeyObtained = false 7} 8 9////////////////////////////// 10/// ゲーム処理のどこか 11////////////////////////////// 12 13// 鍵を取得した時 14GameStatus.sharedInstance.isKeyObtained = true 15 16// ゲーム内の別の場所で、キーを持っているか判定する時 17if GameStatus.sharedInstance.isKeyObtained { 18 // 鍵を使う処理 19} 20

投稿2017/08/24 00:38

YokemuraTakeshi

総合スコア297

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

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

0

戻る処理は、SecondViewControllerのonClickMyButton(sender:)でしょうか?
この中で、

swift

1let myViewController: UIViewController = ViewController()

と、新しいインスタンスを生成して、そこに遷移していますので、これでは「戻る」ではなく別の一つ目の部屋に移動することになります。

戻る時はdismiss(animated:completion:)を使って下さい。

投稿2017/08/24 00:29

fuzzball

総合スコア16731

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

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

退会済みユーザー

退会済みユーザー

2017/08/24 22:15

dismiss は前の状態に戻る、と理解しました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問