我的应用程序出现问题。我有一个登录屏幕,我用一个按钮移动到下一个视图控制器,然后我有3个按钮。 2是隐藏的,1不是。我想要一个不是(红色圆圈)点击运行虚拟按钮更换器。然而,在这个空白中,我为textview.setText(Html.fromHtml("First line,<br/>Second line"))
我创建的RandomNumberButton创建了一个随机数。我做了一个int
声明:
如果randomnumberbutton大于30,那么
if
和redcircle.hidden = YES;
以及Bluecircle.Hidden = NO;
。
问题是我希望按钮在一定时间内可见。所以我将它放入void buttonchanger。
yellowcircle.hidden = YES;
每当我运行这段代码时,它都会给我错误:
线程1 EXC_BAD_ACCESS代码= 2
ButtonTime是一个1,3或4.0秒的随机数。 setbuttonEnabled基本上是一个空洞{{1}}
底线:我在特定行遇到错误[NSTimer scheduledTimerWithTimeInterval:ButtonTime target:self selector:@selector(setButtonEnabled) userInfo:nil repeats:NO];
:
set bluecircle.hidden = YES;
编辑:我将arc4random更改为arc4random_uniform但我仍然得到相同的错误
.m文件:
Thread 1 EXC_BAD_ACCESS code=2
.h文件
[NSTimer scheduledTimerWithTimeInterval:ButtonTime target:self selector:@selector(setButtonEnabled) userInfo:nil repeats:NO];`
答案 0 :(得分:0)
代码2 =无限循环。
ButtonChanger正在调用自己,因为条件总是满足,因此创建无限数量的计时器直到系统放弃。
您应该遵循如何编写清晰可读代码的指南。避免在类或范围之外声明低级属性。还要记住,属性以小写和类大写开头。对象的引用/指针也是属性。
这部分是你的代码看起来如何。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIButton *blueCircleButton;
@property (nonatomic, strong) UIButton *redCircleButton;
@property (nonatomic, strong) UIButton *yellowCircleButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.blueCircleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.blueCircleButton.frame = CGRectMake(50, 0, 100, 100);
self.blueCircleButton.backgroundColor = [UIColor blueColor];
self.redCircleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.redCircleButton.frame = CGRectMake(50, 100, 100, 100);
self.redCircleButton.backgroundColor = [UIColor redColor];
[self.redCircleButton addTarget:self action:@selector(redButtonAction:) forControlEvents:UIControlEventTouchUpInside];
self.yellowCircleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.yellowCircleButton.frame = CGRectMake(50, 200, 100, 100);
self.yellowCircleButton.backgroundColor = [UIColor yellowColor];
for (UIView *view in @[self.blueCircleButton, self.redCircleButton, self.yellowCircleButton]) {
[self.view addSubview:view];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated{
self.yellowCircleButton.hidden = YES;
self.blueCircleButton.hidden = YES;
self.redCircleButton.hidden = NO;
}
-(void)enableBlueCircle:(NSTimer*)timer {
self.blueCircleButton.hidden = YES;
}
-(void)enableYellowCircle:(NSTimer*)timer {
self.yellowCircleButton.hidden = YES;
}
-(void)buttonChanger{
NSInteger randomNumberButton = arc4random() % 100;
if (randomNumberButton>30) {
self.blueCircleButton.hidden = NO;
[NSTimer scheduledTimerWithTimeInterval:[self generateButtonTime] target:self selector:@selector(enableBlueCircle:) userInfo:nil repeats:NO];
self.redCircleButton.hidden = YES;
self.yellowCircleButton.hidden = YES;
}
if (randomNumberButton<30) {
self.yellowCircleButton.hidden = NO;
[NSTimer scheduledTimerWithTimeInterval:[self generateButtonTime] target:self selector:@selector(enableYellowCircle:) userInfo:nil repeats:NO];
self.redCircleButton.hidden = YES;
self.blueCircleButton.hidden = YES;
}
// if (self.yellowCircleButton.hidden = YES, self.blueCircleButton.hidden = YES, self.redCircleButton.hidden = YES) {
// [self buttonChanger];
// }
}
- (void)redButtonAction:(UIButton *)button {
[self buttonChanger];
}
-(double)generateButtonTime {
NSInteger randomNumber = arc4random() % 100;
double buttomTime = 0;
if (randomNumber>50) {
buttomTime = 3.0;
}
if (randomNumber<50) {
buttomTime = 1.0 ;
}
if (randomNumber>10) {
buttomTime = 4.0 ;
}
return buttomTime;
}
@end