xcode 多个按钮使用唯一标识符连接到同一个视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20364517/
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
Multiple buttons segue to the same view controller with unique identifier
提问by Oneill
I have a ViewControllerA that has roughly 15 buttons in my case they represent levels, When a level is pressed it segues to ViewControllerB. Now it doesn't make sense to manually ctrl+drag and create segue for each button. I'm wondering how can a button segue to ViewControllerB and have a unique identifier then ill be passing an object in other words data to ViewControllerB to recognize which level the user pressed. I also want to point out that I have a push view controller. So far one of my unsuccessful attempts were to do is to ctrl+drag all the buttons and create a method called:(the button title in my case is the level number)
我有一个 ViewControllerA,在我的例子中它有大约 15 个按钮,它们代表级别,当按下一个级别时,它会转到 ViewControllerB。现在手动 ctrl+drag 并为每个按钮创建 segue 是没有意义的。我想知道按钮如何转至 ViewControllerB 并具有唯一标识符,然后将对象(即数据)传递给 ViewControllerB 以识别用户按下的级别。我还想指出我有一个推送视图控制器。到目前为止,我不成功的尝试之一是按住 ctrl+拖动所有按钮并创建一个名为的方法:(在我的例子中按钮标题是级别编号)
- (IBAction)buttonPressed:(UIButton *)sender {
[self performSegueWithIdentifier:[sender currentTitle] sender:self];
}
afterwards I call the prepareForSegue:segue sender:sender
method.To be honest Im new to iOS developing. Any helpful tutorial or article would be great for me.
Thanks in advance for all helpers.
之后我调用了prepareForSegue:segue sender:sender
方法。老实说,我是 iOS 开发的新手。任何有用的教程或文章对我来说都很棒。在此先感谢所有帮助者。
回答by rdelmar
You were close with your unsuccessful attempt. You only want one identifier for a single segue that you set up in IB directly from the controller to VCB. In your action method, that all the button's invoke, do this (notice that I'm passing the button in the sender argument):
您已经接近失败的尝试。对于在 IB 中直接从控制器设置到 VCB 的单个 segue,您只需要一个标识符。在您的操作方法中,所有按钮的调用都执行此操作(注意我在 sender 参数中传递按钮):
- (IBAction)buttonPressed:(UIButton *)sender {
[self performSegueWithIdentifier:@"MySegueIdentifier" sender:sender];
}
Then in prepareForSegue, you can use the sender argument to get the button's title:
然后在 prepareForSegue 中,您可以使用 sender 参数来获取按钮的标题:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender {
NSString *title = sender.currentTitle;
// do what you need with the title
}
回答by nhgrif
- Create a private property on
ViewControllerA
(perhaps using an enum) to make note of which level is selected. - In your
- (IBAction)buttonPressed:(UIButton *)sender
method, you need to figure out which button was pressed, and use that information to set the property. Then call[self performSegueWithIdentifier:@"nameOfStoryboardSegue" sender:self];
- Create a public property on
ViewControllerB
to keep track of which level is selected. - Override the
prepareForSegue:segue sender:sender
method. Instantiate the destination view controller as aViewControllerB
, and set the property on B to match the selected property from A. - Perform the segue.
- 在
ViewControllerA
(可能使用枚举)上创建一个私有属性以记下选择了哪个级别。 - 在您的
- (IBAction)buttonPressed:(UIButton *)sender
方法中,您需要确定按下了哪个按钮,并使用该信息来设置属性。然后打电话[self performSegueWithIdentifier:@"nameOfStoryboardSegue" sender:self];
- 创建一个公共属性
ViewControllerB
以跟踪选择了哪个级别。 - 覆盖该
prepareForSegue:segue sender:sender
方法。将目标视图控制器实例化为 aViewControllerB
,并在 B 上设置属性以匹配从 A 中选择的属性。 - 执行转场。
This question & answerwill help you with passing the data between views in the prepareForSegue
method, and this question & answersmight help with handling all the buttons in the single IBAction
method.
此问答将帮助您在prepareForSegue
方法中的视图之间传递数据,此问答可能有助于处理单个IBAction
方法中的所有按钮。
回答by Dug
I know this is an old question, but I found a simple solution...
我知道这是一个老问题,但我找到了一个简单的解决方案......
duplicate the View Controller from your storyboard and assign the same class to it in identity inspector. You will have a duplicate "storyboard view controller" connected to the same code. With its own icon, tag and storyboard ID
从故事板复制视图控制器,并在身份检查器中为其分配相同的类。您将有一个重复的“故事板视图控制器”连接到相同的代码。拥有自己的图标、标签和故事板 ID