gpt4 book ai didi

ios - 具有单个 Segue 的多个按钮

转载 作者:行者123 更新时间:2023-11-28 21:53:10 25 4
gpt4 key购买 nike

我有四个按钮和一个 segue。当这些按钮中的任何一个在事件中被触及时,它将在目标 View Controller 上加载相应的图像。为此,我通过它们的标记属性 [每个按钮 (0,1,2,3)] 来识别按钮。这是我的代码:

- (IBAction)Session1Btn:(id)sender {
[self performSegueWithIdentifier:@"isSection" sender:self];
}

- (IBAction)Session2Btn:(id)sender {
[self performSegueWithIdentifier:@"isSection" sender:self];
}

- (IBAction)Session3Btn:(id)sender {
[self performSegueWithIdentifier:@"isSection" sender:self];
}

- (IBAction)Session4Btn:(id)sender {
[self performSegueWithIdentifier:@"isSection" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

SectionViewController *sVC=[segue destinationViewController];
if([segue.identifier isEqualToString:@"isSection"])
{
// the following line gives an error!
NSInteger tagIndex = [(UIButton *)sender tag];

[sVC setSelectedButton:[NSNumber numberWithInteger:tagIndex]];

}
}

但是我收到以下错误:

enter image description here

最佳答案

您必须像这样编写 UIButton UITouchUpInside 事件:

- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:@"isSection" sender:sender];
}

所有按钮的方式相同。

现在在.h 文件 中添加一个变量UIButton *selectedButton。并更新您的按钮事件方法,如下所示:

- (IBAction)buttonPressed:(id)sender
{
selectedButton = (UIButton *)sender;
[self performSegueWithIdentifier:@"isSection" sender:sender];
}

现在您有了 selectedButton 引用,可以在其他方法中使用它。这样,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender {

SectionViewController *sVC=[segue destinationViewController];
if([segue.identifier isEqualToString:@"isSection"])
{
[sVC setSelectedButton:[NSNumber numberWithInteger: selectedButton.tag]];

}
}

关于ios - 具有单个 Segue 的多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27531123/

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