2013-09-23 15 views
1

我已經看了很多文章來幫助我,但未能找到答案......更換右側的UIBarButtonItem與「完成」按鈕

我想,當鍵盤來代替我的右邊欄按鈕是一個「完成」按鈕激活,然後在編輯完成後將其重新放回...

我已經有一個正確的欄按鈕,它是一個分段控件,並在視圖加載時設置。我不確定一旦它創建後是否可以切換欄按鈕?

下面是一些屏幕截圖...

Would like to change the Prev/Next Button to a done button when keyboard appears

想向後退/前進按鈕更改爲完成按鈕時,鍵盤會出現

Screen with Keyboard

以下是我的代碼觸發鍵盤顯示時...代碼執行但按鈕不顯示...

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView 
{ 
// Replace the Right Navigation Button with done button 

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                      target:self 
                      action:@selector(doneEditing)]; 
self.navigationItem.rightBarButtonItem = doneButton; 

// Slide up the view 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.3]; 

CGRect rect = self.view.frame; 

// Move the view .... Note need to calculate this based on the size of the keyboard !! 

rect.origin.y -= 120.0f; 
rect.size.height += 120.f; 

self.view.frame = rect; 

// Resize the text view .... Note need to calculate this based on screen size !! 

CGSize frameSize = self.reviewTextView.frame.size; 
CGPoint framePos = self.reviewTextView.frame.origin; 
[self.reviewTextView setFrame:CGRectMake(framePos.x, framePos.y, frameSize.width, 200.0f)]; 

[UIView commitAnimations]; 

return YES; 
} 

以下是當視圖被加載

//set up the segmented control and add it to the nav bar rightBartButtonItem 

UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"\U000025C0\U0000FE0E Prev", @"Next \U000025B6\U0000FE0E", nil]]; 
UIBarButtonItem * segmentControlButton = [[UIBarButtonItem alloc] initWithCustomView:segmentControl]; 
[segmentControl setBackgroundColor:[UIColor clearColor]]; 
segmentControl.segmentedControlStyle = UISegmentedControlStyleBar; 
segmentControl.frame = CGRectMake(0, 0, 120, 30); 
[segmentControl setMomentary:YES]; 
[segmentControl addTarget:self 
        action:@selector(onSegmentChanged:) 
     forControlEvents:UIControlEventValueChanged]; 
self.navigationItem.rightBarButtonItem = segmentControlButton; 
+0

您是如何在第一個地方設置prev/next按鈕的? – micantox

+0

添加上面的代碼以顯示在加載視圖時如何設置上一個/下一個按鈕。 – rs2000

+0

然後我不明白。我無法重現該問題。使用完全相同的代碼,它一切正常。問題必須放在其他地方.. – micantox

回答

0

試試這個設置上/下一個按鈕的代碼段,

if(keyboardIsActive){ 
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] 
            initWithTitle:@"Done" 
            style:UIBarButtonItemStyleBordered 
            target:self.revealViewController 
            action:@selector(revealToggle:)]; 

    destinationViewController.navigationItem.leftBarButtonItem = menuButton;} 
1

有時你需要提示系統重繪與變化的酒吧,請嘗試撥打[self.navigationController.navigationBar setNeedsDisplay]

0

我很抱歉,但是這是我的一個編碼錯誤...

從用戶MICANTOX支持您觸發一個思想,我有正確的代碼,但在錯誤的地方!由於這是一個子視圖,我沒有在正確的位置執行代碼....代碼現在已經放在父視圖中