我有四个按钮和一个segue。当这些按钮中的任何一个在事件内部触摸时,它将在目标视图控制器上加载相应的图像。为此,我通过标签属性[每个按钮(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]];
}
}
但是我收到以下错误:
答案 0 :(得分:2)
您必须这样写 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]];
}
}
答案 1 :(得分:0)
为所选按钮创建属性:
var selectedButton: Int = Int()
在每次IBAction中,将selectedButton
分配给1或2并添加performSegue(withIdentifier: "yourSegueIdentifier", sender: self)
。
在准备segue时添加以下内容:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if buttonTapped == 1 {
// what ever happens when first button selected
} else if buttonTapped == 2 {
// what ever happens when second button selected
}
}