0

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 viewDidLoad];
    // 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): .m

ryryan
  • 3,890
  • 13
  • 43
  • 72

3 Answers3

1

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.

Alice Isabelle
  • 618
  • 3
  • 11
0

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.

MishieMoo
  • 6,620
  • 2
  • 25
  • 35
  • It's actually preferable to use `self.toolbar` in the code, as opposed to accessing the ivar directly. – Perception Aug 25 '11 at 20:31
  • He's not creating the ivar in the .h and he's specifically assigning _toolbar in his @synthesize which means he wants to use _toolbar to access self.toolbar. – MishieMoo Aug 25 '11 at 20:36
  • He is using modern runtime, the ivar is created for him by the compiler. – Perception Aug 25 '11 at 20:46
  • Yes, and that means he's going to use it in place of the property. From Apple Docs: "You can use the form property=ivar to indicate that a particular instance variable should be used for the property." From [The Objective-C programming Guide](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html) and lower on that page is an example of using an ivar in place of a property (in Example: Declaring Properties and Synthesizing Accessors). – MishieMoo Aug 25 '11 at 21:13
  • It doesn't at all mean that he is going to use the ivar **in the place of** the property. It means that he is going to use the ivar **for** the property. Exactly as stated by the documentation you linked. In other words if you had access to the synthesized getter/setter methods you would see the ivar you named being used, instead of whatever would have been the default variable. You still have the option of using the synthesized ivar directly, you just need to use the correct name when referencing it. – Perception Aug 25 '11 at 21:23
  • ...And that was my point in my original post. Regardless of semantics, there is no ivar named `toolbar` that he can access. If he's going to create an instance variable, he should use it, otherwise, why have it? It'll only confuse him in the long run if he leaves it in and doesn't use it. – MishieMoo Aug 25 '11 at 21:37
  • @MishieMoo let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2876/discussion-between-perception-and-mishiemoo) – Perception Aug 26 '11 at 00:30
-1

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
Saran
  • 6,274
  • 3
  • 39
  • 48