2012-02-15 59 views
0

我已經做了一個應用程序,您搖動手機打開一個新的視圖。所有這三個視圖一起,當您在最後一個視圖中搖動手機時,您將返回到第一個屏幕。當我用自己的.xib創建新的子類控件視圖時,這工作正常。但我想在故事板項目中使用它,我需要改變什麼?如何在使用故事板時使用搖動手勢?

非常感謝!

以下是在.H的代碼:

#import <UIKit/UIKit.h> 
#import "FirstScreenViewController.h" 
#import "SecondScreenViewController.h" 

@interface的ViewController:UIViewController中

{

NSInteger currentScreen; 
UIViewController* currentController; 

}

@end

這裏IN THE .M:

#import "ViewController.h" 

@implementation的ViewController

- (無效)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning]; 
// Release any cached data, images, etc that aren't in use. 

}

#pragma mark shake 

- (BOOL)canBecomeFirstResponder

{

return true; 

}

- (無效)motionEnded:(UIEventSubtype)運動withEvent:方法(的UIEvent *)事件

{

if(motion == UIEventSubtypeMotionShake) 

{

如果(currentController)

{

[currentController。查看removeFromSuperview]; currentController = nil;

} 

    switch (currentScreen) 

{

 case 0: 
      currentController = [[FirstScreenViewController alloc] initWithNibName:@"FirstScreenViewController" bundle:nil]; 
      break; 
     case 1: 
      currentController = [[SecondScreenViewController alloc] initWithNibName:@"SecondScreenViewController" bundle:nil]; 

    } 


    if(currentController) 

{

 [currentController.view setFrame:self.view.bounds]; 
     [self.view addSubview:currentController.view]; 

    } 

    currentScreen++; 
    if(currentScreen >2) 
     currentScreen=0; 

} 

}

#pragma mark - View lifecycle 

- (無效)viewDidLoad中

{

[super viewDidLoad]; currentScreen = 0;

}

- (無效)viewDidUnload

{

[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 

}

@end

+0

如果你的代碼格式正確,你更可能得到幫助。 – 2012-02-15 20:46:10

+0

現在我編輯了。這個可以嗎? – 2012-02-16 07:08:32

+0

通過粘貼它來格式化代碼,選擇然後單擊問題編輯器上方的{}按鈕。或者,縮進4個空格。 – jrturton 2012-02-16 07:19:39

回答

1

您需要三個視圖控制器添加到故事板,和在他們之間(包括從第三個回到第一個)和沙ke手勢識別器附加到每個場景。

每個手勢識別器的操作方法會告訴視圖控制器performSegue:具有適當的segue標識符。