xcode 使用故事板以编程方式连接按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8718472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:53:31  来源:igfitidea点击:

Programmatically connect buttons using storyboard

iphonexcodestoryboard

提问by Kristoffer Frisell Jarnevid

I have been trying to solve a big "thing" in my application for hours. I'm so tired that I don't even remember what word I should use instead of the "thing" word. :p

几个小时以来,我一直试图解决我的应用程序中的一个大“事情”。我太累了,我什至不记得我应该用什么词来代替“事物”这个词。:p

This is the first time I'm using storyboards in my app.

这是我第一次在我的应用程序中使用故事板。

Im writing out X amount of buttons programmatically which later will be dependent on data from a database. As the title says, my problem is that I'm using a storyboard and can't get a grip on how to create a new view in the storyboard that the buttons then can change to. And also with some data from the first view.

我以编程方式写出 X 数量的按钮,稍后将取决于数据库中的数据。正如标题所说,我的问题是我使用的是情节提要并且无法掌握如何在情节提要中创建按钮可以更改为的新视图。还有一些来自第一个视图的数据。

Here is some code if thats helps you help me. ;p

如果这对您有帮助,这里有一些代码。;p

- (void)viewDidLoad
{
    [super viewDidLoad];

    int heightGrowt = 0;

    for(int i = 0; i < 10; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        button.frame = CGRectMake(10, heightGrowt, 300, 40);

        heightGrowt = heightGrowt + 50;

        [button setTitle:@"First element" forState:UIControlStateNormal];

        [scrollWiew addSubview:button];
    }

    [scrollWiew setScrollEnabled:YES];
    [scrollWiew setContentSize:CGSizeMake(320, heightGrowt)];
}

If you have/know anything that can help me i would be very grateful. :)

如果您有/知道任何可以帮助我的东西,我将不胜感激。:)

回答by Tyler

[theUIViewControllerThatYouAreSeguingFrom performSegueWithIdentifier:@"theIdentifier" sender:theViewCallingForTheSegue];

You can name the segue in the Attributes section of the Inspector.

您可以在 Inspector 的 Attributes 部分为 segue 命名。

回答by lnafziger

Okay, based on your comment you are on the right track.

好的,根据您的评论,您走在正确的轨道上。

In order to retrieve it from the storyboard, you must first go into the storyboard, select the view controller that you want to retrieve (click the yellow circle with the lined paper in it that is under the view controller - Note that you must be zoomed in to see it.) and give the "identifier" field (listed under the Attributes Inspector) a unique name which you will use to refer to this particular view controller. Let's say that we are using firstViewControllerfor purposes of this example.

为了从故事板中检索它,您必须首先进入故事板,选择您要检索的视图控制器(单击视图控制器下方带有横格纸的黄色圆圈 - 请注意,您必须缩放在查看它。)并给“标识符”字段(在属性检查器下列出)一个唯一的名称,您将使用它来引用这个特定的视图控制器。假设我们firstViewController出于此示例的目的而使用。

In your buttonPressed:function, you will need to get the view controller that you want from the storyboard and then display it when the correct button is pressed. I'm assuming that the current view was loaded from the storyboard (and if not you will need to get your reference to the storyboard a different way).

在您的buttonPressed:函数中,您需要从情节提要中获取所需的视图控制器,然后在按下正确的按钮时显示它。我假设当前视图是从故事板加载的(如果不是,您将需要以不同的方式获取对故事板的引用)。

 // I'm assuming that the class is UIViewController.  Change this if not.
 UIViewController *aViewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"firstViewController"];  

 // Once you have retrieved it, you display it like you would any other view controller.  
 // I.e. if you want to push it onto a UINavigationController stack you would use:  
 if (aViewController != nil) {  
   [self.navigationController pushViewController:aViewController animated:YES];  
 }

回答by Herwr

Each storyboard view has a UIViewController class set just as you would do for a separate XIB - Create a new UIViewController subclass file in your project, and set this this as the 'Custom Class - Class' property in the IB Utilities Inspector for the appropriate view in the storyboard.

每个故事板视图都有一个 UIViewController 类设置,就像您为单独的 XIB 设置的一样 - 在您的项目中创建一个新的 UIViewController 子类文件,并将其设置为 IB Utilities Inspector 中的“自定义类 - 类”属性以用于相应的视图在故事板中。

You should then just be able to instantiate this UIViewController object as and when needed from your button, and Push them onto a NavigationController stack, or transition it however suits.

然后,您应该能够在需要时从您的按钮实例化这个 UIViewController 对象,并将它们推送到 NavigationController 堆栈上,或者以适合的方式转换它。

Check these links to a really good Storyboard tutorial:

检查这些链接到一个非常好的故事板教程:

http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2

http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2