2012-05-18 76 views
2

後,我創造了我下面的方法底部工具欄的一些自定義UIBarButtonItems:的iPad - 的UIBarButtonItem消失隱藏UIToolbar

- (void)initialisePageNoProperties 
{ 
    pageNoTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 50, 30)]; 
    [pageNoTextField setDelegate:self]; 
    pageNoTextField.text = @"0";    
    pageNoTextField.textColor = [UIColor blackColor]; 
    pageNoTextField.backgroundColor = [UIColor whiteColor]; 
    pageNoTextField.textAlignment = UITextAlignmentRight; 
    pageNoTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    pageNoTextField.borderStyle = UITextBorderStyleRoundedRect; 
    pageNoTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support 
    pageNoTextField.keyboardType = UIKeyboardTypeNumberPad; 
    pageNoTextField.returnKeyType = UIReturnKeyGo; 
    [pageNoTextField setClearsOnBeginEditing:YES]; 

    pageNoBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:pageNoTextField]; 
    pageNoBarButtonItem.style = UIBarButtonItemStyleBordered; 

    noOfPagesTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 50, 30)]; 
    noOfPagesTextField.text = @"0"; 
    noOfPagesTextField.textColor = [UIColor blackColor]; 
    noOfPagesTextField.backgroundColor = [UIColor clearColor]; 
    noOfPagesTextField.textAlignment = UITextAlignmentLeft; 
    noOfPagesTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    noOfPagesTextField.enabled = NO; 
    noOfPagesBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:noOfPagesTextField]; 
} 

這些按鈕然後被添加到了底部工具欄下面的方法:

- (void)configurePageNoDisplay 
{ 
    if(![self.navigationController isToolbarHidden]) 
    { 
     NSMutableArray *items = [[self.navigationController.toolbar items] mutableCopy]; 
     bool insertIntoArray = ([items count] == 10); // without the page number display the items array will contain 10 items 

     if (insertIntoArray) 
     { 
      [items insertObject:pageNoBarButtonItem atIndex:3]; 
     } 
     else 
     { 
      [items replaceObjectAtIndex:3 withObject:pageNoBarButtonItem]; 
     } 

     if (insertIntoArray) 
     { 
      [items insertObject:noOfPagesBarButtonItem atIndex:4]; 
     } 
     else 
     { 
      [items replaceObjectAtIndex:4 withObject:noOfPagesBarButtonItem]; 
     } 

     [self.navigationController.toolbar setItems:items]; 
     [self SetPageNoDisplay:[pdfViewCtrl GetCurrentPage]]; 
    } 
} 

,併爲這些按鈕的值被置如下:

- (void)SetPageNoDisplay:(NSInteger) pageNumber 
{ 
    pageNoTextField.text = [NSString stringWithFormat:@"%d", pageNumber]; 
    noOfPagesTextField.text = [NSString stringWithFormat:@"of %d", [[pdfViewCtrl GetDoc] GetPageCount]]; 
} 

個按鈕和它們所包含的字段聲明如下:

@property (strong, nonatomic) IBOutlet UIBarButtonItem *pageNoBarButtonItem; 
@property (strong, nonatomic) IBOutlet UIBarButtonItem *noOfPagesBarButtonItem; 
@property (strong, nonatomic) IBOutlet UITextField *pageNoTextField; 
@property (strong, nonatomic) IBOutlet UITextField *noOfPagesTextField; 

最初他們沒有被聲明爲IBOutlets - 但我讀了建議,這樣做將有助於我的問題 - 遺憾的是它並沒有幫助。

我隱藏/顯示響應底部工具欄從用戶雙擊:

- (void)handleDoubleTap:(UITapGestureRecognizer *)gestureRecognizer 
{ 
    [self.navigationController setNavigationBarHidden:![self.navigationController isNavigationBarHidden] animated:true]; 
    [self.navigationController setToolbarHidden:![self.navigationController isToolbarHidden] animated:true]; 
    [self configurePageNoDisplay]; 
    //[sideBarTab setHidden:![sideBarTab isHidden]]; 
} 

我的問題是,一旦工具欄已隱藏 - 按鈕並不時再次出現重新顯示。如果我在重新顯示工具欄後旋轉iPad,則會再次出現按鈕。

回答

0

我最終通過採取不同的方法解決了這個問題。我使用了標準的UIToolbar,而不是導航控制器工具欄。使用界面生成器我能夠添加一個普通的UIView到工具欄,然後在UIView中添加2個UITextFields。這樣我就不必擔心所有代碼將文本字段添加到條形按鈕項目的工具欄中 - 或者調用代碼以恢復方向更改,隱藏和顯示工具欄等文本字段。最終,是一種更簡單,更穩健的方法。

0

在您的UIViewController中使用setToolbarItems方法來設置工具欄按鈕,並且應該解決它。