ViewControllerに今までベタ書きしてしまっていたので、viewを別クラスに分離しようと考えました。
ボタンやテキストの中身の設定の処理を分離してみました。
迷っているところですが、addTargetの部分です。
今まではViewControllerの中でButtonなどのViewを関数で定義していて、関数内にaddTargetも含めていたのですが、画面遷移を行なっているものなどをしているものもあり、どうすれば良いのかわからなくなってしまいました。
良い解決方法があれば教えてください、根本的に私のやり方が間違っている場合は参考になるURLなど教えていただけたら嬉しいです。
UIの設定を詰め込んだクラス
Swift
1 2class UIParts: NSObject { 3 static func taskButton(view: UIViewController, color: UIColor)->UIButton{ 4 let button = UIButton() 5 button.layer.masksToBounds = true 6 button.layer.cornerRadius = 50.0 7 button.frame = CGRect(x:0,y:0,width:view.view.frame.width/4,height:view.view.frame.width/4) 8 button.backgroundColor = color 9 button.layer.position = CGPoint(x: view.view.frame.midX, y: view.view.frame.height - view.view.frame.midY) 10 button.setTitle("タスク", for: UIControlState.normal) 11 return button 12 } 13 14 static func rainButton(this view: UIViewController)->UIButton{ 15 let button = UIButton() 16 let image :UIImage? = UIImage(named:"croudy") 17 button.frame = CGRect(x:0,y:0,width:view.view.frame.width/2,height:view.view.frame.width/6) 18 button.layer.masksToBounds = true 19 button.layer.position = CGPoint(x: view.view.frame.midX, y: view.view.frame.height/13) 20 button.setImage(image, for: .normal) 21 button.translatesAutoresizingMaskIntoConstraints = false 22 return button 23 } 24 static func reloadButton(this view:UIViewController)->UIButton{ 25 let button = UIButton() 26 let image :UIImage? = UIImage(named:"reload") 27 button.frame = CGRect(x:0,y:0,width:view.view.frame.width/6,height:view.view.frame.width/6) 28 button.layer.masksToBounds = true 29 button.layer.cornerRadius = 20.0 30 button.layer.position = CGPoint(x: view.view.frame.width/10, y: view.view.frame.height/13) 31 button.setImage(image, for: .normal) 32 return button 33 } 34 static func addButton(this view: UIViewController)->UIButton{ 35 let image :UIImage? = UIImage(named:"add") 36 let button = UIButton() 37 button.frame = CGRect(x:0,y:0,width:view.view.frame.width/6,height:view.view.frame.width/6) 38 button.layer.masksToBounds = true 39 button.layer.cornerRadius = 20.0 40 button.layer.position = CGPoint(x: view.view.frame.width - view.view.frame.width/10, y: view.view.frame.height/13) 41 button.setImage(image, for: .normal) 42 return button 43 } 44 static func toDoText(this view: UIViewController, title: String)->UITextView{ 45 let textView = UITextView() 46 textView.frame = CGRect(x:view.view.frame.width/5, y:30, width:view.view.frame.width - view.view.frame.width/5, height:60) 47 textView.layer.position = CGPoint(x: view.view.frame.width/2, y: view.view.frame.height - view.view.frame.height/7) 48 textView.text = title 49 textView.font = UIFont.systemFont(ofSize: 20.0) 50 textView.layer.borderWidth = 1 51 textView.isEditable = true 52 return textView 53 } 54}
呼び出し元
Swift
1//雨ボボタン 2 self.rainButton = UIParts.rainButton(this: self) 3 self.rainButton.addTarget(self, action: 4 #selector(self.startApplication), for: .touchUpInside) 5 self.view.addSubview(self.rainButton) 6 //リロードボタン 7 self.reloadButton = UIParts.reloadButton(this: self) 8 self.reloadButton.addTarget(self, action: 9 #selector(self.reload(sender:)), for: .touchUpInside) 10 self.view.addSubview(self.reloadButton) 11 //タスク追加ボタン 12 self.addButton = UIParts.addButton(this: self) 13 self.addButton.addTarget(self, action: 14 #selector(self.addTask(sender:)), for: .touchUpInside) 15 self.view.addSubview(self.addButton)
コードはざっとですが、このような形になっています。
迷っているとは書いてありますが、何と何で迷っているかが書いてありませんので回答のしようがありません。
回答1件
あなたの回答
tips
プレビュー