2012-07-06 151 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