1

注意:我可以通过在我的 barbutton 和 barbutton2 方法中将 leftbarbuttonitem 切换为 rightbarbutton 项目来实现这一点。我只是想知道为什么它不能正常工作!

出于某种奇怪的原因,我的 leftbarbuttonitem 正在显示,但我的 rightbarbuttonitem 永远不会弹出!这是我的代码

    -(void)barButton {
    image = [UIImage imageNamed:@"BBut.png"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
    [button setBackgroundImage: [[UIImage imageNamed: @"BBut.png"] stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateHighlighted];

    [button addTarget:self action:@selector(showlocations:) forControlEvents:UIControlEventTouchUpInside];
    button.frame= CGRectMake(3.0, 0.0, image.size.width+1, image.size.height+0);

    UIView *v=[[UIView alloc] initWithFrame:CGRectMake(3.0, 0.0, image.size.width+1, image.size.height) ];

    [v addSubview:button];

    UIBarButtonItem *modal = [[UIBarButtonItem alloc] initWithCustomView:v];
    self.navigation.leftBarButtonItem = modal;


-(void)barButton2 {
    image2 = [UIImage imageNamed:@"Refresh.png"];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setBackgroundImage: [image2 stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
    [button2 setBackgroundImage: [[UIImage imageNamed: @"Refresh.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateHighlighted];
        // [button2 addTarget:self action:@selector(viewDidLoad) forControlEvents:UIControlEventTouchUpInside];
    button2.frame= CGRectMake(281.0, 5.0, image2.size.width, image2.size.height);

    UIView *v2=[[UIView alloc] initWithFrame:CGRectMake(281.0, 5.0, image2.size.width, image2.size.height) ];

    [v2 addSubview:button2];
        UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithCustomView:v2];    self.navigation.rightBarButtonItem = refresh;
    NSLog(@"THE ACTION HAS BEEN RECIEVED!"); }

在我的 ViewDidLoad 方法中,我有这个:

[self barButton2];
[self barButton];

该操作确实通过了,并且我确实收到了我的 NSLog 方法。有谁知道为什么会这样?左栏按钮始终显示,如果我将第一个 barbutton 更改为 rightbarbuttonitem 并将 barbutton2 更改为 leftbarbutton 项目,然后更改 CGRect 坐标,它就可以工作,这就是我现在用来让它工作的东西,但为什么会这样?提前致谢。

4

2 回答 2

2

您看不到正确的按钮,因为您在屏幕外绘制它。

X 坐标设置为 281.0 ,button2其父视图 X 坐标也设置为 281.0。因此,您的按钮位于 x = 562 中,这意味着它位于屏幕之外。

将您的button2X 坐标设置为 0,看看是否有帮助:

button2.frame= CGRectMake(0.0, 5.0, image2.size.width, image2.size.height);
于 2012-07-06T23:29:12.927 回答
0

您似乎正在为每个操作创建一个新视图并添加一个按钮。所以,你永远不会看到这两个按钮。您将需要在两个操作中访问相同的视图,以确保添加两个按钮子视图。

于 2012-07-06T23:14:01.230 回答