NSUserDefaults没有获取存储的字符串值

时间:2013-10-05 06:02:24

标签: iphone uiwebview nsstring uitextview nsuserdefaults

我有UIWebView阅读内容。之后,我使用Viewcontroller中的NSUserDefaults从字符串中选择一些文本。然后我检索此字符串值并在UITextView中显示到viewcontroller1。但是UITextView没有更新viewcontroller中的文本。 NSUserDefaults也没有更新文本。我使用[self.navigationController pushViewController:viewcontroller1 animated:YES];切换到viewcontroller1。

的ViewController:

[wbCont loadHTMLString:webString baseURL:nil];
    [self.view addSubview:wbCont];

    NSMutableArray *items = [[[UIMenuController sharedMenuController] menuItems] mutableCopy];
    if (!items) items = [[NSMutableArray alloc] init];

    UIMenuItem *menuItem;
    menuItem = [[UIMenuItem alloc] initWithTitle:@"BookMark" action:@selector(save:)];
    [items addObject:menuItem];
    [menuItem release];
    menuItem = [[UIMenuItem alloc] initWithTitle:@"Note" action:@selector(note:)];
    [items addObject:menuItem];
    [menuItem release];

    [[UIMenuController sharedMenuController] setMenuItems:items];
    [items release];


- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(save:))
    {
        return YES;



    }
    else if (action == @selector(note:))
    {
        return YES;
    }
    else if (action == @selector(copy:))
    {
        return NO;
    }

    return [super canPerformAction:action withSender:sender];
}


    -(void)save:(id)sender{
        NSString   *selection = [wbCont stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];

        NSLog(@"selection is %@",selection);

         NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];  
            [userData1 setObject:selection forKey:@"preferenceName"];

            [userData1 synchronize];

           [self.navigationController pushViewController:note animated:YES];

    }

viewcontroller1:

textView = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,400)];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    // getting an NSString
    NSString *savedValue = [prefs stringForKey:@"preferenceName"];
NSLog(@"saved is %@", savedValue);
textView.text = [NSString stringWithFormat:@"%@", savedValue];

2 个答案:

答案 0 :(得分:2)

textView的分配位于viewDidLoad

textView的更新位于viewWillAppear

-(void) viewWillAppear:(BOOL)animated
    [super viewWillAppear:animated];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    // getting an NSString
    NSString *savedValue = [prefs stringForKey:@"preferenceName"];
    NSLog(@"saved is %@", savedValue);
    textView.text = [NSString stringWithFormat:@"%@", savedValue];
}

答案 1 :(得分:0)

我检查了您的代码,只需将NSSTring更改为NSString,如下所示。

-(void)save:(id)sender{
//    NSSTring   *selection = [wbCont stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];

    NSString   *selection = [wbCont stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
    NSLog(@"selection is %@",selection);

     NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];  
        [userData1 setObject:selection forKey:@"preferenceName"];

        [userData1 synchronize];

       [self.navigationController pushViewController:note animated:YES];

}