0

I currently have two buttons in my SearchCategoryChooserViewController. What I want to do is to set the chosenCategory property to a certain value depending on which button is pressed, and then send that value over to CriteriaViewController.

I have some psuedo code commented out in my categoryButtonClick function, but I'm not sure how to format the syntax, and where to take it from there. The topCategoryId1 and topCategoryId2 values are coming from SearchViewController. Let me know if you want me to include code from that or any other classes.

SearchCategoryChooserViewController.m:

#import "SearchCategoryChooserViewController.h"
#import "SearchViewController.h"
#import "CriteriaViewController.h"

@interface SearchCategoryChooserViewController ()

@end

@implementation SearchCategoryChooserViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];




    UIButton *category1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    category1.frame = CGRectMake(10, 120, 300, 35);
    [category1 setTitle: [NSString stringWithFormat:@"%@", self.topCategory1] forState:UIControlStateNormal];
    [category1 addTarget:self action:@selector(categoryButtonClick:)    forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: category1];


    UIButton *category2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    category2.frame = CGRectMake(10, 180, 300, 35);
    [category2 setTitle: [NSString stringWithFormat:@"%@", self.topCategory2] forState:UIControlStateNormal];
    [category2 addTarget:self action:@selector(categoryButtonClick:)    forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: category2];


}




- (IBAction)categoryButtonClick:(id)sender
{
//    if (topCategory1 button is pressed) {
//        set chosenCategory = self.topCategoryId1
//    }
//    
//    else if (topCategory2 button is pressed) {
//        set chosenCategory = self.topCategoryId2
//    }

}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// Send the Category Id over to CriteriaViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

 CriteriaViewController *controller = (CriteriaViewController *) segue.destinationViewController;

 // Send over the search query as well as the specific category to CriteriaVC to use
 controller.chosenCategory = self.topCategoryId1;



}


@end
Ghobs
  • 839
  • 2
  • 14
  • 32
  • possible duplicate of [ios pass values during a segue to another view](http://stackoverflow.com/questions/9907684/ios-pass-values-during-a-segue-to-another-view) – Duncan C May 08 '14 at 19:53

4 Answers4

1

You could do:

- (IBAction)categoryButtonClick:(id)sender
{
    if ([topCategory1 isSelected]) {
        set chosenCategory = self.topCategoryId1
    }

    else if ([topCategory2 isSelected]) {
        set chosenCategory = self.topCategoryId2;
    }

}
hungrxyz
  • 745
  • 11
  • 20
1

One possible solution would be to use tags. Set each button to have its own tag after creating it, for example:

UIButton *category1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
category1.frame = CGRectMake(10, 120, 300, 35);
[category1 setTitle: [NSString stringWithFormat:@"%@", self.topCategory1] forState:UIControlStateNormal];
[category1 addTarget:self action:@selector(categoryButtonClick:)    forControlEvents:UIControlEventTouchUpInside];
category1.tag = 1;  //added tag to button
[self.view addSubview: category1];


UIButton *category2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
category2.frame = CGRectMake(10, 180, 300, 35);
[category2 setTitle: [NSString stringWithFormat:@"%@", self.topCategory2] forState:UIControlStateNormal];
[category2 addTarget:self action:@selector(categoryButtonClick:)    forControlEvents:UIControlEventTouchUpInside];
category2.tag = 2;  //added tag to button
[self.view addSubview: category2];

Then in your button action method (categoryButtonClick:), check the button's tag like so:

- (IBAction)categoryButtonClick:(id)sender
{ UIButton *button = (UIButton *)sender;
  if(button.tag == 1){
        chosenCategory = self.topCategoryId1;
   }else{
        chosenCategory = self.topCategoryId2;
   }

}
chetem
  • 502
  • 3
  • 10
  • This all makes sense, however this line: `chosenCategory = self.topCategoryId1; }else{ chosenCategory = self.topCategoryId2;` is giving me the error `use of undeclared identifier chosenCategory`. Where should I be declaring it? – Ghobs May 08 '14 at 18:55
  • I was simply going off of your psuedo code. What is `chosenCategory` supposed to be? – chetem May 08 '14 at 18:59
  • You have said that `chosenCategory` is a property. It isn't? – thxou May 08 '14 at 19:00
  • Yes `chosenCategory` is an NSNumber property that I'll be sending over to `CriteriaViewController`. – Ghobs May 08 '14 at 19:04
  • Inserting this: `@property (nonatomic, copy) NSNumber *chosenCategory;` into the header file of the `SearchViewController` that `SearchCategoryChooserViewController` imports from doesn't seem to work though. – Ghobs May 08 '14 at 19:05
  • 1
    Try this: In your header file of your SearchCategoryChooserViewController, add a property called chosenCategory and set that value in your button's action method (`self.chosenCategory = self.topCategoryId1` or `self.chosenCategory = self.topCategoryId2` respectively). Then when you prepareForSegue, set `controller.chosenCategory = self.chosenCategory` – chetem May 08 '14 at 19:19
1

In adition to the @croberth's answer, after conditionally setting chosenCategory, send a performSegueWithIdentifier:sender: message:

- (IBAction)categoryButtonClick:(id)sender
{
    UIButton *button = (UIButton *)sender;
    if(button.tag == 1) {
        chosenCategory = self.topCategoryId1;
    }
    else {
        chosenCategory = self.topCategoryId2;
    }

    [self performSegueWithIdentifier:@"YOUR_SEGUE_IDENTIFIER" sender:nil];
}

YOUR_SEGUE_IDENTIFIER is the identifier for your segue between the button and the CriteriaViewController. Since your buttons aren't in your storyboard, it should be a segue between the 2 controllers, not from any button.

thxou
  • 691
  • 7
  • 20
0

What you're asking has been answered several times already on SO. You set up a variable in the sending VC in the prepareForSegue method and prepare a variable to receive that value in the receiving VC. Here's an SO link with a lot more detail: ios pass values during a segue to another view

Community
  • 1
  • 1
GlennRay
  • 959
  • 9
  • 18