Добавление элементов в UIToolbar не работает программно

Недавно я задавал вопросы, касающиеся UIToolbars и чего нет, но теперь я обнаружил, что мне нужно добавлять в него элементы программно, я видел методы других людей о том, как перейти о том, чтобы сделать это, но когда я пытаюсь сделать то же самое, ничего не появляется. Мне нужна помощь в том, чтобы определить эту проблему. Вот мои связи в IB:

alt text

И вот соответствующий код:

Заголовочный файл:

#import <UIKit/UIKit.h>

@interface ParkingRootViewController : UIViewController {
    UINavigationController *navigationController;
    UIToolbar *toolbar;
    UIBarButtonItem *lastUpdateLabel;
}

@property(nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *lastUpdateLabel;

- (IBAction)selectHome:(id)sender;

@end

Файл реализации:

- (void)viewDidLoad {
        [super viewDidLoad];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)];
    label.text = @"last updated...";
    label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter;
    label.font = [UIFont boldSystemFontOfSize:13.0];
    label.userInteractionEnabled = NO;

    lastUpdateLabel = [[UIBarButtonItem alloc] initWithCustomView:label];
    [label release];
    [toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];


    [self.view addSubview:self.navigationController.view];
    //[self.view addSubview:toolbar];
    //[self.navigationController.view addSubview:toolbar];

    [self.navigationController.view setFrame:self.view.frame];

}

Любая помощь приветствуется!

РЕДАКТИРОВАТЬ:

Я удалил все, что было в перо, которое могло вызвать панель инструментов должна отображаться / изменяться, и я обновил свой код в viewDidLoad следующим образом:

    self.navigationController.toolbarHidden = NO;

    //creating label in tool bar 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)];
    label.text = @"last updated...";
    label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter;
    //label.highlightedTextColor = [UIColor colorWithWhite:0.5 alpha:1.0];
    //label.highlighted = YES;
    label.font = [UIFont systemFontOfSize:13.0];
    label.userInteractionEnabled = NO;

    UIBarButtonItem *lastUpdateLabel = [[UIBarButtonItem alloc] initWithCustomView:label];
    //[lastUpdateLabel initWithCustomView:label];
    //[label release];
    //[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
    [self setToolbarItems:[NSArray arrayWithObject:lastUpdateLabel]];

И в итоге я получаю пустую панель инструментов. Я запускаю отладчик и вижу вот что: enter image description here Ага! Поле _text представления lastUpdateLabel выходит за рамки! Но почему? И как мне это исправить?

РЕДАКТИРОВАТЬ 2:

Мне удалось добавить метки и NSActivityIndicator с помощью следующего кода:

@synthesize refreshDataButton;
//...
self.navigationController.toolbarHidden = NO;

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 0.0f, 80.0f, 40.0f)];
    label.text = @"last updated...";
    label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:13.0];
    label.userInteractionEnabled = NO;
    [self.toolbar addSubview:label];

// create activity indicator
    //                        dist frm lft, dist frm top
    CGRect frame = CGRectMake(   90.0,         11.0,      25.0, 25.0);      
    UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithFrame:frame];   
    loading.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; 
    [loading sizeToFit];    
    loading.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
                                UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | 
                                UIViewAutoresizingFlexibleBottomMargin);    
    [loading startAnimating];

    [self.toolbar addSubview:loading];

Но когда я пытаюсь добавить UIBarButtonItem, мне не повезло (не отображаются на панели инструментов):

self.refreshDataButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:@selector(refreshDataButtonTapped)];
[self setToolbarItems:[NSArray arrayWithObject:refreshDataButton]];

Вот заголовочный файл:

 #import <UIKit/UIKit.h>
//#import <CoreData/CoreData.h>

@interface ParkingRootViewController : UIViewController {
    UINavigationController *navigationController;
    UIToolbar *toolbar;
    UIBarButtonItem *refreshDataButton;
    //UIActivityIndicatorView *loading;
}

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
@property (nonatomic, retain) UIBarButtonItem *refreshDataButton;
//@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *loading;


@property (nonatomic, readonly) NSString *applicationDocumentsDirectory;

-(IBAction)selectHome:(id)sender;
-(void)testCoreData;
-(void)refreshDataButtonTapped;

@end
5
задан Cœur 22 January 2019 в 12:22
поделиться