【问题标题】:Saving text within a UITextView with NSUserDefaults?使用 NSUserDefaults 在 UITextView 中保存文本?
【发布时间】:2013-01-21 15:39:36
【问题描述】:

有没有办法使用 NSUserDefaults 将文本保存在 UITextView 中?当我将它用于 UITextField 时,它工作得很好。这是我用于 UITextField 的代码:

-(void)viewDidLoad {

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 NSString *text1 = [defaults objectForKey:@"text1"];
 textfield1.text = text1;

}

-(IBAction)保存按钮 {

 [textfield1 resignFirstResponder];
 NSString *text1 = [textfield1 text];
 NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
 [myDefaults setObject:text1 forKey:@"text1"];
 [myDefaults synchronize];
 NSLog(@"Data Saved")

}

但是,如果我使用这种简单的方法来保存 UITextView 而不是 UITextField,则视图中的数据不会保存。有什么建议吗?

【问题讨论】:

    标签: ios6 uitextview xcode4.5 nsuserdefaults


    【解决方案1】:

    您用于 UITextField 的完全相同的代码应该适用于 UITextView; text 方法适用于两者。

    这段代码对我有用:

    [textView resignFirstResponder] ;
    NSString* textViewContents = textView.text ;
    NSLog(@"textViewContents is: %@", textViewContents) ;
    
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults] ;
    [userDefaults setObject:textViewContents forKey:@"textView"] ;
    [userDefaults synchronize] ;
    

    我猜你的问题在于访问你的 UITextView 的文本。 NSLog 它,就像我在上面做的那样,看看它是否如你所料。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 2013-11-13
      • 1970-01-01
      相关资源
      最近更新 更多