我有UIWebView
阅读内容。之后,我NSUserDefaults
在 Viewcontroller 中使用字符串从内容存储中选择一些文本。然后我检索这个字符串值并显示在UITextView
viewcontroller1 中。但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];