开发者

On shake... iOS

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) {
     开发者_如何学Python       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.


Have you made that view the first responder? I.e. [yourView becomeFirstResponder]; (probably from some viewDidAppear: method). You might want to check if it actually is the first responder when you shake your device.


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:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
    NSLog(@"self is first responder %i",[self isFirstResponder]);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜