摇一摇... iOS
所以我有一个 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。
So I have an IBAction yesNo
that I want to be ran on a shake event. Not all too sure why this is not working. Have followed all the documentation.
-(BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake)
{
[self yesNo];
}
}
And then the IBAction itself:
- (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;
}
}
}
Basically what I have is a fortune ball type thing and when the iPhone is shaken I need that IBAction run.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否将该视图设置为第一响应者?即
[yourViewBecomeFirstResponder];
(可能来自某些viewDidAppear:
方法)。当您摇动设备时,您可能想检查它是否确实是第一响应者。
Have you made that view the first responder? I.e.
[yourView becomeFirstResponder];
(probably from someviewDidAppear:
method).You might want to check if it actually is the first responder when you shake your device.
当我从 IB 中以图形方式定义所有内容切换到 Xcode 中以编程方式内联定义所有内容时,我根本忘记让视图成为第一响应者。这是最终修复它的代码:
When I was switching from defining everything graphically in IB to programmatically inline in Xcode, I forgot to make the view the first responder at all. Here's the code that ultimately fixed it:
我要问的第一件事是这是否应该被定义为 IBAction。如果您仅从代码中调用它,那么您可能需要考虑使用 (void) 代替(只是一种样式选择)。
其次,你确定该方法确实被调用了吗?扔一个 NSLog 进去以确保。
第三,您确定 [def objectForKey:@"activeVersion"] 返回一个字符串吗?返回的值是您期望的吗?扔一个 NSLog 进去以确保。
我的猜测是,其中一个 NSLog 将为您提供问题的答案,因为您的代码的其余部分看起来不错。
First thing I would question is whether this should be defined as an IBAction. If you only call it from code then you might want to consider using (void) instead (Simply a style choice).
Secondly, are you sure that the method is actually being called? Throw an NSLog in there to make sure.
Thirdly, are you sure that [def objectForKey:@"activeVersion"] returns a string? Is the value returned what you would expect? Throw an NSLog in there to make sure.
My guess is that one of the NSLogs will give you the answer to your question as the rest of your code seems fine.