###前提・実現したいこと
evernoteのtodoリストのようなものを搭載したアプリを作ろうとしています。
今のところ、改行したら新しいtextfieldと新しいチェックボックスを生成できるものはできているのですが数々の問題があります。
evernoteのチェックボックス機能のように、ボタンを文字列のように扱えたら楽なのかなと思うのですが、どのように改善すればいいでしょうか。ご教授いただけたら幸いです。
###発生している問題・エラーメッセージ
①一度生成したチェックボックスを削除できない(できれば文字のように削除できるようにしたい)
②文頭(左端)以外にチェックボックスを配置できない
###該当のソースコード
まず親クラスのコードです
import
1 2class ViewController: UIViewController, UITextFieldDelegate { 3 4 @IBOutlet weak var testLabel: UILabel! 5 @IBOutlet weak var firstBox: CheckBox! 6 7 var newBox: CheckBox! 8 9 @IBOutlet weak var firstTextField: UITextField! 10 11 var counter = 0 12 13 var newTextField: UITextField! 14 15 var currentTextField: UITextField! 16 17 func textFieldShouldReturn(_ textField: UITextField) -> Bool 18 { 19 textField.resignFirstResponder() 20 counter += 1 21 createNewTextFieldAndBox(parameter: counter) 22 return true 23 } 24 25 func createNewTextFieldAndBox(parameter: Int) 26 { 27 //information of the textfields 28 let tWidth = Int(firstTextField.frame.width) //250 29 let tHeight = Int(firstTextField.frame.height) //30 30 let XofTextField = Int(firstTextField.frame.origin.x) 31 let YofTextField = (Int(firstTextField.frame.origin.y) + 16)*parameter + 20 32 33 //information of the boxes 34 let bWidth = Int(firstBox.frame.width) 35 let bHeight = Int(firstBox.frame.height) 36 let XofBox = Int(firstBox.frame.origin.x) 37 let YofBox = (Int(firstBox.frame.origin.y) + 16)*parameter + 20 38 39 //create new TF 40 newTextField = UITextField(frame: CGRect(x: XofTextField, y: YofTextField, width: tWidth, height: tHeight)) 41 self.view.addSubview(newTextField) 42 currentTextField = newTextField 43 currentTextField.delegate = self 44 45 //create new Box 46 newBox = CheckBox(frame: CGRect(x: XofBox, y: YofBox, width: bWidth, height: bHeight)) 47 newBox.awakeFromNib() 48 newBox.setImage(#imageLiteral(resourceName: "uncheckedBox"), for: .normal) 49 self.view.addSubview(newBox) 50 51 testLabel.text = String(describing: firstBox.frame.origin.y) 52 53 } 54 55 override func viewDidLoad() { 56 super.viewDidLoad() 57 // Do any additional setup after loading the view, typically from a nib. 58 firstTextField.delegate = self 59 } 60 61 override func didReceiveMemoryWarning() { 62 super.didReceiveMemoryWarning() 63 // Dispose of any resources that can be recreated. 64 } 65 66 67} 68
以下サブクラスのコードです
import
1 2class CheckBox: UIButton { 3 var isChecked:Bool = false 4 { 5 didSet 6 { 7 if isChecked == true 8 { 9 self.setImage(#imageLiteral(resourceName: "checkedBox"), for: .normal) 10 } 11 else 12 { 13 self.setImage(#imageLiteral(resourceName: "uncheckedBox"), for: .normal) 14 } 15 } 16 } 17 18 override func awakeFromNib() 19 { 20 self.addTarget(self, action: #selector(self.buttondClicked(_:)), for: UIControlEvents.touchUpInside) 21 self.isChecked = false 22 } 23 24 func buttondClicked(_ sender:UIButton) 25 { 26 if(sender == self) 27 { 28 if isChecked == true 29 { 30 isChecked = false 31 } 32 else 33 { 34 isChecked = true 35 } 36 } 37 } 38}
###試したこと
上記のコードを直してみたりネットで調べてみたりしたのですがうまくいきません。
###補足情報(言語/FW/ツール等のバージョンなど)
言語:Swift3
環境:Xcode8
あなたの回答
tips
プレビュー