Vikr
Vikr

Reputation: 121

UIButton doing different action ios

I have two navigation buttons(left and right) and two category buttons(for ex. loveCategory and otherCategory). When I select loveCategory I want my navigation buttons to display(in my case Images) only from loveCategory and when I press otherCategory the navigation buttons should be able to display(images) only from otherCategory(that is when I press the left and right button).

Let me know if this is not clear.

here is my code :

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

}

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

How do i call an action within another action

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

Upvotes: 0

Views: 762

Answers (5)

Vikr
Vikr

Reputation: 121

Found out the answer !

in header file

BOOL isLove,isOther;

in implemention file

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

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

If necessary use "else if" to check for multiple buttons.

Upvotes: 0

Chirag Patel
Chirag Patel

Reputation: 11508

You can do this by setting flag that which category is selected at last by doing this

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;
}

Is that what you want ?

For calling action within another action

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

Upvotes: 0

Rudi
Rudi

Reputation: 4314

another way for that is to have 2 UIButtons and two UIImages however it's not a good way for doing this :D

In the beginning loveCategory's button hidden = NO and it's image hidden = YES, also otherCategory's button = YES and its image is hidden = NO.

When you click on loveCategory's button , loveCategory's button hidden = YES and its image hidden = NO and also for otherCategory's button hidden = NO and image hidden = YES

Upvotes: 0

iPatel
iPatel

Reputation: 47119

AnOther Way is ....

Give tag of both UIButton.

such like

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

Both UIButton have same method call.
such like,

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

Upvotes: 0

Shah Paneri
Shah Paneri

Reputation: 739

Give tag 1 and 2 to your button and try this:

    -(IBAction)NavigationBtnTapped:(id)sender {

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

}

Upvotes: 1

Related Questions