2012-07-06 109 views
1

注:我可以通过在我的barbutton和barbutton2方法中将leftbarbuttonitem切换到rightbarbutton项目来实现此目的。我只是想知道为什么它不能正常工作!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坐标系,它就可以工作,这就是我现在正在使用它来使其工作,但为什么会发生这种情况?提前致谢。

回答

2

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

button2 X坐标设置为281.0,其父视图X坐标也设置为281.0。结果你的按钮在x = 562,这意味着它在屏幕之外。

设置你的button2 X坐标为0,并看看是否有帮助:

button2.frame= CGRectMake(0.0, 5.0, image2.size.width, image2.size.height); 
+0

谢谢!就是这样!我不知道为什么我明白它会在x = 281的原始视图中。这让我有点疯狂!再次感谢你! – sridvijay 2012-07-06 23:33:04

0

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

+0

视图的在虽然不同的位置,为什么我不能够看到两种观点在一次? – sridvijay 2012-07-06 23:18:36