效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 @end 5 6 @implementation ViewController 7 8 - (id)init { 9 if (self = [super init]) { 10 //self.title = @"Hello"; 11 UIImage *imgIcon = [UIImage imageNamed:@"Ball1.png"]; 12 self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Hello" image:imgIcon tag:0]; 13 } 14 return self; 15 } 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 20 UILabel* lblMessage = [[UILabel alloc] initWithFrame:self.view.bounds]; 21 lblMessage.text = @"Hello, world!"; 22 lblMessage.textAlignment = NSTextAlignmentCenter; 23 lblMessage.textColor = [UIColor blackColor]; 24 lblMessage.backgroundColor = [UIColor whiteColor]; 25 lblMessage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 26 [self.view addSubview:lblMessage]; 27 } 28 29 - (void)didReceiveMemoryWarning { 30 [super didReceiveMemoryWarning]; 31 // Dispose of any resources that can be recreated. 32 } 33 34 @end
ViewController2.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController2 : UIViewController 4 @end
ViewController2.m
1 #import "ViewController2.h" 2 3 @interface ViewController2 () 4 @end 5 6 @implementation ViewController2 7 8 - (id)init { 9 if (self = [super init]) { 10 //self.title = @"您好"; 11 UIImage *imgIcon = [UIImage imageNamed:@"Ball2.png"]; 12 self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"您好" image:imgIcon tag:1]; 13 } 14 return self; 15 } 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 20 UILabel* lblMessage = [[UILabel alloc] initWithFrame:self.view.bounds]; 21 lblMessage.text = @"您好,世界!"; 22 lblMessage.textAlignment = NSTextAlignmentCenter; 23 lblMessage.textColor = [UIColor whiteColor]; 24 lblMessage.backgroundColor = [UIColor blackColor]; 25 lblMessage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 26 [self.view addSubview:lblMessage]; 27 } 28 29 - (void)didReceiveMemoryWarning { 30 [super didReceiveMemoryWarning]; 31 // Dispose of any resources that can be recreated. 32 } 33 34 @end
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 @property (strong, nonatomic) UIWindow *window; 5 @property (strong, nonatomic) UITabBarController *tabBarController; 6 7 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 #import "ViewController2.h" 4 5 @interface AppDelegate () 6 @end 7 8 @implementation AppDelegate 9 10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 11 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 12 _tabBarController = [[UITabBarController alloc] init]; 13 ViewController *tab1 = [[ViewController alloc] init]; 14 ViewController2 *tab2 = [[ViewController2 alloc] init]; 15 [_tabBarController setViewControllers:@[tab1, tab2] animated:NO]; 16 17 _window.rootViewController = _tabBarController; 18 [_window addSubview:_tabBarController.view]; 19 [_window makeKeyAndVisible]; 20 21 return YES; 22 } 23 24 - (void)applicationWillResignActive:(UIApplication *)application { 25 } 26 27 - (void)applicationDidEnterBackground:(UIApplication *)application { 28 } 29 30 - (void)applicationWillEnterForeground:(UIApplication *)application { 31 } 32 33 - (void)applicationDidBecomeActive:(UIApplication *)application { 34 } 35 36 - (void)applicationWillTerminate:(UIApplication *)application { 37 } 38 39 @end
时间: 2024-11-12 23:27:25