やりたい事
ビューコントローラの特定クラスのビューを取得したい
困っていること
UIViewController を継承したクラス EachViewController のインスタンスに UIView を継承したクラス EachView のインスタンス eachView を override func viewDidload の中で addSubview しました。これを他の関数の中で取得するにはどうしたら良いでしょうか?
日本語だとややこしいので未完成のコードを載せます。
swift
1// 2// EachViewController.swift 3// ColorSample 4// 5 6import UIKit 7 8class EachViewController: UIViewController { 9 10 var index: Int = 0 11 var rgba: UIColor = UIColor.black 12 13 func setIndexTo(_ index: Int) { 14 self.index = index 15 let eachView = self.view as! EachView // ここでランタイムエラー 16 eachView.setIndexTo(index) 17 } 18 19 func setBackgroundRgba(_ backgroundRgba: UIColor) { 20 rgba = backgroundRgba 21 let eachView = self.view as! EachView 22 eachView.backgroundColor = rgba 23 } 24 25 override func viewDidLoad() { 26 super.viewDidLoad() 27 28 let eachView = EachView(frame: view.bounds) 29 30 eachView.backgroundColor = rgba 31 eachView.setIndexTo(index) 32 view.addSubview(eachView) 33 } 34 35 override func didReceiveMemoryWarning() { 36 super.didReceiveMemoryWarning() 37 // Dispose of any resources that can be recreated. 38 } 39}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/23 08:56