swift
1import UIKit 2 3class topViewController: ViewController { 4 5 @IBOutlet var titlelbl: UILabel? 6 7 // ClassMethodクラスを定義 8 class ClassMethod { 9 } 10 // メタタイプを取得 11 let type = ClassMethod.self 12 // 型名の文字列を生成 13 let typeString = String(describing: ClassMethod()) 14 15 16 @IBAction func StartButton(_ sender: Any) { 17 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 18 19 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 20 (action:UIAlertAction!) -> Void in 21 22 if let textFields = alert.textFields { 23 24 for textField in textFields { 25 if let text = textField.text { 26 print(textField.text!) 27 //self.cellData.append(CellData(text)) 28 29 self.navigationItem.title = text 30 //遅延に関するコード 31 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 32 // your code here 33 self.performSegue(withIdentifier: "ViewControllersegue", sender: nil) 34 } 35 } 36 } 37 } 38 }) 39 alert.addAction(okAction) 40 41 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 42 alert.addAction(cancelAction) 43 44 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 45 textField.placeholder = "テキスト" 46 }) 47 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 48 // アラートを画面に表示 49 self.present(alert, animated: true, completion: nil) 50 51 } 52 53override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 54 if segue.identifier == "ViewControllersegue" { 55 let nextView: ViewController = segue.destination as! ViewController 56 57 nextView.titleName = typeString 58 } 59 } 60
やりたい事
ClassMethod
のlet typeString
をif let textFields = alert.textFields {
に結びつけたい
何故やるのか
アラートで入力したものを遷移先のviewcontrollerのtitleで表示させるため
////
出来ている事
ちゃんと遷移先で意図した表示をされるのかを確かめる為var itme = "name"
にして実行し、遷移先のtitleが変わっているまでは確認できた。
その為、変数をlet textFieldsに結びつける事が出来れば意図した意図した事ができると思われる。
追記 navigationItemのボタンの名前について
遷移先でボタンの名前も変わっていましたので以下のようにしたところ、ボタンの固定できるようになりました。
let backButtonItem = UIBarButtonItem(title: "back", style: .plain, target: nil, action: nil)
navigationItem.backBarButtonItem = backButtonItem
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/27 16:11
2018/05/27 16:23
退会済みユーザー
2018/05/27 17:03 編集
2018/05/27 17:13
退会済みユーザー
2018/05/27 17:25
2018/05/27 17:28
退会済みユーザー
2018/05/27 17:38
2018/05/27 17:45 編集
退会済みユーザー
2018/05/27 17:50
2018/05/27 17:53
退会済みユーザー
2018/06/01 16:56
2018/06/02 00:23
退会済みユーザー
2018/06/03 21:21