无法单击工具栏上的栏按钮?

时间:2013-02-12 05:06:32

标签: ios ipad cocoa-touch

在我的iPad应用程序中,我手动添加导航栏和工具栏 我还通过编码添加滚动视图和图像视图。

我的问题是我可以看到导航栏和工具栏 我可以点击导航栏上的按钮 但是,我无法单击工具栏上的条形按钮。

我该怎么做?

这是我的代码。

- (void)viewDidLoad
{

[super loadView];
self.view.backgroundColor = [UIColor grayColor];

UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height)];
ScrollView.pagingEnabled = YES;

NSInteger numberOfViews = 4;
for (int i = 0; i < numberOfViews; i++) {
    CGFloat xOrigin = i * self.view.frame.size.width;

    // Create a UIImage to hold Info.png
    UIImage *image1 = [UIImage imageNamed:@"Image-001.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"Image-002.jpg"];
    UIImage *image3 = [UIImage imageNamed:@"Image-003.jpg"];
    UIImage *image4 = [UIImage imageNamed:@"Image-004.jpg"];

    NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,image3,image4,nil];

    UIImageView *ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-88)];
    [ImageView setImage:[images objectAtIndex:i]];

    [ScrollView addSubview:ImageView];
}

ScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:ScrollView];
}

2 个答案:

答案 0 :(得分:0)

我不知道您的确切问题是什么,因为您显示的代码与UIToolBar无关。

但是以下代码我添加了两个UIBarButton flexSpace 您可以根据需要添加UIBarButton

UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    Toolbar = UIBarStyleBlackTranslucent;
    [Toolbar sizeToFit];

     NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];
    [flexSpace release];

    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(Cancel)];
    [barItems addObject:btnCancel];

    UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(done)];
    [barItems addObject:btnDone];

    [Toolbar setItems:barItems animated:YES];

点击按钮

时,会调用以下方法
-(void)Cancel
{
  // Write Code for Cancel Method
}

-(void)done
{
  // Write Code for Done Method
}

此代码可能对您自己的情况有所帮助。

谢谢:)

答案 1 :(得分:0)

谢谢大家。我可以解决我的问题 我改变了我的代码。

UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height)];

对此。

UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88)];