- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对此很陌生.....抱歉。我已经用xCode创建了一个包含10个 View Controller 的应用程序。每个 View Controller 都有一个图像 View 和一个圆角rect按钮。
我想进行设置,以便在 View 上按按钮可以选择其他9个 View 之一,但是加载的 View 必须是随机的。
到目前为止,我所做的是:
-我在 Storyboard 上放置了10个 View Controller ,并将每个类的类别设置为scene1-10
-我创建了10个名为Scene1-10的Objective-C类-ViewController(.h和.m文件)
-在这个时尚场景中我已经在它们之间创建了segue1-> scene2-> scene3-> scene4
-> scene5-> scene6-> 7scene-> scene8-> scene9-> scene10-> scene1
-我将每个segue的类别设置为segue1-10,在他们要去的场景之后命名
到(场景1和场景2之间的segue是segue2)
-我使所有.h文件看起来都像这样(场景#随文件而异):
#import <UIKit/UIKit.h>
@interface scene1 : UIViewController
- (IBAction) randomButton;
@end
#import "scene1.h"
@interface scene1 ()
@end
@implementation scene1
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction) randomButton : (id) sender
{
int i = arc4random() % 10;
if(i == 1)
{
[self performSegueWithIdentifier: @"segue1"
sender: self];
}
else if(i == 2)
{
[self performSegueWithIdentifier: @"segue2"
sender: self];
}
else if(i == 3)
{
[self performSegueWithIdentifier: @"segue3"
sender: self];
}
else if(i == 4)
{
[self performSegueWithIdentifier: @"segue4"
sender: self];
}
else if(i == 5)
{
[self performSegueWithIdentifier: @"segue5"
sender: self];
}
else if(i == 6)
{
[self performSegueWithIdentifier: @"segue6"
sender: self];
}
else if(i == 7)
{
[self performSegueWithIdentifier: @"segue7"
sender: self];
}
else if(i == 8)
{
[self performSegueWithIdentifier: @"segue8"
sender: self];
}
else if(i == 9)
{
[self performSegueWithIdentifier: @"segue9"
sender: self];
}
else
{
[self performSegueWithIdentifier: @"segue10"
sender: self];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
2012-05-05 07:11:11.789 randomTester[2448:f803] -[scene1 randomButton]: unrecognized selector sent to instance 0x686d370
2012-05-05 07:11:11.792 randomTester[2448:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[scene1 randomButton]: unrecognized selector sent to instance 0x686d370'
*** First throw call stack:
(0x13cc022 0x155dcd6 0x13cdcbd 0x1332ed0 0x1332cb2 0x13cde99 0x1914e 0x190e6 0xbfade 0xbffa7 0xbed8a 0x3e2cf 0x3e5e6 0x24dc4 0x18634 0x12b6ef5 0x13a0195 0x1304ff2 0x13038da 0x1302d84 0x1302c9b 0x12b57d8 0x12b588a 0x16626 0x1f5d 0x1ec5)
terminate called throwing an exception(lldb)
#import <UIKit/UIKit.h>
@interface scene1 : UIViewController
- (IBAction) randomButton;
@end
#import "scene1.h"
@interface scene1 ()
@end
@implementation scene1
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)randomButton
{
int i = arc4random() % 10 + 1;
NSString *segueIdentifier = [NSString stringWithFormat:@"segue%d", i];
[self performSegueWithIdentifier:segueIdentifier sender:self];
}- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
2012-05-05 13:00:09.236 randomTester[2699:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<scene1: 0x6868f90>) has no segue with identifier 'segue5''
*** First throw call stack:
(0x13cb022 0x155ccd6 0xdd61b 0x2592 0x13cce99 0x1814e 0x180e6 0xbeade 0xbefa7 0xbdd8a 0x3d2cf 0x3d5e6 0x23dc4 0x17634 0x12b5ef5 0x139f195 0x1303ff2 0x13028da 0x1301d84 0x1301c9b 0x12b47d8 0x12b488a 0x15626 0x224d 0x21b5)
terminate called throwing an exception(lldb)
2012-05-05 13:45:04.467 randomTester[2803:f803] -[scene1 randomButton:]: unrecognized selector sent to instance 0x68b3760
2012-05-05 13:45:04.479 randomTester[2803:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[scene1 randomButton:]: unrecognized selector sent to instance 0x68b3760'
*** First throw call stack:
(0x13cb022 0x155ccd6 0x13cccbd 0x1331ed0 0x1331cb2 0x13cce99 0x1814e 0x180e6 0xbeade 0xbefa7 0xbdd8a 0x3d2cf 0x3d5e6 0x23dc4 0x17634 0x12b5ef5 0x139f195 0x1303ff2 0x13028da 0x1301d84 0x1301c9b 0x12b47d8 0x12b488a 0x15626 0x224d 0x21b5)
terminate called throwing an exception(lldb)
#import <UIKit/UIKit.h>
@interface scene1 : UIViewController
- (IBAction) randomButton;
@end
#import "scene1.h"
@interface scene1 ()
@end
@implementation scene1
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)randomButton
{
int i = arc4random() % 10 + 1;
NSString *segueIdentifier = [NSString stringWithFormat:@"segue%d", i];
[self performSegueWithIdentifier:segueIdentifier sender:self];
}- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
最佳答案
randomButton
方法的声明和实现应具有相同的签名。因此,您应该在.h
文件中这样声明它:
- (IBAction)randomButton:(id)sender;
.m
文件中更改它,如下所示:
- (IBAction)randomButton
{
int i = arc4random() % 10;
// ...
}
i
的值生成segue标识符,从而简化代码:
- (IBAction)randomButton
{
int i = arc4random() % 10 + 1;
NSString *segueIdentifier = [NSString stringWithFormat:@"segue%d", i];
[self performSegueWithIdentifier:segueIdentifier sender:self];
}
i
的行更改为将值从
1
更改为
10
。您的原始代码会生成从
0
到
9
的值。
关于objective-c - 我可以在xCode中将按钮链接到随机 View Controller 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10461839/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!