我有一个UIPopover,它显示了一个包含用一些文本填充的UITextView的普通视图。我设法突出显示了文本。当弹出窗口被取消并重新打开时,高亮显示将消失。即使应用程序已关闭,我也希望保持文本高亮显示。有什么想法可以实现吗
我使用的代码如下:
- (void)highlight {
NSRange selectedRange = self.textViewAll.selectedRange;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithAttributedString:self.textViewAll.attributedText];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:selectedRange];
// [highlightedRange addObject:];
// This is where i tried to save each location and length in a mutable array but didn't work
[highlightedRangeLocation insertObject:[NSNumber numberWithInteger:selectedRange.location] atIndex:indexOfHighlight];
[highlightedRangeLength insertObject:[NSNumber numberWithInteger:selectedRange.length] atIndex:indexOfHighlight];
///////////////////////////////////////////////////////////////////////////////
self.textViewAll.attributedText = attributedString;
indexOfHighlight ++ ;
}
- (void)viewDidLoad {
UIMenuItem *highlightMenuItem = [[UIMenuItem alloc] initWithTitle:@"Highlight" action:@selector(highlight)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:highlightMenuItem]];
float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
if (sysVer >= 8.0) {
self.textViewAll.layoutManager.allowsNonContiguousLayout = NO;
}
}
有人能指出如何从这里继续吗?
编辑1:
关闭弹出窗口的代码:
- (IBAction)closeFun:(id)sender {
// self.popoverPresentationController set
[self dismissViewControllerAnimated:YES completion:nil];
// [self dismis]
}
您不能在弹出窗口被取消时将突出显示的文本范围保存在[NSUserDefaults standardUserDefaults]中,并在弹出窗口再次出现时检索它吗?
我认为问题在于popover负责突出显示的状态,也就是说,是popover保持了这个事实/状态
popover是表示层/用户界面的一部分。当然,这个亮点代表了一个事实,即(现在是陷阱)-完全独立于popover
例如,突出显示一项任务可能表示该任务已到期。或者,将标签突出显示为红色可能意味着银行余额为负数。您可以看到,无论您使用什么用户界面元素,它们都只代表一些底层的业务现实。但是,当您创建一个popover实例时,您可能会将其设置为具有高亮显示的元素。但是,这个具体的popover实例在关闭时会死亡。
高光也随之消失。
当你点击某个按钮时(我想),会出现一个弹出窗口,但它是一个不同的实例这个实例不知道highlight。即使你以某种方式设法保持了popover的一个实例,并只是隐藏并再次显示它,popover也不应该负责知道某个东西是红色的还是到期的(并因此突出显示)
在您的应用程序中,您应该有一个良好分离的模型层。。。它基本上是一组表示状态的相关对象,即与应用程序从业务角度解决的问题相关的事实(例如,划线、计算兴趣、存储音乐、任何实际的事情)。这个模型层,其中的某个对象,应该存储事实。。也就是说,任务到期了,或者余额不足。
每次显示您的popover时,您都应该调查在显示popover时模型层中的基本事实。Ivestigating意味着找到一种程序化的方式来查看模型对象,找出其中的值,并在这一"调查"的基础上再次设置亮点。你不应该依赖这样一个事实,即它在不久的过去就被强调了。