2013-04-10 101 views
0

我的應用程序有一個父視圖控制器(MainViewController.h/m - UIViewController,沒有NIB文件)的所有其他UIViewControllers,這是在同一時間RootViewController。如何正確使用容器視圖控制器在iOS上

我的應用應該支持的iOS 5,所以自動版式是關閉..

海爾一些代碼:

在AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.mainViewController = [[MainViewController alloc] init]; 
    [self.window setRootViewController:self.mainViewController]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

在MainViewController.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 
// Do any additional setup after loading the view. 


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 

     if ([[UIScreen mainScreen] bounds].size.height == 568) { 
      _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone5" bundle:nil]; 
     } else { 
      _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone" bundle:nil]; 
     } 
    } else { 
     _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]; 
    } 


    [self addChildViewController:_homeViewController]; 
    [self.view addSubview:_homeViewController.view]; 
    [_homeViewController didMoveToParentViewController:self]; 
} 

- (void)changeFromViewController:(UIViewController*)fromViewController toViewController:(UIViewController*)toViewController withDuration:(NSNumber*)duration { 

    toViewController.view.frame = self.view.bounds; 
    [toViewController.view layoutIfNeeded]; 

    [self addChildViewController:toViewController]; 
    [self transitionFromViewController:fromViewController 
         toViewController:toViewController 
           duration:[duration floatValue] 
           options:UIViewAnimationOptionTransitionCrossDissolve 
          animations:nil 
          completion:^(BOOL finished) { 

           [toViewController didMoveToParentViewController:self]; 

           [fromViewController willMoveToParentViewController:nil]; 
           [fromViewController removeFromParentViewController]; 
          }]; 
} 

HomeViewController * .xib包含7個UIButtons。如果其中一個觸及MainViewController類,則會從一個ChildViewController(HomeViewController等)更改爲另一個。

HomeViewController.m

- (IBAction)firstButton_click:(id)sender { 

    [(MainViewController *)self.parentViewController setAnimationForChangeFrom:self toStartTestSettingsViewControllerWithDuration:[NSNumber numberWithDouble:0.4] andWithDelay:[NSNumber numberWithDouble:0.1]]; 
} 

現在這個問題。

在iOS 6或更高版本的iPhone(設備或模擬器)上,UIButton僅在多次觸摸後才作出響應。放置在視圖底部工作的按鈕必須多次觸摸,然後放置在頂部的按鈕。經過幾次觸摸,當事件觸發和查看被改變時,當我回到這個視圖時,一切正常。我只有iPhone iOS 6.x纔有這個問題。它在iPhone iOS 5.x和iPad 5.x-6.x上正常工作。

如果我使HomeViewController RootViewController,按鈕當然響應事件。但後來我無法用UIViewAnimationOptionTransitionCrossDissolve Animation [[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]進行轉換,因爲視圖必須具有相同的父視圖控制器。 我錯了什麼?它是錯誤嗎?有沒有解決方法? 任何幫助將不勝感激。

回答

0

問題是因爲[_homeViewController didMoveToParentViewController:self] ;. 這是多餘的。

的viewDidLoad在MainViewController應該是:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
// Do any additional setup after loading the view. 


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 

     if ([[UIScreen mainScreen] bounds].size.height == 568) { 
      _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone5" bundle:nil]; 
     } else { 
      _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone" bundle:nil]; 
     } 
    } else { 
     _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]; 
    } 


    [self addChildViewController:_homeViewController]; 
    [self.view addSubview:_homeViewController.view]; 
} 

現在一切正常..

2

寫這篇文章.PCH文件

#define IS_IPHONE_5 (fabs((double)[ [ UIScreen mainScreen ] bounds ].size.height - (double)568) < DBL_EPSILON) 


- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 



if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 

    if (IS_IPHONE_5) { 
     _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone5" bundle:nil]; 
    } else { 
     _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone" bundle:nil]; 
    } 
} else { 
    _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]; 
} 


[self addChildViewController:_homeViewController]; 
[self.view addSubview:_homeViewController.view]; 
} 
相關問題