如何在Swift中使用一个Button保存来自多个UITextView的文本

时间:2015-07-10 11:30:11

标签: ios swift textview

We have the following stuff

我们有4个出口(UITextView),最后是一个保存按钮。

我希望能够使用一个简单的按钮保存我们在每个TextView中编写的任何内容。怎么做?我已经完成了这项工作,但保存了一个UITextView。

代码:

class agendaDeSuen_os: UIViewController , UITextFieldDelegate, UITextViewDelegate {      

    @IBOutlet weak var scrollView: UIScrollView!     
    @IBOutlet weak var primerTextView: UITextView!        
    @IBOutlet weak var segundoTextView: UITextView!        
    @IBOutlet weak var tercerTextView: UITextView!        
    @IBOutlet weak var cuartoTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        //ScrollViewSize
        scrollView.contentSize.height = 1400

        //Hide keyboard with touching anywhere
        var tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard")
        view.addGestureRecognizer(tap)


        //Button
        let stringKey = NSUserDefaults.standardUserDefaults()

        segundoTextView.text = stringKey.stringForKey("savedStringKey")

    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()

        return true
    }


    //Hide keyboard with return button
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        if text == "\n"
        {
            primerTextView.resignFirstResponder()
            segundoTextView.resignFirstResponder()
            tercerTextView.resignFirstResponder()
            cuartoTextView.resignFirstResponder()

            return false
        }
        return true
    }

    @IBAction func botonGuardar(sender: AnyObject) {

        let myText = segundoTextView.text


        NSUserDefaults.standardUserDefaults().setObject(myText, forKey: "savedStringKey")
        NSUserDefaults.standardUserDefaults().synchronize()

        var alert = UIAlertController(title: "Guardado", message:"Tu texto se ha guardado", preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))

        self.presentViewController(alert, animated: true, completion: nil)




    }








    //Hide keyboard with touching anywhere

    func DismissKeyboard(){


        view.endEditing(true)

    }

}

3 个答案:

答案 0 :(得分:1)

你能不能在botonGuardar函数中再添加三个NSUserDefualts,例如。

let myPrimerText = PrimerTextView.text

NSUserDefaults.standardUserDefaults().setObject(myPrimerText, forKey: "savedPrimerStringKey")

答案 1 :(得分:1)

我假设您正在谈论将所有文本保存到NSUserDefaults。 只需做你已经完成的事情,但每个人都有不同的按键。

NSUserDefaults.standardUserDefaults().setObject(myText, forKey: "textView1")
NSUserDefaults.standardUserDefaults().setObject(differentText, forKey: "textView2")
NSUserDefaults.standardUserDefaults().synchronize()

然后你只需使用不同的键来访问不同的保存对象(在你的情况下使用字符串)。

这有意义吗?

卢瓦克

答案 2 :(得分:1)

  

将插座插入阵列。使用快速枚举

import pandas as pd
import numpy as np

# generate some artificial data
# ================================
np.random.seed(0)
df1 = pd.DataFrame(dict(value1=np.random.choice('a b'.split(), 10), value2=np.random.randint(1, 10, 10)))

df2 = pd.DataFrame(dict(a=np.random.randn(10), b=np.random.randn(10)), columns=['a', 'b'], index=np.arange(1, 11))

df1

Out[178]: 
  value1  value2
0      a       6
1      b       3
2      b       5
3      a       8
4      b       7
5      b       9
6      b       9
7      b       2
8      b       7
9      b       8


df2

Out[179]: 
         a       b
1   2.5452  0.0334
2   1.0808  0.6806
3   0.4843 -1.5635
4   0.5791 -0.5667
5  -0.1816 -0.2421
6   1.4102  1.5144
7  -0.3745 -0.3331
8   0.2752  0.0474
9  -0.9608  1.4627
10  0.3769  1.5350



# processing: one liner lookup function
# =======================================================
# df1.value2 is the index and df1.value1 is the column
df1['new_values'] = df2.lookup(df1.value2, df1.value1)

Out[181]: 
  value1  value2  new_values
0      a       6      1.4102
1      b       3     -1.5635
2      b       5     -0.2421
3      a       8      0.2752
4      b       7     -0.3331
5      b       9      1.4627
6      b       9      1.4627
7      b       2      0.6806
8      b       7     -0.3331
9      b       8      0.0474