详解IOS中Tool Bar切换视图方法

2020-01-21 02:52:12于海丽

IOS,Tool,Bar,切换视图

在左边选User Interface,右边选View,单击Next,在新窗口中的Device Family中选择iPhone,单击Next,打开如下窗口:

IOS,Tool,Bar,切换视图

输入名称RootView,单击Create,创建了一个.xib文件。用同样的方法再创建两个.xib,名称分别是FirstView和SecondView。

4、修改App Delegate:

4.1 单击AppDelegate.h,在其中添加代码,在@interface之前添加@class RootViewController;在@end之前添加@property (strong, nonatomic) RootViewController *rootViewController;添加之后的代码如下:

view source print ?


#import <UIKit/UIKit.h> @class RootViewController; 
 @interface AppDelegate : UIResponder <UIApplicationDelegate> 
 @property (strong, nonatomic) UIWindow *window; 
 @property (strong, nonatomic) RootViewController *rootViewController; 
 @end 

 

4.2 单击AppDelegate.m,修改其代码。


在@implementation之前添加#import "RootViewController.h",在@implementation之后添加@synthesize rootViewController;然后修改didFinishLaunchingWithOptions方法如下:view source print ? 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
 { 
 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
 // Override point for customization after application launch. 
 self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootView" bundle:nil]; 
 UIView *rootView = self.rootViewController.view; 
 CGRect rootViewFrame = rootView.frame; 
 rootViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height; 
 rootView.frame = rootViewFrame; 
 [self.window addSubview:rootView]; 
 self.window.backgroundColor = [UIColor whiteColor]; 
 [self.window makeKeyAndVisible]; 
 return YES; 
 }

 

① self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootView" bundle:nil];

这行代码用于从RootView.xib文件中初始化rootViewController,注意initWithNibName:@"RootView"中不要后缀名.xib

② rootViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height;

使得RootViewController的视图不会被状态栏挡住

5、修改RootViewController.h:

单击RootViewController.h,在其中添加两个属性和一个方法,如下:

view source print ?


 #import <UIKit/UIKit.h> 
 @class FirstViewController; 
 @class SecondViewController; 
 @interface RootViewController : UIViewController 
 @property (strong, nonatomic) FirstViewController *firstViewController; 
 @property (strong, nonatomic) SecondViewController *secondViewController; 
 - (IBAction)switchViews:(id)sender; 
 @end