2012-04-12 78 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; 
}