我在ibaction中有这段代码
NSUserDefaults().setObject(TAValue1.text, forKey: TAValue1Key)
NSUserDefaults().setObject(TAValue2.text, forKey: TAValue2Key)
NSUserDefaults().setObject(TAValue3.text, forKey: TAValue3Key)
NSUserDefaults().setObject(TAValue4.text, forKey: TAValue4Key)
NSUserDefaults().setObject(TAValue5.text, forKey: TAValue5Key)
NSUserDefaults().setObject(TAValue6.text, forKey: TAValue6Key)
NSUserDefaults().setObject(TAValue7.text, forKey: TAValue7Key)
NSUserDefaults().setObject(TAValue8.text, forKey: TAValue8Key)
NSUserDefaults().setObject(TAValue9.text, forKey: TAValue9Key)
NSUserDefaults().setObject(TAHowMany1.text, forKey: TAHowMany1Key)
NSUserDefaults().setObject(TAHowMany2.text, forKey: TAHowMany2Key)
NSUserDefaults().setObject(TAHowMany3.text, forKey: TAHowMany3Key)
NSUserDefaults().setObject(TAHowMany4.text, forKey: TAHowMany4Key)
NSUserDefaults().setObject(TAHowMany5.text, forKey: TAHowMany5Key)
NSUserDefaults().setObject(TAHowMany6.text, forKey: TAHowMany6Key)
NSUserDefaults().setObject(TAHowMany7.text, forKey: TAHowMany7Key)
NSUserDefaults().setObject(TAHowMany8.text, forKey: TAHowMany8Key)
NSUserDefaults().setObject(TAHowMany9.text, forKey: TAHowMany9Key)
NSUserDefaults().setObject(TATotal1.text, forKey: TATotal1Key)
NSUserDefaults().setObject(TATotal2.text, forKey: TATotal2Key)
NSUserDefaults().setObject(TATotal3.text, forKey: TATotal3Key)
NSUserDefaults().setObject(TATotal4.text, forKey: TATotal4Key)
NSUserDefaults().setObject(TATotal5.text, forKey: TATotal5Key)
NSUserDefaults().setObject(TATotal6.text, forKey: TATotal6Key)
NSUserDefaults().setObject(TATotal7.text, forKey: TATotal7Key)
NSUserDefaults().setObject(TATotal8.text, forKey: TATotal8Key)
NSUserDefaults().setObject(TATotal9.text, forKey: TATotal9Key)
NSUserDefaults().setObject(TATotal10.text, forKey: TATotal10Key)
我觉得Xcode正在使用最后一个文本(TATotal10.text)并仅保存它。然后将其加载到我想要重新加载数据的每个文本字段框中。 如何创建单独保存的字符串? 干杯
这是我的加载数据代码。
override func viewDidLoad() {
TAValue1.text = NSUserDefaults().stringForKey(TAValue1Key)
TAValue2.text = NSUserDefaults().stringForKey(TAValue2Key)
TAValue3.text = NSUserDefaults().stringForKey(TAValue3Key)
TAValue4.text = NSUserDefaults().stringForKey(TAValue4Key)
TAValue5.text = NSUserDefaults().stringForKey(TAValue5Key)
TAValue6.text = NSUserDefaults().stringForKey(TAValue6Key)
TAValue7.text = NSUserDefaults().stringForKey(TAValue7Key)
TAValue8.text = NSUserDefaults().stringForKey(TAValue8Key)
TAValue9.text = NSUserDefaults().stringForKey(TAValue9Key)
TAHowMany1.text = NSUserDefaults().stringForKey(TAHowMany1Key)
TAHowMany2.text = NSUserDefaults().stringForKey(TAHowMany2Key)
TAHowMany3.text = NSUserDefaults().stringForKey(TAHowMany3Key)
TAHowMany4.text = NSUserDefaults().stringForKey(TAHowMany4Key)
TAHowMany5.text = NSUserDefaults().stringForKey(TAHowMany5Key)
TAHowMany6.text = NSUserDefaults().stringForKey(TAHowMany6Key)
TAHowMany7.text = NSUserDefaults().stringForKey(TAHowMany7Key)
TAHowMany8.text = NSUserDefaults().stringForKey(TAHowMany8Key)
TAHowMany9.text = NSUserDefaults().stringForKey(TAHowMany9Key)
TATotal1.text = NSUserDefaults().stringForKey(TATotal1Key)
TATotal2.text = NSUserDefaults().stringForKey(TATotal2Key)
TATotal3.text = NSUserDefaults().stringForKey(TATotal3Key)
TATotal4.text = NSUserDefaults().stringForKey(TATotal4Key)
TATotal5.text = NSUserDefaults().stringForKey(TATotal5Key)
TATotal6.text = NSUserDefaults().stringForKey(TATotal6Key)
TATotal7.text = NSUserDefaults().stringForKey(TATotal7Key)
TATotal8.text = NSUserDefaults().stringForKey(TATotal8Key)
TATotal9.text = NSUserDefaults().stringForKey(TATotal9Key)
TATotal10.text = NSUserDefaults().stringForKey(TATotal10Key)
}
希望有所帮助
答案 0 :(得分:1)
我怀疑你的关键常量都是相同的文字字符串。 (即,您复制/粘贴TATotalxxxKey
并重新编号,但没有更改每个@"literalstringxxx"
的定义
答案 1 :(得分:0)
或者您可以创建标签或文本字段数组,并执行以下操作:
for var i = 1; i <= TAValues.count; i++
{
NSUserDefaults().setObject(TAValues[i-1], forKey: "TAValue\(i)Key")
}
for var i = 0; i < TAHowManys.count; i++
{
NSUserDefaults().setObject(TAValues[i], forKey: "TAValue\(i+1)Key")
}
答案 2 :(得分:0)
感谢Joshua Nozzi的回复,我能够将我的代码改为此,并解决了这个问题。
var TAValue1Key = String(01)
var TAValue2Key = String(02)
var TAValue3Key = String(03)
var TAValue4Key = String(04)
var TAValue5Key = String(05)
var TAValue6Key = String(06)
var TAValue7Key = String(07)
var TAValue8Key = String(08)
var TAValue9Key = String(09)
var TAHowMany1Key = String(10)
var TAHowMany2Key = String(11)
var TAHowMany3Key = String(12)
var TAHowMany4Key = String(13)
var TAHowMany5Key = String(14)
var TAHowMany6Key = String(15)
var TAHowMany7Key = String(16)
var TAHowMany8Key = String(17)
var TAHowMany9Key = String(18)
var TATotal1Key = String(19)
var TATotal2Key = String(20)
var TATotal3Key = String(21)
var TATotal4Key = String(22)
var TATotal5Key = String(23)
var TATotal6Key = String(24)
var TATotal7Key = String(25)
var TATotal8Key = String(26)
var TATotal9Key = String(27)
var TATotal10Key = String(28)
以前我在每个字符串中没有数字()。
我还将保存代码更改为
NSUserDefaults().setObject(TAValue1.text, forKey: String(01))
NSUserDefaults().setObject(TAValue2.text, forKey: String(02))
NSUserDefaults().setObject(TAValue3.text, forKey: String(03))
NSUserDefaults().setObject(TAValue4.text, forKey: String(04))
NSUserDefaults().setObject(TAValue5.text, forKey: String(05))
NSUserDefaults().setObject(TAValue6.text, forKey: String(06))
NSUserDefaults().setObject(TAValue7.text, forKey: String(07))
NSUserDefaults().setObject(TAValue8.text, forKey: String(08))
NSUserDefaults().setObject(TAValue9.text, forKey: String(09))
NSUserDefaults().setObject(TAHowMany1.text, forKey: String(10))
NSUserDefaults().setObject(TAHowMany2.text, forKey: String(11))
NSUserDefaults().setObject(TAHowMany3.text, forKey: String(12))
NSUserDefaults().setObject(TAHowMany4.text, forKey: String(13))
NSUserDefaults().setObject(TAHowMany5.text, forKey: String(14))
NSUserDefaults().setObject(TAHowMany6.text, forKey: String(15))
NSUserDefaults().setObject(TAHowMany7.text, forKey: String(16))
NSUserDefaults().setObject(TAHowMany8.text, forKey: String(17))
NSUserDefaults().setObject(TAHowMany9.text, forKey: String(18))
NSUserDefaults().setObject(TATotal1.text, forKey: String(19))
NSUserDefaults().setObject(TATotal2.text, forKey: String(20))
NSUserDefaults().setObject(TATotal3.text, forKey: String(21))
NSUserDefaults().setObject(TATotal4.text, forKey: String(22))
NSUserDefaults().setObject(TATotal5.text, forKey: String(23))
NSUserDefaults().setObject(TATotal6.text, forKey: String(24))
NSUserDefaults().setObject(TATotal7.text, forKey: String(25))
NSUserDefaults().setObject(TATotal8.text, forKey: String(26))
NSUserDefaults().setObject(TATotal9.text, forKey: String(27))
NSUserDefaults().setObject(TATotal10.text, forKey: String(28))
因为我的加载代码是由字符串名定义的,而字符串名现在分配了一个字符串编号,所以不需要更改我的加载代码。 对于基于其他视图控制器.swift文件的所有加载代码,我只需将字符串命名为我想要的,并使用第一个.swift中的字符串编号进行分配。 E.G我在另一个视图控制器中使用字符串TATotal10Key。
我把它命名为
var TaxableTotalText = String(28)
只要它具有与之关联的正确数字,它就可以被称为我喜欢的。
再次感谢您的帮助。
答案 3 :(得分:-1)
要在NSUSerDefaults中存储数据,请尝试此
// Saving data in nsuserdefaults
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:TAValue1.text forKey:TAValue1Key];
[defaults setObject:TAValue2.text forKey:TAValue2Key];
[defaults setObject:TAValue3.text forKey:TAValue3Key];
[defaults setObject:TAValue4.text forKey:TAValue4Key];
[defaults setObject:TAValue5.text forKey:TAValue5Key];
// Fetching data from nsuserdefaults corresponding to key
NSString* taValue1 = [defaults objectForKey:TAValue1Key];
NSString* taValue2 = [defaults objectForKey:TAValue2Key];
NSString* taValue3 = [defaults objectForKey:TAValue3Key];
NSString* taValue4 = [defaults objectForKey:TAValue4Key];
NSString* taValue5 = [defaults objectForKey:TAValue5Key];
试试这个。希望它会帮助你..谢谢