2013-03-11 58 views
1

我已經在iPad和iPad模擬器上對此進行了測試,並且在一次成功搖晃之後,motionBegan方法從不會被調用。以下是我的程序片段。搖動不再起作用UIAlertview

- (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能夠在第一次搖動時精確顯示一次,並在隨後的所有搖動中被解散。我有一種預感,第一響應者仍然依附於UIAlertView,它阻止了再次調用motionBegan方法。

更新:在我相應的UIView中,有一個UIActionSheet被創建。這是在我的UIView中調用和實現的),並且在屏幕上顯示UIActionSheet的同時觸發motionBegan方法(它位於我的UIViewController中),這是motionBegan方法不再能夠被調用的問題。

之後,UIAlertView從任何按鈕選擇中消除,motionBegan方法不再被調用,但UIActionSheet工作得很好。在UIView中沒有firstResponder賦值,並且UIViewController中只存在「canBecomeFirstResponder」。有任何想法嗎?

+0

我複製並粘貼了你的代碼,並且它在iPad模擬器上反覆工作(我唯一拿出的是alert.tag = shakeTag行)。 – rdelmar 2013-03-12 00:58:59

+0

@rdelmar它在iPAD模擬器上不適用於我。硬件震動手勢不再調用motionBegan,因爲只有一行輸出:「NSLog(@」震動事件發生。「);」我有以下方法來處理特定的警報標籤,「 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex」 – David 2013-03-12 01:08:43

+0

嗯,我不知道爲什麼你的不工作。我添加了標籤和提醒委託方法,對我來說它仍然可以正常工作。 – rdelmar 2013-03-12 01:58:04

回答

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. 打電話給你alertView東西在motionEnd代替motionBegan。它可以更好。

+0

與響應者鏈完全相關的東西是什麼? – David 2013-03-12 16:14:09

+0

我已經更新了答案David。 – 2013-03-12 21:13:27

+0

明白了,謝謝! – David 2013-03-12 21:23:37