Custom UIToolbar button doesn't work

There is a bar button item in my xib file.

.h:

    @property (nonatomic, retain) IBOutlet UIBarButtonItem *toolbarButton;

.m:

    UIButton *aboutToolbarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aboutToolbarButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [aboutToolbarButton setTitle:@"about" forState:UIControlStateNormal];
    aboutToolbarButton.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0f];
    [aboutToolbarButton.layer setCornerRadius:4.0f];
    [aboutToolbarButton.layer setMasksToBounds:YES];
    [aboutToolbarButton.layer setBorderWidth:1.0f];
    [aboutToolbarButton.layer setBorderColor: [[UIColor grayColor] CGColor]];
    aboutToolbarButton.frame = CGRectMake(0.0, 100.0, 60.0, 30.0);
    [aboutToolbarButton addTarget:self action:@selector(getToUserSettingsViewController) forControlEvents:UIControlEventTouchUpInside];
    self.toolbarButton = [[UIBarButtonItem alloc] initWithCustomView:aboutToolbarButton];

and I connect with the button to file's owner,but it doesn't work, please help me with this.

Thank you very much!

Answers


This is what is happening:

  1. Using interface builder, you link the UIBarButtonItem to the controller, telling the controller that that particular UIBarButtonItem is the self.toolbarButton.

  2. Then in your code, you tell your controller that the self.toolbarButton is now another button that you created by code(but you haven't added to your view yet) using the

[[UIBarButtonItem alloc] initWithCustomView:aboutToolbarButton];

Now the self.toolbarButton is no more the button in the interface builder.

Thus, the button you see in your view is in your opinion 'not working' because you haven't assigned any selector to that button.

I would recommend you to link the IBAction to the button using interface builder.

Having said all this, it seems that you don't understand how interface builder works. I suggest you read more tutorials or even better, get this book by Apress: http://www.apress.com/9781430236054


What exactly doesn´t work?

aboutToolbarButton.frame = CGRectMake(0.0, 100.0, 60.0, 30.0);

y=100? Toolbars are only 44 Pixel high...


Need Your Help

About android preferences

android arrays sharedpreferences preferences

My trouble is that when I'm trying to select an item from listpreference, the summary changes to the selected array key from array settings_listofitems_keys.

Each Player in my Java Poker Game uses their own deck

java class duplicates poker

Hello i am trying to create a game of Video Poker. This is part of what I have so far (I did not include enums, imports, variables, toStrings, etc. in order to save space):