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

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

新規登録して質問してみよう
ただいま回答率
85.48%
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++と共存することが意図されています

Q&A

解決済

2回答

832閲覧

Xcodeで文字列がうまく表示されない

退会済みユーザー

退会済みユーザー

総合スコア0

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++と共存することが意図されています

0グッド

0クリップ

投稿2018/09/05 09:35

編集2018/09/05 09:39

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は勉強中で何も設定していません。

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

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

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

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

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

fuzzball

2018/09/06 00:26

「前のページから名前を受け取るための変数」とコメントが書いてありますが、前のページでnameに値を渡しているコードを書いて下さい。
退会済みユーザー

退会済みユーザー

2018/09/06 01:59

すいません。私の勘違いでした。参考にしたサイトの下の下の方にデータの渡し方が書いてあり、それを記述していなかったのが原因でした。申し訳ありません。swiftはいらなくなったのかな?そんなわけないよな?とちょっと不審には思ってたのですが、不勉強でした。すいません。
guest

回答2

0

ベストアンサー

//次の画面に名前を渡す処理 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { //次のページの情報を取得する //次のページはOmikuziViewController Classなので、それに合わせてas!でダウンキャストする let omikuziController = segue.destination as! OmikuziViewController //1ページ目の名前のtextFieldの値にnilが入っていないかチェックをする if let name = nameField.text { //nilチェックが通ったら、次のページのnameに、値を代入する omikuziController.name = name } }

データを渡す記述がなかったのが原因でした。申し訳ありません。

投稿2018/09/06 01:59

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

if let name = name { //nilが入っていなかった場合、Labelに名前を表示させる nameLabel.text = name + "さん" }else { nameLabel.text = "nil" }

投稿2018/09/05 09:46

編集2018/09/05 09:47
panyayan

総合スコア36

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

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

退会済みユーザー

退会済みユーザー

2018/09/05 10:32

ありがとうございます。試してみた所、「さん」は表示されるのですが入力した名前は表示されません。紐づけ等私の中でわかるあらゆる可能性を試してみます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問