文章目录
这是练习 iOS 开发所做的第一个项目:实现一个简单页面,在博客中记录了项目过程所面临的一些问题以及解决方法。
本练习到涉及到的一些开发方法包括:
1. 创建根视图
在 AppDelegate.m 文件的 didFinishLaunchingWithOptions 方法中新增下列语句,首先创建一个和屏幕一样大的 window,然后创建一个 UIViewController 作为该 window 的根视图控制器,最后显示该根视图;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 1. 创建一个和屏幕一样大的 window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 2. 指定 window 的根视图控制器
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
// 3. 显示 window
[self.window makeKeyAndVisible];
return YES;
}
2. 实现 UITabBarViewController
UITabBarViewController 的实现效果是根页面上的三个选项,通过指定不同的选项切换到不同的页面;
选择 UITabBarViewController 作为根视图控制器的父类:
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface ViewController : UITabBarController
@end
为了显示出相应的内容,重载父类的 viewDidLoad 方法即可;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建第一个页面
FirstViewController *firstPage = [[FirstViewController alloc] init];
UIImage *firstBarImage = [UIImage imageNamed:@"001-confetti"];
UIImage *firstBarSelectImage = [UIImage imageNamed:@"002-drum"];
firstPage.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"新闻" image:firstBarImage selectedImage:firstBarSelectImage];
[firstPage.tabBarItem setImageInsets:UIEdgeInsetsMake(0, 20, 0, 20)];
// 创建第二个页面
UIViewController *secondPage = [[UIViewController alloc] init];
UIImage *secondBarImage = [UIImage imageNamed:@"003-sakura"];
UIImage *secondBarSelectImage = [UIImage imageNamed:@"004-sakura"];
secondPage.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"下载" image:secondBarImage selectedImage:secondBarSelectImage];
[secondPage.tabBarItem setImageInsets:UIEdgeInsetsMake(0, 20, 0, 20)];
// 创建第三个页面
UIViewController *thridPage = [[UIViewController alloc] init];
UIImage *thirdBarImage = [UIImage imageNamed:@"014-bench"];
UIImage *thirdBarSelectImage = [UIImage imageNamed:@"012-hanami"];
thridPage.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"未登录" image:thirdBarImage selectedImage:thirdBarSelectImage];
[thridPage.tabBarItem setImageInsets:UIEdgeInsetsMake