0

我用 UIBarButtonItems 创建了一个 UIToolBar,但是当我点击它们时,没有调用选择器。任何帮助将不胜感激。

更新

根据评论中的建议,我修改了我的代码,但不幸的是,按钮仍然不起作用。

-(void)buildBottomMenu
{
    UIImage *newsImg = [UIImage imageNamed:@"243-globe"];
    UIBarButtonItem *newsBtnItem = [[UIBarButtonItem alloc] initWithImage:newsImg style:UIBarButtonItemStylePlain target:self action:@selector(loadHome)];

    UIImage *weatherImg = [UIImage imageNamed:@"25-weather"];
    UIBarButtonItem *weatherBtnItem = [[UIBarButtonItem alloc] initWithImage:weatherImg style:UIBarButtonItemStylePlain target:self action:@selector(loadWeather)];

    UIImage *sportsImg = [UIImage imageNamed:@"63-runner"];
    UIBarButtonItem *sportsBtnItem = [[UIBarButtonItem alloc] initWithImage:sportsImg style:UIBarButtonItemStylePlain target:self action:@selector(loadSports)];

    UIImage *entrnImg = [UIImage imageNamed:@"178-city"];
    UIBarButtonItem *entrnBtnItem = [[UIBarButtonItem alloc] initWithImage:entrnImg style:UIBarButtonItemStylePlain target:self action:@selector(loadEntertainment)];

    UIImage *videoImg = [UIImage imageNamed:@"69-display"];
    UIBarButtonItem *videoBtnItem = [[UIBarButtonItem alloc] initWithImage:videoImg style:UIBarButtonItemStylePlain target:self action:@selector(loadVideoPage)];

    UIImage *moreImg = [UIImage imageNamed:@"259-list"];
    UIBarButtonItem *moreBtnItem = [[UIBarButtonItem alloc] initWithImage:moreImg style:UIBarButtonItemStylePlain target:self action:@selector(loadOptionsPage)];

    UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                              target:nil action:nil];

    [_bottomBar setItems:[NSArray arrayWithObjects:flexibleSpace,newsBtnItem,flexibleSpace,
                      weatherBtnItem,flexibleSpace,
                      sportsBtnItem,flexibleSpace,
                      entrnBtnItem,flexibleSpace,
                      videoBtnItem,flexibleSpace,
                      moreBtnItem,flexibleSpace,
                      nil]];

    [self.view addSubview:_bottomBar];
}

这是一个示例选择器:

- (void)loadWeather
{
    NSLog(@"Loading weather");
}
4

2 回答 2

0

感谢您的帮助,但我刚刚意识到这是我在 viewDidLayoutSubviews 方法中所做的事情导致了问题。

于 2013-06-17T04:21:14.267 回答
0

尝试这个,

-->首先将 UIToolbar 拖放到您的 ViewController.xib

-->connect IBOutlet 连接

视图控制器.h

IBOutlet UIToolbar *_bottomBar;

视图控制器.m

- (void)viewDidLoad
{
[super viewDidLoad];
[self buildBottomMenu];
// Do any additional setup after loading the view, typically from a nib.
}


-(void)buildBottomMenu
{
UIImage *newsImg = [UIImage imageNamed:@"243-globe"];
UIBarButtonItem *newsBtnItem = [[UIBarButtonItem alloc] initWithImage:newsImg style:UIBarButtonItemStylePlain target:self action:@selector(loadHome)];

UIImage *weatherImg = [UIImage imageNamed:@"25-weather"];
UIBarButtonItem *weatherBtnItem = [[UIBarButtonItem alloc] initWithImage:weatherImg style:UIBarButtonItemStylePlain target:self action:@selector(loadWeather)];

UIImage *sportsImg = [UIImage imageNamed:@"63-runner"];
UIBarButtonItem *sportsBtnItem = [[UIBarButtonItem alloc] initWithImage:sportsImg style:UIBarButtonItemStylePlain target:self action:@selector(loadSports)];

UIImage *entrnImg = [UIImage imageNamed:@"178-city"];
UIBarButtonItem *entrnBtnItem = [[UIBarButtonItem alloc] initWithImage:entrnImg style:UIBarButtonItemStylePlain target:self action:@selector(loadEntertainment)];

UIImage *videoImg = [UIImage imageNamed:@"69-display"];
UIBarButtonItem *videoBtnItem = [[UIBarButtonItem alloc] initWithImage:videoImg style:UIBarButtonItemStylePlain target:self action:@selector(loadVideoPage)];

UIImage *moreImg = [UIImage imageNamed:@"259-list"];
UIBarButtonItem *moreBtnItem = [[UIBarButtonItem alloc] initWithImage:moreImg style:UIBarButtonItemStylePlain target:self action:@selector(loadOptionsPage)];

UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:nil action:nil];

[_bottomBar setItems:[NSArray arrayWithObjects:flexibleSpace,newsBtnItem,flexibleSpace,
                  weatherBtnItem,flexibleSpace,
                  sportsBtnItem,flexibleSpace,
                  entrnBtnItem,flexibleSpace,
                  videoBtnItem,flexibleSpace,
                  moreBtnItem,flexibleSpace,
                  nil]];

[self.view addSubview:_bottomBar];
 }



 - (void)loadWeather
 {
  NSLog(@"Loading weather");
 }
于 2013-06-17T05:00:28.333 回答