我目前在SearchCategoryChooserViewController
中有两个按钮。我想要做的是根据按下的按钮将chosenCategory
属性设置为某个值,然后将该值发送到CriteriaViewController
。
我在categoryButtonClick
函数中注释了一些伪代码,但我不确定如何格式化语法,以及从哪里获取它。 topCategoryId1
和topCategoryId2
值来自SearchViewController
。如果您希望我包含该类或任何其他类的代码,请告诉我。
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
答案 0 :(得分:1)
你可以这样做:
- (IBAction)categoryButtonClick:(id)sender
{
if ([topCategory1 isSelected]) {
set chosenCategory = self.topCategoryId1
}
else if ([topCategory2 isSelected]) {
set chosenCategory = self.topCategoryId2;
}
}
答案 1 :(得分:1)
一种可能的解决方案是使用标签。创建后,将每个按钮设置为自己的标记,例如:
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];
然后在你的按钮操作方法(categoryButtonClick :)中,检查按钮的标签,如下所示:
- (IBAction)categoryButtonClick:(id)sender
{ UIButton *button = (UIButton *)sender;
if(button.tag == 1){
chosenCategory = self.topCategoryId1;
}else{
chosenCategory = self.topCategoryId2;
}
}
答案 2 :(得分:1)
根据@ croberth的回答,在有条件地设置chosenCategory
后,发送performSegueWithIdentifier:sender:
消息:
- (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
是按钮和CriteriaViewController
之间segue的标识符。由于您的按钮不在故事板中,因此它应该是两个控制器之间的一个区域,而不是任何按钮。
答案 3 :(得分:0)
你所问的内容已经多次回答过了。您在prepareForSegue方法中的发送VC中设置变量,并准备一个变量以在接收VC中接收该值。这是一个包含更多细节的SO链接:ios pass values during a segue to another view