UIBarButtonItems不调用操作

时间:2013-06-17 02:34:23

标签: ios uibarbuttonitem uitoolbar

我用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");
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

试试这个,

- >首先drog并将UIToolbar拖放到ViewController.xib

- >连接IBOutlet连接

<强> ViewController.h

IBOutlet UIToolbar *_bottomBar;

<强> ViewController.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");
 }