作っているプログラム
テキストフィールドに打ち込んだテキストを、所定のCSVファイル(Dropboxの中のtest.csv)に出力したい。
下の画像参照
コード
import
1 2class ViewController: UIViewController, UITextFieldDelegate, UIScrollViewDelegate{ 3 let text1: UITextField = UITextField(frame: CGRect(x: 0,y: 0,width: 200,height: 30)) 4 let text2: UITextField = UITextField(frame: CGRect(x: 0,y: 0,width: 200,height: 30)) 5 let button1: UIButton = UIButton(frame: CGRect(x: 0,y: 0,width: 200,height: 50)) 6 let sc = UIScrollView(); 7 var dataURL = URL(fileURLWithPath: "../../Dropbox/test.csv") 8 9 var txtActiveField = UITextField() 10 var csvData=[[String]]() 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 // Do any additional setup after loading the view, typically from a nib. 15 16 sc.frame = self.view.frame; 17 sc.backgroundColor = UIColor.cyan; 18 sc.delegate = self; 19 20 //textfileの位置を指定する 21 sc.contentSize = CGSize(width: 250,height: 1000) 22 self.view.addSubview(sc); 23 24 // 表示する文字を代入する. 25 text1.text = "" 26 text2.text = "" 27 28 // Delegateを設定する. 29 text1.delegate = self 30 text2.delegate = self 31 32 //CSVデータの作成 33 csvData.append(["text1.text","text2.text"]) 34 35 dataURL = csvData 36 37 // 枠を表示する. 38 text1.borderStyle = UITextBorderStyle.roundedRect 39 text2.borderStyle = UITextBorderStyle.roundedRect 40 41 // UITextFieldの表示する位置を設定する. 42 text1.layer.position = CGPoint(x:self.view.bounds.width/2,y:100); 43 text2.layer.position = CGPoint(x:self.view.bounds.width/2,y:200); 44 45 self.view.addSubview(text1) 46 self.view.addSubview(text2) 47 48 //UIButtonに表示されるテキスト 49 button1.setTitle("送信する", for: .normal) 50 51 //タップした状態のテキスト 52 button1.setTitle("送信する", for: .highlighted) 53 54 //タップした状態の色 55 button1.setTitleColor(UIColor.gray, for: .highlighted) 56 57 //テキストの色 58 button1.setTitleColor(UIColor.black, for: .normal) 59 60 //背景色 61 button1.backgroundColor = UIColor.white 62 63 //UIButtonの表示する位置を設定する. 64 button1.layer.position = CGPoint(x:self.view.bounds.width/2,y:400); 65 66 self.view.addSubview(button1) 67 // Viewに追加する 68 sc.addSubview(text1) 69 sc.addSubview(text2) 70 71 } 72 73 //改行ボタンが押された際に呼ばれる. 74 func textFieldShouldReturn(_ textField: UITextField) -> Bool { 75 textField.resignFirstResponder() 76 77 return true 78 } 79 80 //UITextFieldが編集された直後に呼ばれる. 81 func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { 82 txtActiveField = textField 83 return true 84 } 85 86 override func viewWillAppear(_ animated: Bool) { 87 super.viewWillAppear(animated) 88 89 let notificationCenter = NotificationCenter.default 90 notificationCenter.addObserver(self, selector: #selector(ViewController.handleKeyboardWillShowNotification(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 91 notificationCenter.addObserver(self, selector: #selector(ViewController.handleKeyboardWillHideNotification(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 92 } 93 94 @objc func handleKeyboardWillShowNotification(_ notification: Notification) { 95 96 let userInfo = notification.userInfo! 97 let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 98 let myBoundSize: CGSize = UIScreen.main.bounds.size 99 var txtLimit = txtActiveField.frame.origin.y + txtActiveField.frame.height + 8.0 100 let kbdLimit = myBoundSize.height - keyboardScreenEndFrame.size.height 101 102 print("テキストフィールドの下辺:((txtLimit))") 103 print("キーボードの上辺:((kbdLimit))") 104 105 if txtLimit >= kbdLimit { 106 sc.contentOffset.y = txtLimit - kbdLimit 107 } 108 } 109 110 @objc func handleKeyboardWillHideNotification(_ notification: Notification) { 111 sc.contentOffset.y = 0 112 } 113 114 115 override func didReceiveMemoryWarning() { 116 super.didReceiveMemoryWarning() 117 // Dispose of any resources that can be recreated. 118 } 119 120} 121 122 123
どなたか教えていただけないでしょうか?
回答よろしくお願いします。
あなたの回答
tips
プレビュー