常见App页面结构分析:
单页面展示:
- 列表页面的展示「UITableView」。
- 滚动页面的展示「UIScrollow」。
多页面展示:
- 通过底部标签栏「TabBar」。
- 通过Push栈的方式进行页面的切换。
UITabBarController:
介绍:通过底部对应标签点击来管理多个ViewController之间的切换「一般4-5个可选选项」。
图示:
UITabBar:「UITabBarItem提供内容」
介绍:展示对应标签、管理对应的ViewController以及响应对应的事件。
图示:
NavigationController:
介绍:通过出栈入栈「Push/Pop」的方式管理页面之间的跳转。
图示:
UINavigationBar:「UIBarButtonItem提供内容」
介绍:展示设置导航的内容包括标题、背景色、按钮等。
图示:
UIWindow:
介绍:提供App展示的基础窗口,继承UIView。
图示:
搭建App:
Next 1:删除系统自动生成的Main.storyboard文件。Application Scene Manifest\Main storyboard file base name。
图示:
Next 2.1:创建Window以及第一种架构模式。
代码演示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
JKNewVc *newVc = [[JKNewVc alloc] init];
newVc.tabBarItem.image = [UIImage imageNamed:@"page"];
newVc.tabBarItem.selectedImage = [UIImage imageNamed:@"page_selected"];
newVc.tabBarItem.title = @"新闻";
newVc.navigationItem.title = @"新闻";
UINavigationController *newNav = [[UINavigationController alloc] initWithRootViewController:newVc];
JKVideoVc *videoVc = [[JKVideoVc alloc] init];
videoVc.tabBarItem.image = [UIImage imageNamed:@"video"];
videoVc.tabBarItem.selectedImage = [UIImage imageNamed:@"video_selected"];
videoVc.tabBarItem.title = @"视频";
videoVc.navigationItem.title = @"视频";
UINavigationController *videoNav = [[UINavigationController alloc] initWithRootViewController:videoVc];
JKRecommendVc *recomendVc = [[JKRecommendVc alloc] init];
recomendVc.tabBarItem.image = [UIImage imageNamed:@"like"];
recomendVc.tabBarItem.selectedImage = [UIImage imageNamed:@"like_selected"];
recomendVc.tabBarItem.title = @"推荐";
recomendVc.navigationItem.title = @"推荐";
UINavigationController *recomendNav = [[UINavigationController alloc] initWithRootViewController:recomendVc];
JKMainVc *mainVc = [[JKMainVc alloc] init];
mainVc.tabBarItem.image = [UIImage imageNamed:@"home"];
mainVc.tabBarItem.selectedImage = [UIImage imageNamed:@"home_selected"];
mainVc.tabBarItem.title = @"我的";
mainVc.navigationItem.title = @"我的";
UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:mainVc];
UITabBarController *tabbarVc = [[UITabBarController alloc] init];
tabbarVc.viewControllers = @[newNav,videoNav,recomendNav,mainNav];
self.window.rootViewController = tabbarVc;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
Next 2.2:创建Window以及第二种架构模式。
代码演示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
JKNewVc *newVc = [[JKNewVc alloc] init];
newVc.tabBarItem.image = [UIImage imageNamed:@"page"];
newVc.tabBarItem.selectedImage = [UIImage imageNamed:@"page_selected"];
newVc.tabBarItem.title = @"新闻";
newVc.navigationItem.title = @"新闻";
JKVideoVc *videoVc = [[JKVideoVc alloc] init];
videoVc.tabBarItem.image = [UIImage imageNamed:@"video"];
videoVc.tabBarItem.selectedImage = [UIImage imageNamed:@"video_selected"];
videoVc.tabBarItem.title = @"视频";
videoVc.navigationItem.title = @"视频";
JKRecommendVc *recomendVc = [[JKRecommendVc alloc] init];
recomendVc.tabBarItem.image = [UIImage imageNamed:@"like"];
recomendVc.tabBarItem.selectedImage = [UIImage imageNamed:@"like_selected"];
recomendVc.tabBarItem.title = @"推荐";
recomendVc.navigationItem.title = @"推荐";
JKMainVc *mainVc = [[JKMainVc alloc] init];
mainVc.tabBarItem.image = [UIImage imageNamed:@"home"];
mainVc.tabBarItem.selectedImage = [UIImage imageNamed:@"home_selected"];
mainVc.tabBarItem.title = @"我的";
mainVc.navigationItem.title = @"我的";
UITabBarController *tabbarVc = [[UITabBarController alloc] init];
tabbarVc.viewControllers = @[newVc,videoVc,recomendVc,mainVc];
UINavigationController *newNav = [[UINavigationController alloc] initWithRootViewController:tabbarVc];
self.window.rootViewController = newNav;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
Delegate:
介绍:
给某对象提供一个代理以控制对该对象的访问。这时,访问对象不适合或者不能直接引用目标对象,代理对象作为访问对象和目标对象之间的中介。
自定义代理:「常见系统代理」:AppDelegate、UITabBarControllerDelegate、UITableViewDelegate等
Next 1:委托方:声明协议、声明协议对象、对应的事件告诉代理要做什么。
/*.h文件*/
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@class Person;
@protocol PersonDelegate <NSObject>
//必须实现
@required
- (void)eating;
- (void)runPerson:(Person *)per;
//可选实现
@optional
- (void)playing;
@end
@interface Person : NSObject
@property (nonatomic,assign) id<PersonDelegate>delegate;
- (void)printLogType:(int)type;
@end
NS_ASSUME_NONNULL_END
/*.m文件*/
#import "Person.h"
@implementation Person
- (void)printLogType:(int)type{
if (type == 0){
if ([self.delegate respondsToSelector:@selector(eating)]){
[self.delegate eating];
}
}else if (type == 1){
if ([self.delegate respondsToSelector:@selector(runPerson:)]){
[self.delegate runPerson:self];
}
}else if (type == 2){
if ([self.delegate respondsToSelector:@selector(playing)]){
[self.delegate playing];
}
}
}
@end
Next 2:代理方:成为委托方的代理、遵守委托方的协议、完成委托方需要的方法。
#import "JKNewVc.h"
#import "Person.h"
@interface JKNewVc ()<PersonDelegate>
@end
@implementation JKNewVc
- (void)viewDidLoad {
[super viewDidLoad];
Person *per = [Person new];
per.delegate = self;
[per printLogType:1];
// Do any additional setup after loading the view.
}
#pragma mark - PersonDelegate
-(void)eating{
代理eating 回调
}
-(void)runPerson:(Person *)per{
代理runPerson 回调
}
-(void)playing{
代理playing 回调
}
@end