2011-06-16 207 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

相關問題