为什么键盘没有显示在我的UITextView中?

时间:2011-06-10 09:41:10

标签: iphone xcode uitextview

我有一个容器类,它是一个视图控制器 它包含用于注释的UITextView,并且具有另一个视图控制器作为子视图,称为PDFViewController。那个有一个视图控制器数组,在它们中称为PageViewController,和一个UIScrollView,所以我可以从数组中刷过不同的视图控制器。

每个PageViewController都有一个UIScrollView,所以我可以缩放不同的页面。

但是当我展示我的UITextView时,我无法编辑或写任何东西 它显示一个闪烁的光标,但没有键盘,也无法写入文本 当我突出显示默认文本中的单词时,它会显示一些字典选项,但不会替换单词。

我只是不知道问题出在哪里。

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

- (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委托

- (void)textViewDidBeginEditing:(UITextView *)textView {

NSLog(@"begin editing");
}

- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText
{
NSLog(@"something changed"); 
return YES; 
}

10 个答案:

答案 0 :(得分:53)

只想在这里增加另一种可能性。在iOS模拟器中,确保未选中“硬件/键盘/连接硬件键盘”。我花了几个小时来弄明白这一点。我想通过意外按下快捷方式切换了该选项。不好的经历:(

Menus

如图所示,连接硬件键盘应该经过联合国检查。

答案 1 :(得分:4)

没有人问,但你有没有问过:

notes.editable = YES ; 

答案 2 :(得分:4)

UITextView是否接受了接触?实现UITextViewDelegate方法并查看它们是否被调用(esp textViewDidBeginEditing: textView:shouldChangeTextInRange:replacementText:)。通过这种方式,您可以确定文本视图是否确实处理了触摸。

如果确实如此,那么我认为您的代码没有任何问题(除非您可能意外添加了与textview重叠的视图)

答案 3 :(得分:2)

确保在textview的委托中。以下方法返回YES。

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
  return YES;
}

答案 4 :(得分:1)

您需要使用UITextViewDelegate实现容器类 并添加notes.delegate = self;刚刚在initNotes方法中初始化notes对象之后。

答案 5 :(得分:1)

检查两件事:

  1. 是notes.editable = YES;如果是这样,请将其改为NO。
  2. 2.检查您的UITextView代表是否被调用。如果没有请确保包含在您的实施文件中并设置notes.delegate = self。

答案 6 :(得分:1)

尝试不使用container,只需使用UITextView(notes)作为容器本身。 每次使用容器时都会使用注释。

然后不要:

[container addSubview:notes];
[self.view addSubview:container];

只需:

[self.view addSubview:notes];

告诉我它是怎么回事

答案 7 :(得分:1)

设置备注委托后,请执行以下操作。

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

这应该确认说明的代表确实被召唤。

答案 8 :(得分:1)

我有类似的行为,UITextView没有响应触摸而键盘没有显示。

我的UITextView位于UICollectionView之上,即使UITextView明显位于最顶层,也接受了所有触摸。

我通过调整UICollectionView的框架来修复它,以便UITextView背后没有任何内容。之后,所有委托方法都被称为魅力。

希望这有帮助。

答案 9 :(得分:0)

请参考这个。其中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;

}];

}