一 initialize方法
+ (void)initialize
- 第一次使用這個類或者它的子類的時候調用,但是並不是說只會調用一次。
- 初始化子類的時候,先初始化父類,在初始化子類
+ (void)load
- 如果一個類只想做一次操作的時候,在load方法裏去調用
二 新特性界面加立即體驗
- 最後一個 cell 加上立即體驗
- 封裝方法最後一個 cell 判斷方法 XMGNewFeatureCell
// 用來判斷下當前cell對象是否是最後一個cell
- (void)setIndexPath:(NSIndexPath *)indexPath count:(int)count
{
if (indexPath.item == count - 1) {
// 最後一個cell
// 添加一個立即體驗按鈕,首先保存整個cell只有一個體驗按鈕
// 顯示這個按鈕
self.startBtn.hidden = NO;
}else{ // 不是最後一個cell
// 隱藏這個按鈕
self.startBtn.hidden = YES;
}
}
- 懶加載,保證一個控件只有一個
- (UIButton *)startBtn
{
if (_startBtn == nil) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
_startBtn= btn;
[btn setBackgroundImage:[UIImage imageNamed:@"guideStart"] forState:UIControlStateNormal];
[btn sizeToFit];
// 監聽點擊
[btn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
btn.center = CGPointMake(self.width * 0.5 , self.height * 0.9);
[self.contentView addSubview:btn];
}
return _startBtn;
}
// 點擊立即體驗按鈕的時候調用
- (void)start
{
// 跳轉到主框架界面。界面之間跳轉,導航控制器,tabBarVc,modal
// 不能使用modal原因:新特性界面一直存在,被窗口的根控制器一直強引用
XMGTabBarController *vc = [[XMGTabBarController alloc] init];
// 設置窗口的根控制器爲主框架控制器
XMGKeyWindow.rootViewController = vc;
}
- 封裝一個工具類,專門用來存儲XMGSaveTool
// 讀取數據
+ (id)objectForKey:(NSString *)defaultName
{
return [[NSUserDefaults standardUserDefaults] objectForKey:defaultName];
}
// 存儲數據
+ (void)setObject:(id)value forKey:(NSString *)defaultName
{
[[NSUserDefaults standardUserDefaults] setObject:value forKey:defaultName];
}
- 選擇根控制器XMGRootTool
- 抽取AppDelegate裏的的判斷版本代碼
+ (UIViewController *)chooseWindowRootVC
{
// 判斷下當前有沒有最新的版本
// 最新的版本保存到info.plist文件
NSString *curVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
// 獲取上一次保存的最新版本號
NSString *lastVersion = [XMGSaveTool objectForKey:"version"];
UIViewController *rootVc;
if ([curVersion isEqualToString:lastVersion]) { // 相等
// 沒新版本,進入主框架界面
// 創建tabBarVc
rootVc = [[XMGTabBarController alloc] init];
}else{ // 表示有最新的版本號,進入新特性界面
// 如果有,進入新特性界面
rootVc = [[XMGNewFeatureViewController alloc] init];
// 保存當前的最新的版本號
[XMGSaveTool setObject:curVersion forKey:"version"];
// 新特性界面:就是展示幾張圖片
}
return rootVc;
}
三 搭建設置界面
- 非根控制器隱藏 tabBar 條
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.childViewControllers.count != 0) { // 非跟控制器
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
- 設置 tableview 樣式,重寫XMGSettingViewController的 init 方法
- (instancetype)init
{
return [self initWithStyle:UITableViewStyleGrouped];
}
- 建一模型XMGSettingItem,模型決定 cell 展示
@interface XMGSettingItem : NSObject
/** 描述cell圖片 */
@property (nonatomic, strong) UIImage *image;
/** 描述cell文字 */
@property (nonatomic, strong) NSString *title;
+ (instancetype)itemWithImage:(UIImage *)image title:(NSString *)title;
@end
+ (instancetype)itemWithImage:(UIImage *)image title:(NSString *)title
{
XMGSettingItem *item = [[self alloc] init];
item.image = image;
item.title = title;
return item;
}
- 數組模型XMGGroupItem
+ (instancetype)groupWithItems:(NSArray *)items
{
XMGGroupItem *group = [[self alloc] init];
group.items = items;
return group;
}
- 第一組樣式
// 添加第0組
- (void)setUpGroup0
{
// 創建行模型
// 使用兌換碼
XMGSettingItem *RedeemCode = [XMGSettingItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兌換碼"];
// Items:存儲當前數組有多少行模型
// 創建一個組模型,描述第0組
XMGGroupItem *group = [XMGGroupItem groupWithItems:@[RedeemCode]];
// 設置頭部標題
group.headerTitle = @"abc";
// 添加組模型到groups數組,有多少個組模型就有多少組
[self.groups addObject:group];
}
- cell 設置 數據源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groups.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 取出當前的組模型
XMGGroupItem * group = self.groups[section];
return group.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
// 取模型
// 哪一組的模型
XMGGroupItem *group = self.groups[indexPath.section];
// 從模型數組數組中取出對應的模型
XMGSettingItem *item = group.items[indexPath.row];
cell.imageView.image = item.image;
cell.textLabel.text = item.title;
return cell;
}
// 返回第section組的頭部標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// 取出當前是哪一組
XMGGroupItem *group = self.groups[section];
return group.headerTitle;
}
// 設置第section組的尾部標題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
// 取出當前是哪一組
XMGGroupItem *group = self.groups[section];
return group.footerTitle;
}
四 設置 cell 完善
根據行模型確定cell右邊輔助視圖
- 1.提供一個類型枚舉,箭頭,開頭
- 2.用子類去判斷cell的類型(這個方法更加面向對象)
- 箭頭類 XMGSettingArrowItem
- 開關類 XMGSettingSwitchItem
- @property (nonatomic, assign) BOOL isOpen;
封裝 cell 創建方法
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"cell";
XMGSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[XMGSettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
return cell;
}
- 封裝cell 模型
- (void)setItem:(XMGSettingItem *)item
{
_item = item;
// 設置子控件數據
[self setUpData];
// 設置輔助視圖
[self setUpAccessoryView];
}
#pragma mark - 設置輔助視圖
- (void)setUpData
{
self.imageView.image = _item.image;
self.textLabel.text = _item.title;
}
#pragma mark - 設置輔助視圖
- (void)setUpAccessoryView
{
// 設置輔助視圖
if ([_item isKindOfClass:[XMGSettingArrowItem class]]) {
// 展示箭頭
UIImageView *arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right"]];
self.accessoryView = arrowView;
}else if ([_item isKindOfClass:[XMGSettingSwitchItem class]]){
// 展示開關
UISwitch *switchView = [[UISwitch alloc] init];
self.accessoryView = switchView;
}else{
self.accessoryView = nil;
}
實現點擊 cell 跳轉對應 view
- 保存一個跳轉的控制器類名
- 1.字符串 (有可能會寫錯)
- 2.Class
- 目的控制器的類名 Class:一般用assign
@property (nonatomic, assign) Class descVc;
- 保存一個跳轉的控制器類名
保存控制器
// 設置目的控制器的類名
push.descVc = [XMGPushViewController class];
- descVc有值需要跳轉
if (arrowItem.descVc) {
// 創建目的控制器
UIViewController *vc = [[arrowItem.descVc alloc] init];
vc.navigationItem.title = item.title;
// 跳轉界面
[self.navigationController pushViewController:vc animated:YES];
}
- 設置導航欄標題
- (void)viewDidLoad {
[super viewDidLoad];
// self.navigationItem.title = @"設置";
// 另一種簡單寫法
self.title = @"設置";
}
- 檢查新版本 cell 完善
- 第三方框架MBProgressHUD
- 經常用到會增加很多判斷語句,封裝一個 block,方便調用
- XMGSettingItem.h
/** 保存點擊cell做的事情 */
@property (nonatomic, strong) void(^operationBlock)();
// 保存檢查新版本需要做的事情
version.operationBlock = ^{
[MBProgressHUD showSuccess:@"沒有最新的版本"];
};
- 監聽 cell 點擊代碼段
// 判斷下有木有事情,就判斷下block有沒有值
if (item.operationBlock) {
// 執行保存的代碼
item.operationBlock();
return;
}
- block 使用注意點:
- 避免循環引用
- block 會把代碼塊裏面所有的強指針強引用
- 在 block 中最好不要直接訪問成員屬性
- 弱指針.成員屬性:weakSelf.groups
- 解決循環引用,weak
- 把 self 強指針轉換爲弱指針
__weak XMGSettingViewController *weakSelf = self
- 另一種寫法:typeof(x)獲取 x 的類型XMGSettingViewController
__weak typeof(self) *weakSelf = self
- 把 self 強指針轉換爲弱指針
五 設置推送界面
- 抽取基類
- XMGPushViewController
// 添加第0組
- (void)setUpGroup0
{
// 創建行模型
// 開獎推送
XMGSettingArrowItem *item = [XMGSettingArrowItem itemWithImage:nil title:@"開獎推送"];
item.descVc = [UIViewController class];
// 比分直播
XMGSettingArrowItem *item1 = [XMGSettingArrowItem itemWithImage:nil title:@"比分直播"];
item1.descVc = [XMGScoreViewController class];
// 中獎動畫
XMGSettingArrowItem *item2 = [XMGSettingArrowItem itemWithImage:nil title:@"中獎動畫"];
// 購彩提醒
XMGSettingArrowItem *item3 = [XMGSettingArrowItem itemWithImage:nil title:@"購彩提醒"];
// Items:存儲當前數組有多少行模型
// 創建一個組模型,描述第0組
XMGGroupItem *group = [XMGGroupItem groupWithItems:@[item,item1,item2,item3]];
// 添加組模型到groups數組,有多少個組模型就有多少組
[self.groups addObject:group];
}
- 推薦關注比賽 XMGScoreViewController
- 創建3組cell(都封裝好了)
- (void)setUpGroup0
{
// 創建行模型
XMGSettingSwitchItem *item = [XMGSettingSwitchItem itemWithImage:nil title:@"關注比賽"];
// 創建組模型
XMGGroupItem *group = [XMGGroupItem groupWithItems:@[item]];
group.footerTitle = @"sadsad";
// 添加groups數組
[self.groups addObject:group];
}
- (void)setUpGroup1
{
// 創建行模型
XMGSettingItem *item = [XMGSettingItem itemWithImage:nil title:@"起始時間"];
item.subTitle = @"00:00";
// 創建組模型
XMGGroupItem *group = [XMGGroupItem groupWithItems:@[item]];
// 添加groups數組
[self.groups addObject:group];
}
- (void)setUpGroup2
{
// 創建行模型
XMGSettingItem *item = [XMGSettingItem itemWithImage:nil title:@"結束時間"];
item.subTitle = @"23:59";
__weak typeof(self) weakSelf = self;
item.operationBlock = ^(NSIndexPath *indexPath){
// 獲取選中的cell,把鍵盤添加到cell上面
UITableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:indexPath];
// 彈出鍵盤
UITextField *textField = [[UITextField alloc] init];
[textField becomeFirstResponder];
[cell addSubview:textField];
// 在iOS7之後,只要把textField添加到需要彈出鍵盤的cell上面,就會自動做好鍵盤處理
};
// 創建組模型
XMGGroupItem *group = [XMGGroupItem groupWithItems:@[item]];
// 添加groups數組
[self.groups addObject:group];
}
- 拖動view退出鍵盤
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.view endEditing:YES];
}