2012-04-12 77 views
3

我无法获得上面显示的工具栏(就像苹果用完成按钮)键盘。任何事情都能想到它为什么不显示?像下面的图片一样。工具栏不会显示在键盘上方

enter image description here

.H

@interface CustomerNotesController : UITableViewController <UITextViewDelegate> 
{ 
    UIToolbar *noteToolbar; 
    IBOutlet UITextView *note; 
    id delegate; 
} 

@property (nonatomic, strong) IBOutlet UITextView *note; 
@property (nonatomic, retain) id delegate; 
@property (nonatomic, retain) UIToolbar *noteToolbar; 

.M

@synthesize note, noteToolbar, delegate; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [note setDelegate:self]; 
    [note becomeFirstResponder]; 
} 

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    NSLog(@"EDITING"); 
    if (noteToolbar == nil) { 
     NSLog(@"Creating toolbar"); 

     noteToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 44.0)]; 
     noteToolbar.barStyle = UIBarStyleBlackTranslucent; 

     UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNote:)]; 

     UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(clearNote:)]; 

     UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

     UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveNote:)]; 

     noteToolbar.items = [NSArray arrayWithObjects:cancelButton, clearButton, extraSpace, doneButton, nil]; 
     NSLog(@"Items: %@", noteToolbar.items); 
    } 

    NSLog(@"Current field: %@",textView); 
    NSLog(@"keyboard: %@", noteToolbar); 
    textView.inputAccessoryView = noteToolbar; 
} 

日志

2012-04-11 17:31:00.148 MyApp[28892:fb03] EDITING 
2012-04-11 17:31:00.148 MyApp[28892:fb03] Creating toolbar 
2012-04-11 17:31:00.149 MyApp[28892:fb03] Items: (
    "<UIBarButtonItem: 0x10dc3420>", 
    "<UIBarButtonItem: 0x10dc3480>", 
    "<UIBarButtonItem: 0x10dc34e0>", 
    "<UIBarButtonItem: 0x10dc3540>" 
) 
2012-04-11 17:31:00.149 MyApp[28892:fb03] Current field: <UITextView: 0x10d94f40; frame = (5 5; 310 140); text = 'This is sample text'; clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x10d95170>; contentOffset: {0, 0}> 
2012-04-11 17:31:00.150 MyApp[28892:fb03] keyboard: <UIToolbar: 0x10dc2820; frame = (0 0; 320 44); opaque = NO; layer = <CALayer: 0x10dc2730>> 
+1

尝试在'viewDidLoad'方法中设置这些内容 – Legolas 2012-04-12 01:33:20

+0

@Legolas我尝试了一些这段代码的位置。我做了视图加载,keyboardWillAppear和textViewDidBeginEditing和textViewShouldBeginEditing。 – Bot 2012-04-12 15:20:10

+0

@QwertyBob你们不理解这个问题......它可以像我之前在其他应用中使用过的那样完成。工具栏就像苹果公司在其键盘上一样放置在键盘上方。 – Bot 2012-04-12 15:32:50

回答

3

尝试在viewDidLoad方法设置这些!

+0

澄清任何人有此问题。当我把它放在视图中时,它可能不适用于我,因为我可能没有'textview.delegate = self;'set。事实上它可能已被注释掉。 – Bot 2012-04-12 18:49:41

0

你不能做到这一点。键盘是一个系统控制的UIView,你不能直接控制。操作系统将在所有其他视图之上放置键盘视图,以确保它具有正确的焦点来收集用户输入。

你能提供更多的背景知道你为什么要这样做吗?即使你确实找出了使观点按照你所描述的方式行事的方式,它几乎肯定会被苹果拒绝违反人机界面指南。

+0

呵呵?我认为你不了解这个问题。我试图让一个工具栏显示在键盘上方(而不是前面),一个完成按钮和几个其他按钮。就像苹果在键盘上一样。它可以像我之前看到的那样完成。 – Bot 2012-04-12 15:18:58

+0

我想你们应该使用我以上发布的用户方法。 – 2012-04-13 08:15:35

3

//使用这个方法

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{ 

    textView.inputAccessoryView = yourToolBar; 

    return YES; 
}