0

我的应用程序有一个父视图控制器(MainViewController.h/m - UIViewController,没有 NIB 文件)用于所有其他 UIViewController,它同时是 RootViewController。

我的应用程序应该支持 iOS 5,所以 AutoLayout 是关闭的..

继承一些代码:

在 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 个 UIButton。如果其中一个触及 MainViewController 类被调用以从一个 ChildViewController(HomeViewController, etc..) 更改为另一个。

主视图控制器.m

- (IBAction)firstButton_click:(id)sender {

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

现在关于问题。

在 iOS 6 及更高版本的 iPhone(设备或模拟器)上,UIButtons 仅在几次触摸后才会响应。放置在视图底部的按钮要工作,必须触摸更多次,然后放置在顶部的按钮。几次触摸后,当事件触发并更改视图时,当我回到此视图时,一切正常。我只有 iPhone iOS 6.x 才有这个问题。它可以在 iPhone iOS 5.x 和 iPad 5.x-6.x 上正常运行。

如果我将 HomeViewController 设为 RootViewController,按钮当然会响应事件。但是我无法使用 UIViewAnimationOptionTransitionCrossDissolve Animation[[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:] 进行转换,因为视图必须具有相同的父视图控制器。我做错了什么?是虫子吗?有什么解决办法吗?任何帮助将不胜感激。

4

2 回答 2

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];
}
于 2013-04-11T09:38:12.717 回答
0

问题是因为 [_homeViewController didMoveToParentViewController:self];。这是多余的。

MainViewController 中的 ViewDidLoad 应该是:

- (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];
}

现在一切正常..

于 2013-04-11T08:42:19.547 回答