Swiftで携帯アプリを作っています。
またStoryBoardは使用していなく、コードで全て作成しています。
TextFieldとUIButtonを設置し、ボタンをタップすることでそこに入力されたテキストを.txtの形で指定したフォルダに保存したいです。
どこのサイトを見ても、コード内の""にテキストを書き込みそれをパスで指定したフォルダに入れるというような解説しか載っていません。
どなたかご教授願います。
ちなみに今のところ、下のようなコードを書くところまで来ています。
import
1 2class ViewController: UIViewController, UITextFieldDelegate, UIScrollViewDelegate{ 3 var text1: UITextField = UITextField(frame: CGRect(x: 0,y: 0,width: 200,height: 30)) 4 var text2: UITextField = UITextField(frame: CGRect(x: 0,y: 0,width: 200,height: 30)) 5 let sc = UIScrollView(); 6 7 var txtActiveField = UITextField() 8 var csvData=[[String]]() 9 10 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","text2"]) 34 35 36 // 枠を表示する. 37 text1.borderStyle = UITextBorderStyle.roundedRect 38 text2.borderStyle = UITextBorderStyle.roundedRect 39 40 // UITextFieldの表示する位置を設定する. 41 text1.layer.position = CGPoint(x:self.view.bounds.width/2,y:100); 42 text2.layer.position = CGPoint(x:self.view.bounds.width/2,y:200); 43 44 self.view.addSubview(text1) 45 self.view.addSubview(text2) 46 47 48 //button1の設定 49 let button1: UIButton = UIButton(frame: CGRect(x: 0,y: 0,width: 200,height: 50)) 50 51 button1.setTitle("送信する", for: .normal) //UIButtonに表示されるテキスト 52 53 button1.setTitle("送信する", for: .highlighted) //タップした状態のテキスト 54 55 button1.setTitleColor(UIColor.gray, for: .highlighted) //タップした状態の色 56 57 button1.setTitleColor(UIColor.black, for: .normal) //テキストの色 58 59 button1.backgroundColor = UIColor.white //背景色 60 61 button1.layer.position = CGPoint(x:self.view.bounds.width/2,y:400); //UIButtonの表示する位置を設定する. 62 63 64 self.view.addSubview(button1) 65 66 // Viewに追加する 67 sc.addSubview(text1) 68 sc.addSubview(text2) 69 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
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/25 09:21