xcode 为什么我的 UITextView 中没有显示键盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6304599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does the keyboard not show in my UITextView?
提问by madmax
I have a container class which is a view controller.
It holds the UITextView for notes and has another view controller as a subview, called it PDFViewController. That one has an array of view controllers, called them PageViewController's, in it and a UIScrollView so i can swipe through the different view controllers from the array.
我有一个容器类,它是一个视图控制器。
它保存用于笔记的 UITextView,并有另一个视图控制器作为子视图,称为 PDFViewController。那个有一个视图控制器数组,称为 PageViewController's,在它和一个 UIScrollView 中,所以我可以从数组中滑动不同的视图控制器。
Each of the PageViewController's has also an UIScrollView, so i can zoom the different pages.
每个 PageViewController 也有一个 UIScrollView,所以我可以缩放不同的页面。
But when I show my UITextView i cannot edit or write anything.
It shows a blinking cursor, but no keyboard and can't write text.
When i highlight a word from the default text, it shows me some dictionary options but the words won't be replaced.
但是当我显示我的 UITextView 时,我无法编辑或写任何东西。
它显示一个闪烁的光标,但没有键盘并且不能写文本。
当我从默认文本中突出显示一个单词时,它会向我显示一些字典选项,但这些单词不会被替换。
I just don't know where the problem might be.
我只是不知道问题可能出在哪里。
container.m (View Controller)
container.m(视图控制器)
- (void) initNotes {
notesVisible = FALSE;
notes = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, note_width, note_height)];
notes.backgroundColor = [UIColor yellowColor];
notes.font = [UIFont fontWithName:@"Arial" size:24];
notes.text = @"Hier ist Platz für Ihre Notizen";
container = [[UIView alloc] initWithFrame:CGRectMake(start_x, start_y, container_width, container_height)];
container.backgroundColor = [UIColor yellowColor];
[container.layer setShadowColor:[[UIColor blackColor] CGColor]];
[container.layer setShadowOffset:CGSizeMake(2.0, 3.0)];
[container.layer setShadowOpacity:0.6];
[container.layer setShadowRadius:5];
container.layer.shouldRasterize = YES;
[container addSubview:notes];
[self.view addSubview:container];
}
- (void) showTextView {
[UIView beginAnimations:@"MoveAndStrech" context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
container.frame = CGRectMake(start_x, self.view.center.y-container_height, container_width, container_height);
[UIView commitAnimations];
notesVisible = !notesVisible;
}
- (void) hideTextView {
[UIView beginAnimations:@"MoveAndStrech" context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
container.frame = CGRectMake(start_x, start_y, container_width, container_height);
// [notes resignFirstResponder];
[UIView commitAnimations];
notesVisible = !notesVisible;
}
@implementation PDFViewController
@implementation PDFViewController
- (void)awakeFromNib
{
painting = false;
dataInstance = [PDFDataInstance sharedInstance];
chapter = [dataInstance chapter];
[self getNumberOfPages];
kNumberOfPages = dataInstance.pages;
// Register observer to be called when a cell from BookmarkPDFController is pressed
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didSelectBookmark:)
name:@"bookmarkPressedinPDFView" object:nil];
// Register observer to be called when a cell from BookmarkPDFController is pressed
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(togglePainting:)
name:@"togglePainting" object:nil];
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++)
{
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.delegate = self;
scrollView.directionalLockEnabled = YES;
currentPage = [[chapter currentPage] integerValue];
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
//
// Load pages based on currentPage cause of when coming from bookmarks
[self loadScrollViewWithPage:currentPage];
[self loadScrollViewWithPage:currentPage+1];
// update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * currentPage;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
}
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
// replace the placeholder if necessary
PageViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
//page+1 cause CGPDF indexing starts with 1
controller = [[PageViewController alloc] initWithPageNumberAndUrl:page+1: [chapter urlOnFilesystem]];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
TextView delegates
TextView 委托
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(@"begin editing");
}
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText
{
NSLog(@"something changed");
return YES;
}
回答by LiweiZ
Just want to add another possibility here. In the iOS simulator, make sure the "Hardware/Keyboard/Connect Hardware Keyboard" is not checked. It took me a couple of hours to figure out this. I guess I toggled that option by accidently pressing the shortcut. Not good experience :(
只是想在这里添加另一种可能性。在 iOS 模拟器中,请确保未选中“硬件/键盘/连接硬件键盘”。我花了几个小时才弄清楚这一点。我想我不小心按下了快捷方式切换了该选项。不好的体验:(
As pictured, Connect Hardware Keyboard should be UN-checked.
如图,连接硬件键盘应该是未选中的。
回答by Nir Golan
No one asked , but have you set :
没有人问,但你有没有设置:
notes.editable = YES ;
?
?
回答by lostInTransit
Is the UITextView receiving touches? Implement the UITextViewDelegate methods and see if they get called (esp textViewDidBeginEditing:and textView: shouldChangeTextInRange: replacementText:). This way you'll know for sure whether the text view is indeed handling the touches or not.
UITextView 是否接收触摸?实施UITextViewDelegate方法,看看他们是否得到所谓(ESP textViewDidBeginEditing:和TextView的:shouldChangeTextInRange:replacementText: )。这样你就可以确定文本视图是否确实在处理触摸。
If it does, then I don't see anything wrong with your code (except you might have accidentally added a view overlapping the textview)
如果是这样,那么我看不出您的代码有任何问题(除非您可能不小心添加了与 textview 重叠的视图)
回答by Adarsh V C
Make sure that in the delegate of your textview. Following method is returning YES.
确保在你的 textview 的委托中。以下方法返回YES。
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
return YES;
}
回答by Nicolas S
Try without container
, just use the UITextView (notes
) as the container itself.
Every time you used container use notes instead.
尝试不container
使用,只需使用 UITextView ( notes
) 作为容器本身。每次使用容器时,请改用注释。
And then dont:
然后不要:
[container addSubview:notes];
[self.view addSubview:container];
Just:
只是:
[self.view addSubview:notes];
Tell me how it goes
告诉我进展如何
回答by Nicolas S
After you have set the notes delegates, do the following.
设置笔记代表后,请执行以下操作。
- (void)textViewDidBeginEditing:(UITextView *)textView {
if ([textView isEqual:notes]) {
NSLog(@"notes begin editing");
}
}
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText {
if ([aTextView isEqual:notes]) {
NSLog(@"something changed in notes");
}
return YES;
}
This should confirm delegates of notes are indeed getting called.
这应该确认笔记的代表确实被召唤了。
回答by GraehamF
I had similar behavior, the UITextView
not responding to touches and the keyboard not showing.
我有类似的行为,UITextView
没有响应触摸和键盘没有显示。
My UITextView
was layered on top of a UICollectionView
which was receiving all the touches even though the UITextView
was visibly on top.
我的UITextView
被分层在一个UICollectionView
接收所有触摸的顶部,即使它UITextView
明显在顶部。
I fixed it by adjusting the frame of the UICollectionView
so that the UITextView
has nothing behind it. After that, all the delegate methods were called like a charm.
我通过调整框架来固定它,UICollectionView
使其UITextView
后面没有任何东西。之后,所有的委托方法都像魅力一样被调用。
Hope this helps.
希望这可以帮助。
回答by Kapil Mandlik
you need to implement your container class with UITextViewDelegate and add notes.delegate=self; just after initialize the notes object in initNotes method.
你需要用 UITextViewDelegate 实现你的容器类并添加 notes.delegate=self; 在 initNotes 方法中初始化 notes 对象之后。
回答by Rachit
Check for 2 things:
检查两件事:
- Is notes.editable = YES; if so please change it to NO.
- 是 notes.editable = YES; 如果是,请将其更改为 NO。
2.Check whether your UITextView delegates are called are not. If not Please make sure you include in your implementation file and set notes.delegate = self.
2.检查您的 UITextView 委托是否被调用。如果不是请确保您包含在您的实现文件中并设置 notes.delegate = self。
回答by Avinash shirsath
please Refer this one. where ContantView is your view/textfield and _bubbletable is your table.
请参考这个。其中 ContantView 是您的视图/文本字段,_bubbletable 是您的表。
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.2f animations:^{
CGRect frame = _ContantView.frame;
frame.origin.y -= kbSize.height;
_ContantView.frame = frame;
frame = _bubbleTable.frame;
frame.size.height -= kbSize.height;
_bubbleTable.frame = frame;
}];
}
}