2011-06-16 208 views
3

谢谢大家。我发现真正有效的代码。它看起来像:UIBarButtonItem操作不起作用。为什么?

- (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]; 
} 
+1

究竟什么是问题呢?代码是否编译?按钮出现了吗?按下它什么都不做?按下它会导致你的应用程序崩溃? – deanWombourne 2011-06-16 11:18:02

+0

是什么,如果你做'NSLog(@“%@”,self.toolBar)'是'无';你有没有在界面生成器中正确连线? – deanWombourne 2011-06-16 11:20:59

回答

0

这工作:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

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

} 

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

您还没有关闭您的viewDidLoad方法。

0
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]; 

试试这个

0
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

这会将按钮从屏幕底部的工具栏移动到顶部的导航栏 - 用于测试按钮的操作是否正常,但不能完全回答问题:) – deanWombourne 2011-06-16 11:20:11

相关问题