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

标签 iphone ios ios5 storyboard

我是 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/

相关文章:

ios - 使用 SwiftUI 呈现多个模式表

iOS多步表单

iphone - 如何正确应用 Core Image 滤镜

iPhone 用户界面选择器

iphone - UIDocument 和一个 NSDictionary

ios - 以编程方式定位 UIToolbar 的最佳方式(有或没有 UIToolbarDelegate)?

objective-c - setCompletionBlock 中的 AFNetworking EXC_BAD_ACCESS

objective-c - 定时 UIAlert 每 60 秒触发一次,无论我使用什么延迟

ios - 将应用程序提交到应用程序商店错误

ios - 搜索后 TableView 中 Cell 的值错误