2012-05-21 71 views
1

我有我想從使用如何調用輕掃手勢的方法從另一種方法在Objective-C

[自performSelector另一種方法把這種姿態刷卡方法:@selector(handleSwipeGesture :) withObject:無afterDelay :10];

我想不通的語法放在@selector()

任何幫助表示讚賞。這裏是我的代碼:

- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)sender { 
     if(sender.direction == UISwipeGestureRecognizerDirectionLeft) { 
      NSLog(@"swipe left"); 
      TutorialMenuViewController *tutorialMenuViewController = [[TutorialMenuViewController alloc] 
                  initWithNibName:@"TutorialMenuViewController" bundle:nil]; 
      [self.navigationController pushViewController:tutorialMenuViewController animated:YES]; 
      [tutorialMenuViewController release]; 
     } 
    } 
+0

您必須爲相應的視圖註冊swipeGestureRecognizer。 – waheeda

+0

謝謝。以下是什麼語法? [self performSelector:@selector(handleSwipeGesture :) withObject:nil afterDelay:10]; – hanumanDev

+1

這是什麼問題?你正在調用它,雖然你不會進入if語句,因爲你正在傳遞一個'nil'對象。所以if語句看起來像這樣並且計算結果爲false [[無方向] == UISwipeGestureRecognizerDirectionLeft]' –

回答

2

如果你想提出無論是在手勢或時間延遲TutorialMenuViewController你會更好只是抽象的呈現出來到不同的方法

- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)sender { 
    if(sender.direction == UISwipeGestureRecognizerDirectionLeft) { 
     [self presentTutorial]; 
    } 
} 

- (void)presentTutorial; 
{ 
    TutorialMenuViewController *tutorialMenuViewController = [[TutorialMenuViewController alloc] 
                 initWithNibName:@"TutorialMenuViewController" bundle:nil]; 
    [self.navigationController pushViewController:tutorialMenuViewController animated:YES]; 
    [tutorialMenuViewController release]; 
} 

現在,你可以簡單地調用

[self performSelector:@selector(presentTutorial) withObject:nil afterDelay:10]; 
2
- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)sender { 
if(sender.direction == UISwipeGestureRecognizerDirectionLeft) { 
    NSLog(@"swipe left"); 
    //TutorialMenuViewController *tutorialMenuViewController = [[TutorialMenuViewController alloc] 
    //              initWithNibName:@"TutorialMenuViewController" bundle:nil]; 
    //[self.navigationController pushViewController:tutorialMenuViewController animated:YES]; 
    //[tutorialMenuViewController release]; 
} 
} 

調用它像這樣:

UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:nil action:nil]; 
[leftSwipe setDirection:(UISwipeGestureRecognizerDirectionLeft)]; 
[self performSelector:@selector(handleSwipeGesture:) withObject:leftSwipe afterDelay:1]; 
[leftSwipe release]; 
+0

似乎很多不必要的工作是創建和配置一個對象來傳遞'if'語句。它也促進了糟糕的設計,因爲現在調用方法需要知道調用方法的內部細節。 –

+0

@ Paul.s我同意。但** hanumanDev **想要這個。 –

+0

無論您提出新問題還是提出新建議,最好的解決方案都是引導人們遠離不良設計。是的,這將是實際盜用OP想要的方式,但這不是解決他們問題的正確方法。我的意思不是聽起來很刺耳,我只是在幫助下一個人的時候給你提供建設性的反饋意見,以便記住它。 –

相關問題