首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用IBAction更改不同IBAction的操作

使用IBAction更改不同IBAction的操作
EN

Stack Overflow用户
提问于 2013-06-22 06:06:40
回答 1查看 227关注 0票数 0

我以前问过这个问题,但没有得到回应。所以我再问一遍!

我有4个这样的单选按钮:

代码语言:javascript
复制
- (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个类似的动作按钮:

代码语言:javascript
复制
- (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个搜索按钮之一的激活。类似这样的东西,但这不起作用:

代码语言:javascript
复制
-(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语句中使用字符串,但什么也没有发生。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-22 08:00:05

好吧,这就是我要做的。首先,像这样定义一个枚举:

代码语言:javascript
复制
typedef NS_ENUM(NSInteger, SearchType) {
    SearchTypeWine,
    SearchTypeWinery,
    SearchTypeGrapes,
    SearchTypeReviews
};

像这样给你的控制器一个属性:

代码语言:javascript
复制
@property (nonatomic) SearchType searchType;

然后你会得到4个UIButtons。您可以将它们都连接到相同的IBAction,以减少LoC。

代码语言:javascript
复制
- (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;
    }
}

然后执行搜索的方法:

代码语言:javascript
复制
- (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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17245049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档