Ios iPad-隐藏UIToolbar后UIBarbuttonite消失

Ios iPad-隐藏UIToolbar后UIBarbuttonite消失,ios,ipad,uibarbuttonitem,uitoolbar,Ios,Ipad,Uibarbuttonitem,Uitoolbar,我正在使用以下方法为底部工具栏创建一些自定义UIBarButtonims: - (void)initialisePageNoProperties { pageNoTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 50, 30)]; [pageNoTextField setDelegate:self]; pageNoTextField.text = @"0";

我正在使用以下方法为底部工具栏创建一些自定义UIBarButtonims:

- (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)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;
按钮及其包含的字段声明如下:

- (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,按钮会再次出现。

我最终采用了不同的方法解决了这个问题。我使用了标准的UIToolbar,而不是导航控制器工具栏。使用Interface Builder,我可以在工具栏中添加一个普通UIView,然后在UIView中添加两个UITextFields。这样,我就不必担心将文本字段添加到工具栏中的工具栏按钮项中的所有代码,也不必调用代码来恢复方向更改时的文本字段,隐藏和显示工具栏等。它最终成为一种更简单、更健壮的方法。

我最终通过采用不同的方法解决了这个问题。我使用了标准的UIToolbar,而不是导航控制器工具栏。使用Interface Builder,我可以在工具栏中添加一个普通UIView,然后在UIView中添加两个UITextFields。这样,我就不必担心将文本字段添加到工具栏中的工具栏按钮项中的所有代码,也不必调用代码来恢复方向更改时的文本字段,隐藏和显示工具栏等。这是一种更简单、更可靠的方法。

使用UIViewController中的
setToolbarItems
方法来设置工具栏按钮,这应该可以解决问题。

使用UIViewController中的
setToolbarItems
方法来设置工具栏按钮,这应该可以解决问题解决它