ver
swift4/xcode9
swift
1import UIKit 2 3class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 4 5 @IBOutlet weak var segmentedControl: UISegmentedControl! 6 @IBOutlet weak var tableView: UITableView! 7 8 var name = ["cell add btr"]//["ipad","mac","iPhone"] 9 var subname = ["cell add btr"] 10 var subname2 = ["cell add btr"]//["one","two","three","four"] 11 var subname3 = ["add segment"]//["apple","peach","orrange"] 12 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 tableView.delegate = self 17 tableView.dataSource = self 18 // Do any additional setup after loading the view, typically from a nib. 19 } 20 21 @IBAction func addsegmentctr(_ sender: Any) { 22 switch segmentedControl.selectedSegmentIndex { 23 case 3: 24 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 25 26 // OKボタンの設定 27 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 28 (action:UIAlertAction!) -> Void in 29 30 // OKを押した時入力されていたテキストを表示 31 if let textFields = alert.textFields { 32 33 // アラートに含まれるすべてのテキストフィールドを調べる 34 for textField in textFields { 35 //indexPath.rowで1,2,3,4のような順になる 36 self.subname3.insert(textField.text!, at:self.subname3.count - 1) 37 } 38 self.tableView.reloadData() 39 40 } 41 }) 42 alert.addAction(okAction) 43 44 // キャンセルボタンの設定 45 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 46 alert.addAction(cancelAction) 47 48 // テキストフィールドを追加 49 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 50 textField.placeholder = "テキスト" 51 }) 52 53 54 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 55 56 // アラートを画面に表示 57 self.present(alert, animated: true, completion: nil) 58 default: 59 break 60 } 61 } 62 func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { 63 return indexPath 64 } 65 66 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 67 switch segmentedControl.selectedSegmentIndex { 68 case 0: 69 return name.count 70 case 1: 71 return subname.count 72 case 2: 73 return subname2.count 74 default: 75 break 76 } 77 78 return 0 79 } 80 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 81 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 82 83 84 switch segmentedControl.selectedSegmentIndex { 85 case 0: 86 cell.textLabel?.text = name[indexPath.row] 87 cell.backgroundColor = UIColor.red 88 case 1: 89 cell.textLabel?.text = subname[indexPath.row] 90 cell.backgroundColor = UIColor.blue 91 case 2: 92 cell.textLabel?.text = subname2[indexPath.row] 93 cell.backgroundColor = UIColor.lightGray 94 default: 95 break 96 } 97 98 return cell 99 } 100 101 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ 102 103 let cell = tableView.cellForRow(at: indexPath) 104 105 //first 106 if name[indexPath.row] != "cell add btr"{ 107 108 }else{ 109 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 110 111 // OKボタンの設定 112 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 113 (action:UIAlertAction!) -> Void in 114 115 // OKを押した時入力されていたテキストを表示 116 if let textFields = alert.textFields { 117 118 // アラートに含まれるすべてのテキストフィールドを調べる 119 for textField in textFields { 120 //indexPath.rowで1,2,3,4のような順になる 121 self.name.insert(textField.text!, at:self.name.count - 1) 122 } 123 self.tableView.reloadData() 124 125 } 126 }) 127 alert.addAction(okAction) 128 129 // キャンセルボタンの設定 130 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 131 alert.addAction(cancelAction) 132 133 // テキストフィールドを追加 134 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 135 textField.placeholder = "テキスト" 136 }) 137 138 139 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 140 141 // アラートを画面に表示 142 self.present(alert, animated: true, completion: nil) 143 } 144 145 //second 146 if subname[indexPath.row] != "cell add btr"{ 147 148 }else{ 149 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 150 151 // OKボタンの設定 152 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 153 (action:UIAlertAction!) -> Void in 154 155 // OKを押した時入力されていたテキストを表示 156 if let textFields = alert.textFields { 157 158 // アラートに含まれるすべてのテキストフィールドを調べる 159 for textField in textFields { 160 //indexPath.rowで1,2,3,4のような順になる 161 self.subname.insert(textField.text!, at:self.subname.count - 1) 162 } 163 self.tableView.reloadData() 164 165 } 166 }) 167 alert.addAction(okAction) 168 169 // キャンセルボタンの設定 170 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 171 alert.addAction(cancelAction) 172 173 // テキストフィールドを追加 174 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 175 textField.placeholder = "テキスト" 176 }) 177 178 179 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 180 181 // アラートを画面に表示 182 self.present(alert, animated: true, completion: nil) 183 } 184 185 //third 186 if subname2[indexPath.row] != "cell add btr"{ 187 188 }else{ 189 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 190 191 // OKボタンの設定 192 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 193 (action:UIAlertAction!) -> Void in 194 195 // OKを押した時入力されていたテキストを表示 196 if let textFields = alert.textFields { 197 198 // アラートに含まれるすべてのテキストフィールドを調べる 199 for textField in textFields { 200 //indexPath.rowで1,2,3,4のような順になる 201 self.subname2.insert(textField.text!, at:self.subname2.count - 1) 202 } 203 self.tableView.reloadData() 204 205 } 206 }) 207 alert.addAction(okAction) 208 209 // キャンセルボタンの設定 210 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 211 alert.addAction(cancelAction) 212 213 // テキストフィールドを追加 214 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 215 textField.placeholder = "テキスト" 216 }) 217 218 219 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 220 221 // アラートを画面に表示 222 self.present(alert, animated: true, completion: nil) 223 } 224 225 226 } 227 228 @IBAction func segmentedChanged(_ sender: Any) { 229 tableView.reloadData() 230 } 231 override func didReceiveMemoryWarning() { 232 super.didReceiveMemoryWarning() 233 // Dispose of any resources that can be recreated. 234 } 235 236 237}
追加後のイメージはこのような感じです。
やりたいこと
add segmentをタップ時にalert textが表示され入力したものを新たなsegment controlにしたい
出来たこと
add segmentをタップ時にalert textを呼び出すまで出来た。
困っていること
入力したものをsegment control内に追加することが出来ない。
(出来るかどうかも分からない)
私なりにネットで調べてみたのですがこのような実装をしている人はいなかったのでもし出来るのならやり方を知りたいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/12/11 01:11
2017/12/11 08:35
退会済みユーザー
2017/12/13 03:45