我之前已经问过这个问题,但没有收到回复。所以我再问一遍!
我有4个这样的单选按钮:
- (IBAction)optWine:(id)sender {
ChangeLabel.text = @"Hint: try a partial phrase such as 'cab' to find information on the single-vineyard cabernet's made at the Arcadian Winery";
}
- (IBAction)optWinery:(id)sender {
ChangeLabel.text = @"Hint: try a phrase such as 'arc' to find the beautiful Arcadian Winery in Lompac, California.";
}
- (IBAction)optGrape:(id)sender {
ChangeLabel.text = @"Hint: type partial phrases such as 'zin' for Zinfandel grape";
}
- (IBAction)optReview:(id)sender {
ChangeLabel.text = @"Hint: try a partial phrase such as 'arc' to check-out reviews on wines by the Arcadian Winery";
}
...我还有其他4个动作按钮:
- (IBAction)btnSearchWineries:(id)sender {
WineriesViewController *review = [[WineriesViewController alloc] initWithNibName:nil bundle:nil];
review.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:review animated:YES completion:NULL];
}
- (IBAction)btnSearchGrapes:(id)sender {
GrapesViewController *review = [[GrapesViewController alloc] initWithNibName:nil bundle:nil];
review.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:review animated:YES completion:NULL];
}
- (IBAction)btnSearchWines:(id)sender {
WinesViewController *review = [[WinesViewController alloc] initWithNibName:nil bundle:nil];
review.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:review animated:YES completion:NULL];
}
- (IBAction)btnSearchReviews:(id)sender {
ReviewsViewController *review = [[ReviewsViewController alloc] initWithNibName:nil bundle:nil];
review.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:review animated:YES completion:NULL];
}
根据按下哪个单选按钮,我想更改单个按钮以执行4个搜索按钮之一的激活。像这样的东西,但这不起作用:
-(IBAction)btnSearch:(id)sender
{
switch (((UIButton *)sender).tag) {
case 1:
{
WinesViewController *review = [[WinesViewController alloc] initWithNibName:nil bundle:nil];
review.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:review animated:YES completion:NULL];
break;
}
case 2:
{
WineriesViewController *review = [[WineriesViewController alloc] initWithNibName:nil bundle:nil];
review.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:review animated:YES completion:NULL];
break;
}
break;
case 3:
{
GrapesViewController *review = [[GrapesViewController alloc] initWithNibName:nil bundle:nil];
review.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:review animated:YES completion:NULL];
break;
}
case 4:
{
ReviewsViewController *review = [[ReviewsViewController alloc] initWithNibName:nil bundle:nil];
review.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:review animated:YES completion:NULL];
break;
}
default:
break;
}
}
我在IB中分配了标签但没有任何反应。我也尝试为各个按钮分配标签,没有任何反应。我尝试在if语句中使用字符串,没有任何反应。
答案 0 :(得分:0)
好的,这就是我要做的。首先定义一个这样的枚举:
typedef NS_ENUM(NSInteger, SearchType) {
SearchTypeWine,
SearchTypeWinery,
SearchTypeGrapes,
SearchTypeReviews
};
为控制器提供如下属性:
@property (nonatomic) SearchType searchType;
然后你制作4个UIButton。您可以将它们全部挂钩到相同的IBAction以减少LoC。
- (IBAction)buttonPresssed:(id)sender
{
UIButton *button = (UIButton *)sender;
switch (button.tag) {
case 100:
self.searchType = SearchTypeWine;
break;
case 101:
self.searchType = SearchTypeWinery;
break;
case 102:
self.searchType = SearchTypeReviews;
break;
case 103:
self.searchType = SearchTypeGrapes;
break;
default:
break;
}
}
然后执行搜索的方法:
- (IBAction)performSearch:(id)sender
{
switch (self.searchType) {
case SearchTypeWine:
//handle appropriately
break;
case SearchTypeWinery:
//handle appropriately
break;
case SearchTypeReviews:
//handle appropriately
break;
case SearchTypeGrapes:
//handle appropriately
break;
default:
break;
}
}