The UIBarButtonItem action does not work. Why?

Thanks to everyone. I found really working code. It looks like this:

- (void)viewDidLoad {
    [super viewDidLoad];

     UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoLight];
     [infoButton addTarget:self action:@selector(settingsClick) forControlEvents:UIControlEventTouchDown];

     UIBarButtonItem* theSettingsButton =[[UIBarButtonItem alloc]initWithCustomView:infoButton];

     [self.toolbar setItems:[NSArray arrayWithObjects:theSettingsButton,nil]];

     [theSettingsButton release];
}
+3
source share
3 answers

It works:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
                                           initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered
                                           target:self action:@selector(settingsClick)] autorelease];

}

-(void)settingsClick
{    
    NSLog(@"Hello");
}

You have not closed your viewDidLoad method.

0
source
 UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
                                  initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered
                                  target:self action:@selector(settingsClick)]autorelease];
NSMutableArray * arr = [NSMutableArray arrayWithObjects:theSettingsButton, nil];
[self.toolbar setToolbarItems:arr animated:YES];

try it

0
source
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
                                       initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered
                                       target:self action:@selector(settingsClick)]autorelease];
NSArray * arr = [NSArray arrayWithObjects:theSettingsButton, nil];
[toolbar setItems:arr animated:YES];
[self.view addSubview:toolbar];
[toolbar release];
0
source

All Articles