0

first subview (BIDBlueViewController's BlueView.xib

I am following Apress Beginning IOS 6 for school where we were asked by the professor to implement a custom algorithm that switches between 3 views (blue, yellow, and green) with each press of the "Next View" button in the toolbar. My approach was to add the subviews at different indices and shuffle the views in the hierarchy in BIDSwitchViewController.m:

#import "BIDSwitchViewController.h"
#import "BIDYellowViewController.h"
#import "BIDBlueViewController.h"
#import "BIDGreenViewController.h"

NSInteger count;

@interface BIDSwitchViewController ()

@end

@implementation BIDSwitchViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.yellowViewController = [[BIDYellowViewController alloc] initWithNibName:@"YellowView"
                                                                              bundle:nil];
        self.greenViewController = [[BIDGreenViewController alloc] initWithNibName:@"GreenView"
                                                                            bundle:nil];
        self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:@"BlueView"
                                                                          bundle:nil];
        //the topmost view is the last one in the stack (2)
        [self.view insertSubview:self.blueViewController.view atIndex:2];
        [self.view insertSubview:self.yellowViewController.view atIndex:1];
        [self.view insertSubview:self.greenViewController.view atIndex:0];
        [self.view setBackgroundColor:self.blueViewController.view.backgroundColor];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.blueViewController = [[BIDBlueViewController alloc]
                               initWithNibName:@"BlueView" bundle:nil];
    [self.view insertSubview:self.blueViewController.view atIndex:0];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.;
    self.blueViewController = nil;
    self.yellowViewController = nil;
    self.greenViewController = nil;
}

- (IBAction)switchViews:(id)sender
{
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:                  
     UIViewAnimationTransitionFlipFromRight                 
                           forView:self.view cache:YES];
    switch (count)
    {
        case 0:
            [self.view sendSubviewToBack:self.blueViewController.view];
            [self.view bringSubviewToFront:self.yellowViewController.view];
            [self.view setBackgroundColor:self.yellowViewController.view.backgroundColor];
            break;
        case 1:
            [self.view sendSubviewToBack:self.yellowViewController.view];
            [self.view bringSubviewToFront:self.greenViewController.view];
            [self.view setBackgroundColor:self.greenViewController.view.backgroundColor];
            break;
        case 2:
            [self.view sendSubviewToBack:self.greenViewController.view];
            [self.view bringSubviewToFront:self.blueViewController.view];
            [self.view setBackgroundColor:self.blueViewController.view.backgroundColor];
            break;
    }
    if (++count >= 3)
    {
        count = 0;
    }
    [UIView commitAnimations];
}

@end

Here is the code in BIDAppDelegate.m where the root view controller is added as an instance of BIDSwitchViewController:

@implementation BIDAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    self.switchViewController = [[BIDSwitchViewController alloc]
                                 initWithNibName:@"SwitchView" bundle:nil];
    UIView *switchView = self.switchViewController.view;
    CGRect switchViewFrame = switchView.frame;
    switchViewFrame.origin.y += [UIApplication
                                 sharedApplication].statusBarFrame.size.height;
    switchView.frame = switchViewFrame;
    //[self.window addSubview:switchView];
    self.window.rootViewController = self.switchViewController;


    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

And the BIDSwitchViewController.h header file:

#import <UIKit/UIKit.h>
@class BIDSwitchViewController;
@interface BIDAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) BIDSwitchViewController *switchViewController;

@end

The app and logic all work as the views switch from blue -> yellow -> green and back to blue again. As shown in the first picture (BIDBlueViewController's BlueView.xib subview of the BIDSubViewController) each of the views overlap the toolbar slightly. I have double and triple checked all of my simulated metrics in IB with the help of one of my classmates:

Interface Builder 1

enter image description here

Am I using poor practice in shuffling the topmost views through the view hierarchy array, instead of removing each view via the book's method of "removeFromParentViewController()," or is their another, hidden cause for the sub views not properly sitting inside / behind the parent view?

4

1 回答 1

0

你的 UIToolbar 控件在它的视图上的 Z-Order 在你的蓝色视图控制器后面。由于您在 IB 中创建了它,因此您可以为其添加一个 IBOutlet,并将其视图带到父视图的顶部。您将 ViewControllers 作为子视图加载,因此它们都是您父级的 self.view 的子视图。

在您的标头定义中:

IBOutlet *toolBar UIToolbar;

在您的实施中脱颖而出。

[self.view bringSubviewToFront:toolBar];

您可以在添加蓝色控制器子视图后或在所有案例语句的末尾执行此操作。

于 2013-06-24T19:14:46.297 回答