所以我有一个 IBAction yesNo
,我想在一个震动事件中运行它。不太清楚为什么这不起作用。已遵循所有文档。
-(BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake)
{
[self yesNo];
}
}
然后是 IBAction 本身:
- (IBAction)yesNo
{
int rNumber = rand() % 26;
NSUserDefaults *def=[NSUserDefaults standardUserDefaults];
if ([[def objectForKey:@"activeVersion"] isEqualToString:@"0"])
{
switch (rNumber) {
case 0:
result.text = @"Never";
break;
case 1:
result.text = @"If you're lucky...";
break;
case 3:
result.text = @"Think twice";
break;
...
default:
break;
}
}
else if ([[def objectForKey:@"activeVersion"] isEqualToString:@"1"])
{
switch (rNumber) {
case 0:
result.text = @"Never1";
break;
case 1:
result.text = @"If you're lucky...1";
break;
...
case 25:
result.text = @"Very doubtful2";
break;
default:
break;
}
}
else if ([[def objectForKey:@"activeVersion"] isEqualToString:@"3"])
{
switch (rNumber) {
case 0:
result.text = @"Never3";
break;
...
case 25:
result.text = @"Very doubtful3";
break;
default:
break;
}
}
}
基本上我所拥有的是一个幸运球类型的东西,当 iPhone 摇晃时,我需要运行 IBAction。