swift初心者ですが、現在swiftを用いて学校の時間割アプリを作っています。
内容としては、ViewControllerクラスに配置したUIボタン(月曜から金曜までの1~6限の36個)のどれかを押すと、それに応じてViewController2クラスに遷移して、UIテーブルビューに置かれたUIテキストビューから各項目(授業名、授業メモ、授業開始時間など)を編集できるようにするものです。
現段階では、ViewControllerクラスからViewController2クラスへのボタンによる画面遷移と、テーブルビューに授業名と授業メモを編集できるUIテキストビューの配置までが終わっています。
しかし、UIテーブルビューにUIDatePickerを配置し、授業開始時間を設定することと、ViewController2クラスの内容の保存と読み出しの実装方法がまったくわからず困っています。理想としては、ViewControllerクラスのボタンのタップによる遷移時に値の読み出し、ViewController2クラスで各項目の編集をして、前の画面に戻る際に値の保存をしたいと考えています。
お手数だとは思いますが、どのように実装すればよいか教えていただきたいです。
追記:遷移先のクラスです。講義開始時間のセルからdatepickerで値を入力させたいのと、元のクラスに戻る際(具体的にはviewWillDisappear)に各情報を保存させたいです。
swift
1import UIKit 2 3class ViewController2: UITableViewController{ 4 5 // 講義名 6 private let titleCell = UITableViewCell() 7 private let titleField = UITextField() 8 // 講義内容 9 private let textCell = UITableViewCell() 10 private let textField = UITextView() 11 // 講義開始時間 12 private let textCell2 = UITableViewCell() 13 private let textField2 = UITextField() 14 15 override func viewDidLoad() { 16 super.viewDidLoad() 17 // titleCellへtitleFieldを子画面として登録 18 if (self.titleField.superview == nil) { 19 self.titleCell.contentView.addSubview(self.titleField) 20 self.titleCell.addTextEditor(self.titleField) 21 } 22 // textCellへtextFieldを子画面として登録 23 if (self.textField.superview == nil) { 24 self.textCell.contentView.addSubview(self.textField) 25 self.textCell.addTextEditor(self.textField) 26 } 27 // textCell2へtextField2を子画面として登録 28 if (self.textField2.superview == nil) { 29 self.textCell2.contentView.addSubview(self.textField2) 30 self.textCell2.addTextEditor(self.textField2) 31 } 32 } 33 34 override func didReceiveMemoryWarning() { 35 super.didReceiveMemoryWarning() 36 } 37 38 // セクション数を返す 39 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 40 return 3 41 } 42 43 // セクションごとの項目数を返す 44 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 45 return 1 46 } 47 48 // 項目用のセルを返す 49 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 50 if (indexPath.section == 0) { 51 return self.titleCell 52 }else if (indexPath.section == 1){ 53 return self.textCell 54 }else{ 55 return self.textCell2 56 } 57 } 58 59 // ヘッダー 60 override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 61 if (section == 0) { 62 return "講義名" 63 }else if (section == 1){ 64 return "講義内容" 65 }else{ 66 return "講義開始時間" 67 } 68 } 69 70 // 項目ごとの高さ 71 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 72 if (indexPath.section == 0) { 73 return 30 74 }else if (indexPath.section == 1){ 75 return 100 76 }else{ 77 return 30 78 } 79 } 80extension UITableViewCell { 81 // subViewをcontentViewプロパティに登録 82 func addTextEditor(subView:UIView) { 83 self.contentView.addSubview(subView) 84 self.selectionStyle = UITableViewCellSelectionStyle.None 85 subView.setTranslatesAutoresizingMaskIntoConstraints(false) 86 self.contentView.addConstraint(NSLayoutConstraint(item:subView, attribute:.Left,relatedBy:.Equal, toItem:self.contentView, attribute:.Left, multiplier:1, constant:0)) 87 self.contentView.addConstraint(NSLayoutConstraint(item:subView, attribute:.Right,relatedBy:.Equal, toItem:self.contentView, attribute:.Right, multiplier:1, constant:0)) 88 self.contentView.addConstraint(NSLayoutConstraint(item:subView, attribute:.Top,relatedBy:.Equal, toItem:self.contentView, attribute:.Top, multiplier:1, constant:5)) 89 self.contentView.addConstraint(NSLayoutConstraint(item:subView, attribute:.Bottom,relatedBy:.Equal, toItem:self.contentView, attribute:.Bottom, multiplier:1, constant:-5)) 90 } 91 92}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/10/16 08:26