iOS開發 ----- UITablBarController_NSUserDefault

UITabBarController

區別


UIToolBarController 用於實現平級導航,比如QQ 微信下邊的三個按鈕 實現平級視圖的切換
UINavigationController 用於實現具有層級關係的導航實現

使用方法

//在appdelegate中生命
/*
toolBarController
    navigationController1
        firstViewController
    navigationController2
        secendViewController
    navigationController3
        thirdViewController
    navigationController4
        forthViewController
*/

//個數一般不超過4個
//toolBarController是一個跟視圖,包含了四個平級視圖控制器
//每一個平級視圖,採用了navigationViewController

FirstViewController * fVC = [[FirstViewController alloc]init];
UINavigationController * uNC1 = [[UINavigationController alloc]initWithRootViewController:fVC];


SecendViewController * sVC = [[SecendViewController alloc]init];
UINavigationController * uNC2 = [[UINavigationController alloc]initWithRootViewController:sVC];


ThirdViewController  *tVC = [[ThirdViewController alloc]init];
UINavigationController * uNC3 = [[UINavigationController alloc]initWithRootViewController:tVC];


FourthViewController * forVC = [[FourthViewController alloc]init]; 
UINavigationController * uNC4 = [[UINavigationController alloc]initWithRootViewController:forVC];



UITabBarController * uTBC = [[UITabBarController alloc]init];
uTBC.viewControllers = @[uNC1,uNC2,uNC3,uNC4];

//這樣的話就可以實現下邊有四個導航欄

相關屬性

//設置每一個viewController對應的標題,圖片,點擊時的圖片
tVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"find" image:[UIImage imageNamed:@"home_tabbar_icon_find.png"] selectedImage:[UIImage imageNamed:@"home_tabbar_icon_find_sel.png"]];

//設置未讀消息的個數
tVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",10];


//設置tabBar的樣式
uTBC.tabBar.barStyle = UIBarStyleBlack;


//設置tabBar的背景圖片
uTBC.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bar.png"];


//設置默認展示那個頁面
uTBC.selectedIndex = 1;


//拿到所有的viewController
NSArray * array = uTBC.viewControllers;

代理方法

//設置了代理之後,常用的有兩種方法
uTBC.delegate = self;
//點擊到這個button時要做的事
-(void)tabBarController:(nonnull UITabBarController *)tabBarController didSelectViewController:(nonnull UIViewController *)viewController


//該按鈕是否可以點擊
-(BOOL)tabBarController:(nonnull UITabBarController *)tabBarController shouldSelectViewController:(nonnull UIViewController *)viewController

//取得當前點擊的元素的索引
NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];

NSUserDefaults


//簡單數據的持久化存儲方式

//是一個單例對象,把數據存儲到本地的plis文件中
[[NSUserDefaults standardUserDefaults] setValue:@0 forKey:@"last"];
[[NSUserDefaults standardUserDefaults] synchronize];

//通過objectForKey取出數據做相應的操作
[[[NSUserDefaults standardUserDefaults] objectForKey:@"last"] integerValue];

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章