概要
現在練習で自作のカスタムアラートを作っています。
カスタムアラート内に自作の数字のテンキーを用意しています。
自作のテンキーでいくつかのtextfieldsの値の入力、そしてラベルに表示させています。
出来ている事
アラートの呼び出し、自作のテンキーでtextFieldsに入力までは出来ました。
やりたい事
私がやりたい事としてはセグメントで選択したテキストフィールドのみ入力出来、ラベルに表示させたいです
###困っている事
現在、自作のテンキーで入力までは出来たのですが、本来なら選択したテキストフィールド、ラベルのみ表示される仕様ですが他のテキストフィールド、ラベルまで同時表示されてしまいます。
そのため、セグメントで選択されたもののみ入力、表示されるようにしたいです。
ソースコードはgitを見て頂けると幸いです。
git: CustomAlert
改良した点
swift
1import UIKit 2 3 4class weightView: UIView { 5 6 @IBOutlet weak var weightlbl: UILabel! 7 @IBOutlet weak var bfplbl: UILabel! 8 @IBOutlet weak var choice: UISegmentedControl! 9 10 var delegate:CustomAlertDelegate? 11 var dataSource = DataSource() 12 13 14 override init(frame: CGRect){ 15 super.init(frame: frame) 16 loadNib() 17 } 18 19 required init(coder aDecoder: NSCoder) { 20 super.init(coder: aDecoder)! 21 loadNib() 22 } 23 24 func loadNib(){ 25 if let view = Bundle.main.loadNibNamed("weightView", owner: self, options: nil)?.first as? UIView { 26 view.frame = self.bounds 27 self.addSubview(view) 28 } 29 } 30 31 @IBAction func selectedSegment(_ sender: UISegmentedControl) { 32 guard let selectedType = DataSource.InputType(rawValue: sender.selectedSegmentIndex) else { return } 33 dataSource.inputType = selectedType 34 } 35 36 @IBAction func tapNumber(_ sender: UIButton) { 37 let text = sender.tag == 10 ? "." : String(sender.tag) 38 dataSource.upDate(value: text) 39 labelUpdate() 40 } 41 42 func labelUpdate() { 43 weightlbl.text = dataSource.wValue + "kg" 44 bfplbl.text = dataSource.bValue + "%" 45 print(dataSource.wValue) 46 print(dataSource.bValue) 47 } 48 49 @IBAction func clear(_ sender: UIButton) { 50 dataSource.clear() 51 labelUpdate() 52 } 53 54 @IBAction func OKbtr(_ sender: Any) { 55 let cellString = "体重" + weightlbl.text! + "体脂肪率" + bfplbl.text! 56 delegate?.appendData(str: cellString, color: bfplbl.textColor) 57 self.removeFromSuperview() 58 } 59 60 @IBAction func CANCELbtr(_ sender: Any) { 61 self.removeFromSuperview() 62 } 63 64 class DataSource { 65 66 enum InputType: Int { 67 case p, f 68 } 69 70 var inputType: InputType = .p 71 var wValue = "" 72 var bValue = "" 73 74 func upDate(value: String) { 75 switch inputType { 76 case .p: 77 guard (wValue + value).numberOfOccurrences(of: ".") < 2 else { return } 78 wValue += value 79 case .f: 80 guard (bValue + value).numberOfOccurrences(of: ".") < 2 else { return } 81 bValue += value 82 } 83 84 } 85 func clear() { 86 switch inputType { 87 case .p: 88 wValue = "" 89 case .f: bValue = "" 90 } 91 } 92 } 93 94}
swift
1import Foundation 2 3extension String { 4 func numberOfOccurrences(of word: String) -> Int { 5 var count = 0 6 var nextRange = self.startIndex..<self.endIndex 7 while let range = self.range(of: word, options: .caseInsensitive, range: nextRange) { 8 count += 1 9 nextRange = range.upperBound..<self.endIndex 10 } 11 return count 12 } 13}
入力されるlabelは2つになっています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/07/11 16:54
2018/07/11 23:39
退会済みユーザー
2018/07/12 04:34
2018/07/12 22:43
退会済みユーザー
2018/07/13 04:55 編集
2018/07/15 03:27
退会済みユーザー
2018/07/24 02:21