Recientemente he estado haciendo preguntas relacionadas con UIToolbars y lo que no, pero ahora descubro que necesito agregarle elementos programáticamente, he visto los métodos de otras personas sobre cómo hacerlo. pero cuando trato de hacer lo mismo, nada termina apareciendo. Identificar este problema es en lo que necesito ayuda. Aquí están mis conexiones en IB:Agregar elementos a UIToolbar Programaticamente no funciona
Y aquí está el código correspondiente:
Archivo de cabecera: archivo
#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
Implementación:
- (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];
}
Cualquier ayuda es ¡apreciado enormemente!
EDIT:
He quitado todo lo que tenía en la punta lo que causaría que aparezca la barra de herramientas/ser modificado y actualizado mi código en viewDidLoad a lo siguiente:
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]];
y termino obteniendo una barra de herramientas en blanco apareciendo. Arranco el depurador y esto es lo que veo: Aha! El campo _text de la vista lastUpdateLabel está fuera del alcance. ¿Pero por qué? ¿Y cómo remediaría esto? ¡Gracias por adelantado!
EDIT 2:
he sido capaz de añadir etiquetas y un NSActivityIndicator con el siguiente código:
@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];
Pero cuando intento añadir un UIBarButtonItem no tengo suerte (no se muestra en la barra de herramientas):
self.refreshDataButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:@selector(refreshDataButtonTapped)];
[self setToolbarItems:[NSArray arrayWithObject:refreshDataButton]];
Aquí está el archivo de cabecera:
#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
¿Alguna idea? ¡Gracias!
hizo lo que usted sugiere, sin suerte. Acabo de ver una simple barra de herramientas gris. Lanzando en esta línea de código: toolbar.barStyle = UIBarStyleBlackOpaque; en realidad hace que la barra de herramientas se ponga negra, por lo que las conexiones no se estropean ni nada ... Gracias por la respuesta. – Stunner