Xcode根据按钮在新视图中加载数据

时间:2012-02-17 19:56:28

标签: ios xcode button view

我有一个视图,其中包含大量按钮,这些按钮都会导致新视图。我想在新视图中显示不同的数据,具体取决于按下哪个按钮进入它。

正确方向上的一点很棒。

2 个答案:

答案 0 :(得分:2)

使用segues将按钮连接到新的视图控制器。然后,您可以在包含按钮的视图控制器中实现方法prepareForSegue:sender:。在该方法中,您可以使用自定义新视图所需的任何数据在新视图上设置属性。您需要确定单击了哪个按钮。一种方法是将每个按钮的tag属性设置为不同的值(在IB或代码中)。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // sender is the button that was clicked
    // you could set the tag property of the UIButtons when they are created
    // and inspect that property here to determine which was clicked

    // vc will be the view controller you are segueing to
    YourViewController *vc = [segue destinationViewController];

    // set properies of youe next view controller with data needed by new view
    switch (sender.tag) {
        case 0:   // first button
            vc.myProperty = someData;
            break;
        case 1:   // second button
            vc.myProperty = someOtherData;
            break;

        etc...
    }

}

答案 1 :(得分:1)

我猜您需要将按钮链接到类似

的功能
- (IBAction) buttonClicked: (UIButton*) sender;

然后根据点击的按钮(您可以设置其标签属性以区分它们并获得sender.tag值以了解已点击的按钮),您只需调用新的相应功能查看更新其数据。