在UIAlertview之后,摇动不再有效

时间:2013-03-11 23:13:51

标签: ios objective-c cocoa-touch ios6 uialertview

我已经在iPad和iPad模拟器上测试了这个,并且在成功摇动之后,从未调用过motionBegan方法。以下是我的程序片段。

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
  if(event.type == UIEventSubtypeMotionShake)
  {
    NSLog(@"Shake event occurred.");

            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Information" message:@"Some message here" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
            alert.tag = shakeTag;
            [alert show];

  }
}

- (BOOL)canBecomeFirstResponder
{
return YES;
}

-(void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:NO];
  [self becomeFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:NO];
}

-(void)viewDidDisappear:(BOOL)animated {
  [self resignFirstResponder];
  [super viewDidDisappear:NO];
}

究竟是什么阻止了motionBegan方法再次发生?我希望UIAlertView能够在第一次摇动时呈现一次,并在随后的震动中被解雇。我有一种预感,即First Responder仍然附加到UIAlertView,这阻止了再次调用motionBegan方法。

更新:在我相应的UIView中,创建了一个UIActionSheet。这是在我的UIView中调用并实现的,并且我在屏幕上显示UIActionSheet的同时触发了motionBegan方法(在我的UIViewController中),不再能够调用motionBegan方法的问题存在。

之后,UIAlertView从任何按钮选择中被解除,不再调用motionBegan方法,但UIActionSheet工作得很好。 UIView中没有firstResponder赋值,UIViewController中只存在“canBecomeFirstResponder”。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

  1. 由于该代码位于viewController中,因此请删除与响应程序链相关的所有内容。你实际上并不需要它。它是自动为您完成的。更准确地说,你可以删除:

    - (BOOL)canBecomeFirstResponder // remove all that method
    {
    return YES;
    }
    
      [self becomeFirstResponder]; // remove this line
    
    ...
      [self resignFirstResponder]; // remove this line
    ...
    

    并删除这个以及所有方法

    -(void)viewWillDisappear:(BOOL)animated {
      [super viewWillDisappear:animated];
    }
    
  2. 在motionEnd中调用alertView内容而不是motionBegan。它可能会更好。

相关问题