Swiftの勉強でおみくじアプリを作っています。
年齢と名前を入力し結果ボタンを押すと入力した名前とともにランダムで大吉や凶などの結果が出るアプリです。
しかし、名前を入力しても結果に名前が反映されません。
きちんと
@IBOutlet weak var nameLabel: UILabel!
ラベルはヒモ付されています。
問題の箇所がどうすればきちんと反映されるでしょうか?
大吉などの運勢はきちんと表示されます。
また年齢は気持ちで入力するだけのためのもので運勢や名前の表示には関わりません。
「Xcodeのストーリーボード」
ここに表示されている田中一郎という名前はデフォルトで入れたものです。
「ソース」
import UIKit class omikuziViewController: UIViewController { //前のページから名前を受け取るための変数 var name:String? = "" //おみくじの結果を配列に格納 var result:[String] = ["大吉", "中吉", "小吉", "凶"] @IBOutlet weak var nameLabel: UILabel! @IBOutlet weak var resultLabel: UILabel! //viewDidLoadはページが表示された時に実行される override func viewDidLoad() { super.viewDidLoad() if let name = name { //nilが入っていなかった場合、Labelに名前を表示させる nameLabel.text = name + "さん" } //ランダムな数値0〜3を取得、結果に応じて配列からおみくじの値を表示させる let num = Int(arc4random_uniform(4)) resultLabel.text = "(result[num])です!" // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
「ビルド時のデバッグエリアの表示」
2018-09-05 17:05:51.163381+0900 omikuji[26365:3465120] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/xxxxxxxx/Library/Developer/CoreSimulator/Devices/xxxxxxxxx/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2018-09-05 17:05:51.167274+0900 omikuji[26365:3465120] [MC] Reading from private effective user settings. 2018-09-05 17:05:52.769508+0900 omikuji[26365:3465120] [Common] _BSMachError: port 6f03; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND" 2018-09-05 17:06:05.195513+0900 omikuji[26365:3465120] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "<NSLayoutConstraint:0x60400009fd10 UIImageView:0x7faf9de45620.bottom == UILayoutGuide:0x6040007a1500'UIViewSafeAreaLayoutGuide'.bottom (active)>", "<NSLayoutConstraint:0x604000284ab0 UIImageView:0x7faf9de45620.centerY == UIView:0x7faf9de43ec0.centerY (active)>", "<NSLayoutConstraint:0x604000284b00 V:|-(0)-[UIImageView:0x7faf9de45620] (active, names: '|':UIView:0x7faf9de43ec0 )>", "<NSLayoutConstraint:0x60400009e190 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x6040007a1500'UIViewSafeAreaLayoutGuide']-(34)-| (active, names: '|':UIView:0x7faf9de43ec0 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x604000284ab0 UIImageView:0x7faf9de45620.centerY == UIView:0x7faf9de43ec0.centerY (active)> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2018-09-05 17:06:19.265122+0900 omikuji[26365:3465120] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "<NSLayoutConstraint:0x6040002850a0 UIImageView:0x7faf9df25950.bottom == UILayoutGuide:0x6040007a1f80'UIViewSafeAreaLayoutGuide'.bottom (active)>", "<NSLayoutConstraint:0x604000283a70 UIImageView:0x7faf9df25950.centerY == UIView:0x7faf9df26fe0.centerY (active)>", "<NSLayoutConstraint:0x604000283ac0 V:|-(0)-[UIImageView:0x7faf9df25950] (active, names: '|':UIView:0x7faf9df26fe0 )>", "<NSLayoutConstraint:0x604000283c00 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x6040007a1f80'UIViewSafeAreaLayoutGuide']-(34)-| (active, names: '|':UIView:0x7faf9df26fe0 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x604000283a70 UIImageView:0x7faf9df25950.centerY == UIView:0x7faf9df26fe0.centerY (active)> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
ちなみにauto layoutは勉強中で何も設定していません。
回答2件
あなたの回答
tips
プレビュー