2012-06-13 126 views
-1

enter image description here自定义UIToolbar按钮不起作用

在我的xib文件中有一个栏按钮项。

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

,我和按钮文件的所有者连接,但它不能正常工作,请帮助我。

非常感谢!

回答

0

这是正在发生的事情:

  1. 使用Interface Builder,你链接的UIBarButtonItem控制器,告诉控制器特定的UIBarButtonItem是self.toolbarButton 。

    在你的代码
  2. 然后,你告诉你的控制器,该self.toolbarButton现在是你的代码中创建另一个按钮使用

(但你没有添加到您的视图还)
[[UIBarButtonItem alloc] initWithCustomView:aboutToolbarButton]; 

现在self.toolbarButton不再是界面生成器中的按钮。

因此,您在视图中看到的按钮在您认为“无法使用”,因为您尚未将任何选择器分配给该按钮。

我会建议你使用接口生成器将IBAction链接到按钮。

说了这么多,似乎你不明白界面构建器是如何工作的。我建议你阅读更多的教程,甚至更好,得到这本书由Apress: http://www.apress.com/9781430236054

1

什么不工作?

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

y = 100?工具栏都只有44像素的高...

+0

谢谢......我是这样一个白痴,但我改变它(0.0,0.0,60.0,30.0),它不'工作。 – jxdwinter