2013-03-21 96 views
0

我有兩個導航按鈕(左和右)和兩個類別按鈕(例如loveCategory和otherCategory)。當我選擇loveCategory時,我希望我的導航按鈕僅在loveCategory中顯示(在我的情況下爲Images),當我按otherCategory時,導航按鈕應該只能從otherCategory顯示(圖像)(即當我按左右按鈕)。UIButton做不同的動作ios

讓我知道如果這不明確。

這裏是我的代碼:

-(IBAction)NavigationBtnTapped:(UIButton*)sender { 
UIButton *button=sender; 
switch (button.tag) { 
    case 1: 
     //statements 
     break; 
    case 2: 
     //statements 
     break; 
    default: 
     break; 
} 

} 

-(IBAction)loveCategory { 
} 
-(IBAction)otherCategory { 
} 

我如何調用一個動作與另一動作中

-(IBAction)loveCategory 
{ 
    -(IBAction)NavigationBtnTapped:(id)sender 
{ 
// Is it possible to call an action within another action? 
} 
} 
+0

@chirag - 拼寫錯誤?大聲笑 – Vikr 2013-03-21 12:47:25

+0

我沒有得到你完全請解釋更多。你是什​​麼意思'只從loveCategory'和'只從otherCategory'。你想爲不同類別的不同導航按鈕嗎? – 2013-03-21 12:47:33

+0

仍然接受:) – 2013-03-21 12:48:16

回答

1

給標籤1和2的按鈕,並嘗試這個辦法:

-(IBAction)NavigationBtnTapped:(id)sender { 

    switch ([sender tag]) { 
     case 1: 
      //statements 
      break; 
     case 2: 
      //statements 
      break; 
     default: 
      break; 
    } 

} 
+0

我已經這樣做了..我的問題是我如何使用NavigationBtnTapped行動來執行其他操作?我真的想在另一個方法中調用這個NavigationBtnTapped方法 - (IBAction)loveCategory – Vikr 2013-03-21 12:49:46

+0

根據點擊哪個按鈕調用相關的方法。有什麼問題 ? – Petar 2013-03-21 12:53:35

+0

我該如何調用 - (IBAction)loveCategory { - (IBAction)NavigationBtnTapped:(id)sender //是否可以在另一個操作中調用一個動作? } – Vikr 2013-03-21 13:00:02

0

AnOther Way is ....

給予標籤均爲UIButton

諸如此類

button1.tag = 101; 
button2.tag = 102; 

兩個UIButton具有相同的方法調用。
諸如此類,

-(void)buttonTapped:(UIButton *)sender 
{ 
    if(sender.tag == 101) 
    { 
    // code for loveCategory 
    } 
    if(sender.tag == 102) 
    { 
    // code for otherCategory 
    } 
} 
0

您可以通過設置在最後做這個

int selectedCategory = 0; 

-(IBAction)NavigationBtnTapped:(UIButton*)sender { 
UIButton *button=sender; 
switch (button.tag) { 
    case 1: 
     //statements 
     if(selectedCategory == 0){ 
      // go left for lovecategory 
     }else{ 
      // go left for OtherCategory 
     } 
     break; 
    case 2: 
     //statements 
     if(selectedCategory == 0){ 
      // go right for lovecategory 
     }else{ 
      // go right for OtherCategory 
     } 
     break; 
    default: 
     break; 
} 

} 

-(IBAction)loveCategory { 
    selectedCategory = 0; 
} 
-(IBAction)otherCategory { 
    selectedCategory=1; 
} 

選擇哪一類標誌做,這是你想要的嗎?

對於另一個動作中調用動作

-(IBAction)loveCategory 
{ 
    [self NavigationBtnTapped:null]; 
} 
-(IBAction)NavigationBtnTapped:(id)sender 
{ 
// Is it possible to call an action within another action? 
} 
+0

我想愛情類中調用NavigationBtnTapped像下面 - (IBAction爲)loveCategory { - (IBAction爲)NavigationBtnTapped:(ID)發送 {// 是否可以調用另一個行動中的作用? (IBAction)loveNavigationBtnTapped:(UIButton *)發件人,並試圖在(-IBAction)內調用它(loveCategory {[code] } – Vikr 2013-03-21 13:06:04

+0

@Vikr我編輯了我的答案 – 2013-03-21 14:56:38

+0

它似乎沒有工作。 self loveNavigationBtnTapped:null]; }和它的不工作.. – Vikr 2013-03-22 05:10:18

0

另一種方式是有2個UIButtons和兩個UIImages但它不是這樣做的好方法:d

一開始loveCategory的按鈕隱藏= NO,它的圖像隱藏= YES,otherCategory的按鈕= YES,其圖像隱藏= NO。

當你點擊loveCategory的按鈕,loveCategory的按鈕隱藏= YES和圖像隱藏= NO,也爲otherCategory的按鈕隱藏= NO和圖像隱藏= YES

0

發現了答案!

在頭文件

BOOL isLove,isOther; 

在FPGA實現文件

- (void)viewDidLoad 
{ 
    isOther=TRUE; 
    isLove=FALSE; 
} 

-(IBAction)NavigationBtnTapped:(UIButton*)sender { 
if (isLove) { 
// Statement 
} 
else 
{ 
//statement 
} 

如果需要使用 「否則,如果」 檢查多個按鈕。