问题描述
我有什么:
文本视图
NSArray(字符串)
NSArray (string)
AVAudioplayer(尚未实现)
AVAudioplayer (not yet implemented)
当我在 TextView 中选择一个词时 :
• 检查数组中是否存在单词
• Check if word exist in Array
• 启动带有关联声音的音频播放器
• Start Audioplayer with associated sound
不幸的是,当我点击两次以在 TextView 中选择一个单词时,textViewDidChangeSelection 被调用了两次.我不知道为什么我看到两次Youpie".
Unfortunately when I tap twice to select a word inside TextView, textViewDidChangeSelection is called twice. I don’t know why I see "Youpie" twice.
我只是将 inputView 更改为隐藏键盘,因为我只需要在选择模式下使用 TextView.
I just changed inputView to hide keyboard because I only need TextView to be used in selecting mode.
- (void)textViewDidChangeSelection:(UITextView *)tve;
{
NSString *selectedText = [tve textInRange:tve.selectedTextRange];
if(selectedText.length > 0)
{
for (NSString *text in textArray)
{
if ([selectedText isEqualToString:text])
NSLog(@"Youpie");
tve.selectedTextRange = nil;
if (ps1.playing == YES)
{
[self stopEveryPlayer];
[self updateViewForPlayerState:ps1];
}
else if ([ps1 play])
{
[self updateViewForPlayerState:ps1];
fileName.text = [NSString stringWithFormat: @"%@ (%d ch.)", [[ps1.url relativePath] lastPathComponent], ps1.numberOfChannels, nil];
}
else
NSLog(@"Could not play %@\n", ps1.url);
break;
}
}
}
}
- (void)awakeFromNib
{
textArray = [[NSArray alloc] initWithObjects:@"dog",@"cat",@"person",@"bird",@"mouse", nil];
textView.inputView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
textView.delegate = self;
// ...
}
当我双击文本中的每个好词时,我注意到了一些东西.
I noticed something when I was double tapping on each good word in my text.
textViewDidChangeSelection
如果已经选择了一个单词并且没有选择任何操作,我有 1 个Youpie".
If a word is already selected and no action choosen, I have 1 "Youpie".
如果没有,我有 2 个Youpie".
If not, I have 2 "Youpie".
我找到了一个简单的解决方案.我在获得价值后删除了 selectedRange .textViewDidChangeSelection 调用一次.
I found a simple solution. I removed selectedRange after getting value. textViewDidChangeSelection called once.
我改变了什么
tve.selectedTextRange = nil;
我使用 UITextView
的子类来禁用菜单.
I use a subclass of UITextView
to disable menu.
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return NO;
return [super canPerformAction:action withSender:sender];
}
我也为 AVAudioPlayer
(ps1) 添加了一个实现.
I added an implementation for AVAudioPlayer
(ps1) too.
如果一个已知单词正在选择,我的自动播放"正在工作:)
My "autoplay" is working if a known word is selecting :)
推荐答案
我不知道为什么该方法被调用两次或如何防止这种情况发生,但替代解决方案可能是在双击单词时在文本视图中弹出的编辑菜单.然后,可以从该附加菜单项中定义的操作选择器触发基于单词启动声音的操作.在此设计中,您将删除 textViewDidChangeSelection,因此不会被调用两次.请参阅 http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/AddingCustomEditMenuItems/AddingCustomEditMenuItems.html 了解有关修改标准菜单的一些附加信息.
I don't have an answer for why the method gets called twice or how to prevent this, but an alternative solution might be to display an additional item in the edit menu that pops up in a text view when a word is double clicked. Then, your action for initiating a sound based on the word could be triggered from the action selector defined in that additional menu item. In this design, you'd remove your textViewDidChangeSelection and thus would not get called twice. See http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/AddingCustomEditMenuItems/AddingCustomEditMenuItems.html for some additional info about modifying the standard menu.
这篇关于UITextView textViewDidChangeSelection 调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!