iOS - UIToolbar not working in h file
I am new to developing iOS apps. I am currently making an app which changes nib files.
However, after creating a new UIViewController subclass, with a XIB user interface, I cannot get the UIToolbar IBOutlet to work.
It may sound silly but the "UIToolbar" dosen't go pink/purple in the header file (.h):
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
Nor does the "_toolbar", go blue/green, in the .m:
@synthesize toolbar = _toolbar;
This is causing me issue to use the UIToolbar.
Here is my code for the .h:
#import <UIKit/UIKit.h>
@interface pedestrians : UIViewController;
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
@end
And for the .m:
#import "pedestrians.h"
@implementation pedestrians
@synthesize toolbar = _toolbar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDi开发者_如何学GodLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
Any help would be great. Thanks.
EDIT:
Just tried to add a UIBar Button like this (not picking up any UI stuff):
The code you've written created two things relevant to your toolbar: an instance variable named _toolbar (coming from the synthesize statement), and the accessors at self.toolbar. You're trying to use an instance variable named "toolbar" when no such thing exists. Either use _toolbar, or use self.toolbar. Preferably the latter, as it preserves key-value observing functionality and allows for better inheritance behaviors.
There is no instance variable named toolbar
in that file. Using @property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
does not create an instance variable. You need to access self.toolbar
or, preferably, _toolbar
in your viewDidLoad.
I don't think you can end an @interface line with semi-colon. Can you change your .h file code like below
@interface pedestrians : UIViewController {
}
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
@end
精彩评论