標籤:with 變化 標頭檔 nat 選項 入門 super tco 自己
1.刪除故事板中預設的視圖控制器,和與之對應的.h.m檔案
2.從物件程式庫拖導航控制器對象到編輯器中(會好像加了兩個情境)
3.添加兩個類,第一個為UINavigationCOntroller子類關聯到導航控制,第二個為UIViewCOntroller子類關聯到根視圖 和其他視圖
(class是自己的命名,subclassof必須選擇相應的父類)
4.關聯完成後可以更改相應控制器的標籤讓其編程時更友好(這裡的標籤與底層的代碼不關聯)
5.導航控制器與視圖控制器資料聯絡,
可在導航控制器.H中建立屬性@property (nonatomic)int pushCount;
,在視圖控制器的標頭檔中匯入#import“CountingNAvigationController.H”(就是你命名的導航控制器)
在視圖控制器類.M中存取方法
-(IBACTION)incrementCOunt:(id)sender{
((CountingNavigationController *)self..parentViewCOntroller).pushCount++; //屬性parentViewController在導航控制器管理的情境中都自動化佈建為導航控制器對象
}
在視圖控制器.M方法ViewWillAppear:animated中更新標籤更好,因為可能在其他視圖PUSH後返回原始情境
-(void)viewWillAppear:(BOOL)animated {
NSString *pushText;
pushText=[[NSString alloc] initWithFormat:@"%d",((CountingNavigationController *)self.parentViewController).pushCount];
self.countLabel.text=pushText;
}
導航控制器
1.刪除原來的視圖控制器和相應的類檔案.H.M
2.從物件程式庫中拉入TOOLBAR CONTROLLER
3.建立相應的類,UITabBarCOntroller子類與TOOLBARcontroller相連
viewcontroller子類與普通情境控制器相連
4.新添加情境時,將選項卡蘭控制器與新情境控制器相連,類型選擇releationship-viewControllers(選項卡蘭控制器會自動新增切換)
5.規劃變數
需跟蹤3個不同計數器,countingtabbarCOntroller包含三個屬性,分別為每個情境的計數器
viewcontroller包含兩個屬性,outputlabel指向一個標籤顯示三個計數器當前值,第二個為baritem串連到每個情境選項卡蘭項
來更新其徽章值
因為有三個不同計數器,所以genericviewcontroller有三個操作方法,每個情境按鈕觸發針對該情境的方法(三個情境公用一個類控制)
還需兩個方法updatecounts,updatebadge,更新當前計數器值,和徽章(這樣不用在每個increment中重寫相同代碼)
6.在相應的視圖中點擊底部選項卡按鈕,設定屬性可以選擇自定的選項卡圖片,和相應的標題。(主選項卡控制器中相應的會自動變化)
7.建立輸出口,outputlabel串連到每個情境
baritem串連到每個情境的選項卡
需要的操作
incrementcountfirst;串連到第一個情境按鈕,更新第一個情境計數器,二三同樣
由於count操作獨立所以每個按鈕都建立自己的操作
8.實現邏輯
在countingtabbarcontroller中添加3個屬性
@property(nonatomic)int firstcount 。。。。二三同樣
在相應的子視圖控制器中匯入檔案#import“countingtabbarcontroller。h”
9.顯示計數器
在.H中聲明方法標頭檔
。M中實現
-(void)updateCounts {
NSString *countString;
countString=[[NSString alloc] initWithFormat:
@"First: %d\nSecond: %d\nThird: %d",
((CountingTabBarController *)self.parentViewController).firstCount,
((CountingTabBarController *)self.parentViewController).secondCount,
((CountingTabBarController *)self.parentViewController).thirdCount];
self.outputLabel.text=countString;
}
10.選項卡徽章值遞增
在.H中聲明方法原型
.M中實現
-(void)updateBadge {
NSString *badgeCount;
int currentBadge;
currentBadge=[self.barItem.badgeValue intValue]; //INTVALUE方法將badgevalue轉換成整數,badgevalue可以讀取選項卡的當前值
currentBadge++;
badgeCount=[[NSString alloc] initWithFormat:@"%d",
currentBadge];
self.barItem.badgeValue=badgeCount;
}
badgevalue是一個NSString不是整數所以修改他必須執行相應的轉換
11.出發計數器更新
- (IBAction)incrementCountFirst:(id)sender {
((CountingTabBarController *)self.parentViewController).firstCount++;
[self updateBadge];
[self updateCounts];
}
二三同樣
12記住要將相應屬性取消
- (void)viewDidUnload
{
[self setOutputLabel:nil];
[self setBarItem:nil]; //setBarItem 中B會自動變成大寫的
[super viewDidUnload];
}
13,記住在viewwillappear中更新
【self.updatecounts】
ios入門筆記(導航控制器)