是代码的第1部分
menuItems = [[NSArray alloc]initWithObjects:@"page 1",@"page 2",@"page 3",@"page 4",@"page 5",@"page 6",@"page 7", nil];
for (int b=0;b<[menuItems count];b++) {
UIButton *mybutton = [[UIButton alloc]initWithFrame:CGRectMake(3.0f, originofButtons, buttonWidth, buttonHeight)];
mybutton.backgroundColor = [UIColor redColor];
[mybutton setTag:b];
[mybutton setTitle:[menuItems objectAtIndex:b] forState:UIControlStateNormal];
[mybutton setSelected:false];
[mybutton addTarget:self action:@selector(buttonpress:) forControlEvents:UIControlEventTouchUpInside];
[m_scrollview addSubview:mybutton];
originofButtons += (buttonHeight + buttonseparator);
}
下面是我想为上面的代码实现的切换方法
- (void)m_scrollview:(UIScrollView *)m_scrollview clickedButtonAtIndex:(NSArray*)buttonpress
{
switch (buttonpress)
{
case 0:
[self performSegueWithIdentifier:@"about" sender:self];
break;
case 1:
[self performSegueWithIdentifier:@"about" sender:self];
break;
case 2:
[self performSegueWithIdentifier:@"about" sender:self];
break;
case 3:
[self performSegueWithIdentifier:@"about" sender:self];
break;
case 4:
[self performSegueWithIdentifier:@"about" sender:self];
break;
}
}
我无法切换。下面的代码在技术上是正确的,因为xcode没有显示错误。但它不会运行。然后xcode会给我错误:
[MainViewController buttonpress:]:无法识别的选择器发送到实例0x7fb5385c1c40 2014-12-13 23:09:37.705 APP [13563:14150802] *由于未捕获的异常终止应用程序&#39; NSInvalidArgumentException&#39;,原因:& #39; - [MainViewController buttonpress:]:无法识别的选择器发送到实例0x7fb5385c1c40&#39; *第一次抛出调用堆栈:
如果我使用下面的代码,我可以让它工作。并且所有@ 1,2,3,4 5等都将具有相同的segue
-(void)buttonpress:(id)sender {
[self performSegueWithIdentifier:@"about" sender:self];
[self drawerAnimation];
}
如何修复此代码,以便能够为所有&#34; @&#34;
使用切换方法答案 0 :(得分:0)
我是OS X程序员,所以细节可能会有所不同,但基本上答案都在你问题的标题中。
在switch语句中,buttonpress需要是一个整数。此外,最好通过包含默认操作来使switch语句详尽无遗。
-(void)buttonpress:(id)sender
{
NSInteger senderButton = [sender tag];
switch (senderButton)
{
case 0:
[self performSegueWithIdentifier:@"about" sender:self];
break;
case 1:
[self performSegueWithIdentifier:@"about" sender:self];
break;
case 2:
[self performSegueWithIdentifier:@"about" sender:self];
break;
case 3:
[self performSegueWithIdentifier:@"about" sender:self];
break;
case 4:
[self performSegueWithIdentifier:@"about" sender:self];
break;
default:
[self performSegueWithIdentifier:@"about" sender:self];
break;
}
}
我还假设声明:
[self performSegueWithIdentifier:@"about" sender:self];
发送有效的选择器。如果没有,Xcode将无法捕捉到这一点。这将是运行时错误。