Xcode6.1 Development Environment Development View switch program, xcode6.1 View
Xcode6.1 Development Environment Development view switching Program
1. Create an empty project
Because xcode6.1 does not have a blank project, create an ios project from the Single View Application template.
Product Name: Enter Switch. Next, delete some files and change some configurations.
2. Delete views and interface files
Delete Main. storyboard and LaunchScreen. xib. After the configuration is completed, delete the corresponding configuration in the info. plist file. Click the minus sign.
Delete the default ViewController file ViewController. h ViewController. m;
3. Create a View Controller
Next, enter the Controller class name;
Two files are generated: SwitchViewController. h and SwitchViewController. m;
Follow the same steps to create BlueViewController. m and YellowViewController. m and Their. H files.
4. Create a View File
Create a file and select the View in the User Interface category. Name them SwitchView, BlueView, and YellowView respectively;
5. Add a View Controller
Change the default NSObject class of the view to the corresponding controller class;
6. Associate views with Controllers
Add controllers to the other two views in the same way;
7. Modify application Delegation
# Import <UIKit/UIKit. h>
@ ClassSwitchViewController;
@ InterfaceAppDelegate: UIResponder <UIApplicationDelegate>
@ Property (strong, nonatomic) UIWindow * window;
@ Property (strong, nonatomic) SwitchViewController * switchViewController;
@ End
Modify the didfinishlaunchingwitexceptions method of the AppDelegate. m file as follows:
# Import "AppDelegate. h"
# Import "SwitchViewController. h"
@ ImplementationAppDelegate
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {
Self. window = [[uiappswalloc] initWithFrame: [UIScreenmainScreen] bounds];
Self. switchViewController = [[SwitchViewControlleralloc] initWithNibName: @ "SwitchView" bundle: nil];
UIView * switchView = self. switchViewController. view;
CGRect switchViewFrame = switchView. frame;
SwitchViewFrame. origin. y + = [UIApplicationsharedApplication]. statusBarFrame. size. height;
SwitchView. frame = switchViewFrame;
Self. window. rootViewController = self. switchViewController;
Self. window. backgroundColor = [UIColorgreenColor];
[Self. windowmakeKeyAndVisible];
ReturnYES;
}
8. Run the test and view the result.
The green interface is the background color of switchView, And the Toolbar is added to the bottom.
9. Declare other controllers in the master controller
# Import <UIKit/UIKit. h>
@ ClassBlueViewController;
@ ClassYellowViewController;
@ InterfaceSwitchViewController: UIViewController
@ Property (strong, nonatomic) BlueViewController * blueViewController;
@ Property (strong, nonatomic) YellowViewController * yellowViewController;
@ End
Generate-(IBAction) switchView :( UIBarButtonItem *) sender {} method;
10. Insert a Blue View when loading the Root View
-(Void) viewDidLoad {
[SuperviewDidLoad];
Self. blueViewController = [[BlueViewControlleralloc] initWithNibName: @ "BlueView" bundle: nil];
[Self. viewinsertSubview: self. blueViewController. viewatIndex: 0];
}
11. Respond to button events for switchover
-(Void) didReceiveMemoryWarning {
[SuperdidReceiveMemoryWarning];
If (self. blueViewController. view. superview = nil ){
Self. blueViewController = nil;
} Else {
Self. yellowViewController = nil;
}
}
-(IBAction) switchView :( UIBarButtonItem *) sender {
If (self. yellowViewController. view. superview = nil ){
If (self. yellowViewController. view = nil ){
Self. yellowViewController = [[YellowViewControlleralloc] initWithNibName: @ "YellowView" bundle: nil];
}
[Self. blueViewController. viewremoveFromSuperview];
[Self. viewinsertSubview: self. yellowViewController. viewatIndex: 0];
} Else {
If (self. blueViewController. view = nil ){
Self. blueViewController = [[BlueViewControlleralloc] initWithNibName: @ "BlueView" bundle: nil];
}
[Self. yellowViewController. viewremoveFromSuperview];
[Self. viewinsertSubview: self. blueViewController. viewatIndex: 0];
}
}
12. Achieve animation Switching
-(IBAction) switchView :( UIBarButtonItem *) sender {
[UIViewbeginAnimations: @ "View Flip" context: nil];
[UIViewsetAnimationDuration: 1.25];
[UIViewsetAnimationCurve: UIViewAnimationCurveEaseInOut];
If (self. yellowViewController. view. superview = nil ){
If (self. yellowViewController. view = nil ){
Self. yellowViewController = [[YellowViewControlleralloc] initWithNibName: @ "YellowView" bundle: nil];
}
[UIViewsetAnimationTransition: UIViewAnimationTransitionFlipFromRightforView: self. viewcache: YES];
[Self. blueViewController. viewremoveFromSuperview];
[Self. viewinsertSubview: self. yellowViewController. viewatIndex: 0];
} Else {
If (self. blueViewController. view = nil ){
Self. blueViewController = [[BlueViewControlleralloc] initWithNibName: @ "BlueView" bundle: nil];
}
[UIViewsetAnimationTransition: UIViewAnimationTransitionFlipFromRightforView: self. viewcache: YES];
[Self. yellowViewController. viewremoveFromSuperview];
[Self. viewinsertSubview: self. blueViewController. viewatIndex: 0];
}
[UIViewcommitAnimations];
}
13. FAQs
Question 1:
The file "Info. plist" couldn't be openedbecause there is no such file
Deleting the test file is not cleared. You can see and delete it on the project configuration page.
Question 2:
'Could not load NIB in bundle: 'NSBundle </Users/c/Library/Developer/CoreSimulator/Devices/C817BF05-3462-41CB-9D9D-61CB794C78D4/data/Containers/Bundle/Application/F9EC13C2-7589-41CF-A010-48958E902262/Switch. app> (loaded) 'with name 'switchviews''
Self. switchViewController = [[SwitchViewControlleralloc] initWithNibName: @ "SwitchView" bundle: nil];
The name of the initialized View Controller is not specified explicitly, and it is case sensitive;