viewcontrollerに複数のボタンを作成したいため、classを使って制御をしようと思いこのサイトを参考に作成しましたが、
self.view.addSubview(myButton)のところでviewのメンバにSubjectが入っていないことでエラーが発生しました。
self.viewのところをviewcontrollerに関連付けるには、どう記述したらよいのでしょうか。
swift
1import UIKit 2import Foundation 3 4 5private class Subject: UIButton{ 6 7 private var myButton: UIButton! 8 let string = "" 9 10 func makeButton(xx:Int,yy:Int){//場所を指定する引数 11 init(frame: CGRect){ 12 suprt.init(frame:frame) 13 } 14 // Buttonを生成する. 15 myButton = UIButton() 16 17 // サイズを設定する. 18 myButton.frame = CGRectMake(0,0,200,40) 19 20 // 背景色を設定する. 21 myButton.backgroundColor = UIColor.redColor() 22 23 // 枠を丸くする. 24 myButton.layer.masksToBounds = true 25 26 // タイトルを設定する(通常時). 27 myButton.setTitle("ボタン(通常)", forState: UIControlState.Normal) 28 myButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) 29 30 // タイトルを設定する(ボタンがハイライトされた時). 31 myButton.setTitle("ボタン(押された時)", forState: UIControlState.Highlighted) 32 myButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted) 33 34 // コーナーの半径を設定する. 35 myButton.layer.cornerRadius = 20.0 36 37 // ボタンの位置を指定する. 38 myButton.layer.position = CGPoint(x: xx, y:yy) 39 40 // タグを設定する. 41 myButton.tag = 1 42 43 // イベントを追加する. 44 myButton.addTarget(self, action: Selector("onClickMyButton:"), forControlEvents: .TouchUpInside) 45 46 // ボタンをViewに追加する. 47 self.view.addSubview(myButton) 48 } 49 50 required init?(coder aDecoder: NSCoder) { 51 fatalError("init(coder:) has not been implemented") 52 } 53 54}
###補足情報
Xcode7.3.1
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。