gpt4 book ai didi

iphone - 如何让多个按钮都连接到同一个 View Controller ?

转载 作者:可可西里 更新时间:2023-11-01 03:44:10 26 4
gpt4 key购买 nike

我是 iOS 和 Storyboard的新手,我需要一些关于设计新应用架构的可能方法的建议。我将在数据库中保存一些数据,我将有一个 UIViewController 页面,其中只有一些按钮具有某些类别的名称。当我们说单击“全部”或单击“业务”时,它将根据该类别将数据显示提取到 UITableViewController。

现在我对一个部分感到困惑:

  1. 在 Story board 中是否允许创建从不同按钮到同一个 ViewController 类的多个转场?因此,如果不允许这样做,我是否必须创建 6 个不同的 UITableViewController 类来为 6 个类别制作 segue?

  2. 如果允许从多个按钮转至单个 UIViewController,我如何在 Storyboard 中发送参数以使其意识到我单击了特定按钮以转到 UIViewController。

我是否应该在 UITableViewController 中创建一个带有参数的自定义构造函数,我将从其他按钮单击方法初始化该构造函数?以便它按类别显示结果?还是 Storyboard可以更轻松地做到这一点?

最佳答案

  1. 当然可以。

  2. 创建完所有按钮后,按住 Control 从每个按钮拖动到下一个 View Controller 并创建(例如)“推送”转场。接下来,单击转场图形以选择转场。切换到属性检查器并为 segue 提供一个标识符,例如“All”或“Business”。您可以在 -prepareForSegue: 方法中查看 segue 的标识符,以确定哪个 segue 导致了转换,从而确定要显示哪个类别。

不过,我不推荐这种方法。没有必要创建六个 segue 来做完全相同的事情。我会使用触发单个 segue 的操作:

  • 将每个按钮的 tag 属性设置为唯一值,以便您可以区分按钮。

  • 向您的第一个 View Controller (管理按钮的 View Controller )添加一个操作方法,名称类似于 -(IBAction)switchToCategoryFromButton:(id)sender

    <
  • 将所有按钮连接到该单个操作。

  • 执行操作,使其查看 sender.tag 以确定按下了哪个按钮,使用它来确定加载哪个类别,然后触发 one 以编程方式转到下一个 View Controller 。

由一个 Action 触发的单个 segue 似乎比许多 segue 要整洁得多。执行此操作的代码可能如下所示:

- (IBAction)switchToCategory:(UIControl*)sender
{
// map the tapped button to a category
switch (sender.tag) {
case kAllButton: {
self.category = kAllCategories;
break;
}
case kBusinessButton: {
self.category = kBusinessCategory;
break;
}
default: {
self.category = kAllCategories;
break;
}
}

// start the segue
[self performSegueWithIdentifier:kCategorySegue sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// tell the destination view controller what category to display
if ([segue.identifier isEqualToString:kCategorySegue]) {
[(NextViewController*)segue.destinationViewController setCategory:self.category];
}
}

关于iphone - 如何让多个按钮都连接到同一个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12257711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com