0

我有UIWebView阅读内容。之后,我NSUserDefaults在 Viewcontroller 中使用字符串从内容存储中选择一些文本。然后我检索这个字符串值并显示在UITextViewviewcontroller1 中。但UITextView不是从视图控制器更新文本。NSUserDefaults也没有更新文本。我正在使用[self.navigationController pushViewController:viewcontroller1 animated:YES];切换到 viewcontroller1。

视图控制器:

[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];

    }

视图控制器1:

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];
4

2 回答 2

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];
}
于 2013-10-05T07:50:35.973 回答
0

我检查了您的代码,只需更改NSSTringNSString如下所示即可。

-(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];

}
于 2013-10-05T07:19:53.180 回答