您的位置:首页 > 产品设计 > UI/UE

UI部分 — 14

2018-03-01 12:42 176 查看
    - iOS应用数据存储的常用方式

    
(1)XML属性列表(plist)归档

    
(2)preference(偏好设置)

    
(3)NSKeyedArchiver归档(NSCoding)

    
(4)SQLite3

    
(5)Core Data

    - 应用沙盒

    
(1)每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离。应用必须呆在自己的沙盒里,其他应用不能访问该沙盒

    
(2)应用沙盒结构分析

                 1  应用程序包:包含了所有的资源文件和可执行文件

                 2  Documents:保存应用运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录

                 3  tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除,应用没有运行时,系统也可能会清除该目录下的文件,iTunes同步设备时不会备份该目录

                 4  Library/Caches:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录,一般存储体积大,不需要备份的非重要数据

                 5  Library/Preference:保存应用的所有偏好设置,iOS的Setting(设置)应用会在该目录中查找应用的设置信息,iTunes同步设备时会备份该目录  

    - plist存储

     (1)#import
"ViewController.h"

                
#import "Person.h"

                
@interface
ViewController ()

                 @end

                
@implementation ViewController

                
- (void)viewDidLoad {

                     [super
viewDidLoad];

                  
  // Do any additional setup after loading the view, typically from a nib.

                 }

                
- (IBAction)save:(id)sender { 

                    
//第一个参数:搜索的目录

                  
 
//第二个参数:搜索的范围

                  
 
//第三个参数:是否展开路径(在ios当中识别~)

                    
NSString *path =
NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES)[0];

                  
  NSLog(@"%@",path);

    

                  
 
//拼接一个文件名

  
                  NSString *filePath = [path
stringByAppendingString:@"data.plist"];

                   
///Users/xieyang/Library/Developer/CoreSimulator/Devices/5D7C40BE-CCF7-499D-87B3-E17D9A93798D/data/Containers/Data/Application/6BED8552-9E13-4300-9154-3741F2CF92C4/Documentsdata.plist

                
//stringByAppendingPathComponent拼接一个文件路径.自动加一个(/)

                    
NSString *filePath = [path stringByAppendingPathComponent:@"data.plist"];

                    
NSLog(@"%@",filePath);

    

                    
//把数组保存到沙盒

                    
NSArray *dataArray = 
@[@"xmg",@10];

                    
//路径是沙盒路径

  
                  [dataArray writeToFile:filePath
atomically:YES];

    

    

  
                  Person *per = [[Person
alloc]
init];

                     per.name =
@"xmg";

                     per.age
= 10;

                   
 

                    
//在plist文件当中是不能够保存自定义的对象

  
                  NSDictionary *dict =
@{@"name" :
@"xmg",@"age" :
@10};

  
                  NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,
YES)[0];

                    
NSString *dictPath = [path
stringByAppendingPathComponent:@"dict.plist"];

  
                  [dict writeToFile:dictPath
atomically:YES];

    

                 }

                
- (IBAction)read:(id)sender {

    

                  
  NSString *path =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)[0];

                  
  NSString *filePath = [path
stringByAppendingPathComponent:@"data.plist"];

                  
  NSArray *dataArray = [NSArray
arrayWithContentsOfFile:filePath];

                  
  NSLog(@"%@",dataArray);

    

                  
  NSString *path =
NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES)[0];

                    
NSString *filePath = [path
stringByAppendingPathComponent:@"dict.plist"];

                    
NSDictionary *dict = [NSDictionary
dictionaryWithContentsOfFile:filePath];

                    
NSLog(@"%@",dict);

    

                 }

                
- (void)didReceiveMemoryWarning {

                     [super
didReceiveMemoryWarning];

                  
  // Dispose of any resources that can be recreated.

                 }

                 @end

    - 偏好设置

    
(1)每个应用都有NSUserDefaults实例,通过它来存取偏好设置

     (2)- (IBAction)save:(id)sender
{

                  
  //NSUserDefaults它保存也是一个plist.

                  
  NSUserDefaults *defaults = [NSUserDefaults
standardUserDefaults];

                  
  [defaults setObject:@"xmg"
forKey:@"name"];

                  
  [defaults setInteger:10
forKey:@"age"];

                    
//立马写入到文件当中

                     [defaults
synchronize];

                    
NSLog(@"%@",NSHomeDirectory());

    

                 }

                
- (IBAction)read:(id)sender {

    

                  
  NSUserDefaults *defaults = [NSUserDefaults
standardUserDefaults];

                  
  NSString *name = [defaults
objectForKey:@"name"];

                  
  NSLog(@"name ===%@",name);

  
                  NSInteger age = [defaults
integerForKey:@"age"];

                    
NSLog(@"%ld",age);

                 }

    - 归档

     (1)- (IBAction)save:(id)sender
{   

                  
  Person *per =  [[Person
alloc]
init];

                  
  per.name =
@"xmg";

                  
  per.age =
10;

    

                  
  Dog *dog = [[Dog
alloc]
init];

                  
  dog.name =
@"wangcai";

                  
  per.dog = dog;

      

                    
//获取沙盒目录

                  
  NSString *tempPath = 
NSTemporaryDirectory();

                  
  NSString *filePath = [tempPath
stringByAppendingPathComponent:@"Person.data"];

                  
  NSLog(@"%@",tempPath);

    

                    
//归档 archiveRootObject会调用encodeWithCoder:

                     [NSKeyedArchiver
archiveRootObject:per toFile:filePath];

                 }

                
- (IBAction)read:(id)sender { 

                    
//获取沙盒目录

                    
NSString *tempPath = 
NSTemporaryDirectory();

                  
  NSString *filePath = [tempPath
stringByAppendingPathComponent:@"Person.data"];

                  
  //unarchiveObjectWithFile会调用initWithCoder

                  
  Person *per = [NSKeyedUnarchiver
unarchiveObjectWithFile:filePath];

                    
NSLog(@"%@---%@",per.name,per.dog.name);

                 }

            
#import "Person.h"

                
@implementation Person

                 //在保存对象时告诉要保存当前对象哪些属性.

                
-(void)encodeWithCoder:(NSCoder *)aCoder{

                     [aCoder
encodeObject:self.name
forKey:@"name"];

  
                  [aCoder encodeInt:self.age
forKey:@"age"];

  
                  [aCoder encodeObject:self.dog
forKey:@"dog"];   

                 }

                 //当解析一个文件的时候调用.(告诉当前要解析文件当中哪些属性.)

                
- (instancetype)initWithCoder:(NSCoder *)aDecoder{

    

                    
//当只有遵守了NSCoding协议时,才有[super initWithCoder]

                  
  if (self = [super
init]) {

        

   
                    self.name = [aDecoder
decodeObjectForKey:@"name"];

                   
    self.age = [aDecoder
decodeIntForKey:@"age"];

                     
  self.dog = [aDecoder
decodeObjectForKey:@"dog"];

                     }

                    
return
self;

                 }

                 @end

                 #import
"Dog.h"

                
@implementation Dog

                
-(void)encodeWithCoder:(NSCoder *)aCoder{

    

                     [aCoder
encodeObject:self.name
forKey:@"name"];

                 }

                
-(instancetype)initWithCoder:(NSCoder *)aDecoder{

    

                  
  if (self = [super
init]) {

                  
      self.name = [aDecoder
decodeObjectForKey:@"name"];

                  
  }

                  
 
return
self;

                 }

                 @end

    - UINavigationBar补充

     (1)凡是在导航条下面的scrollView.默认会设置偏移量.UIEdgeInsetsMake(64,
0, 0, 0)

                
self.tableView.contentInset =
UIEdgeInsetsMake(64,
0,
0,
0);

                 //不要自动设置偏移量

                
self.automaticallyAdjustsScrollViewInsets =
NO;

    
(2)让导航条隐藏

                
self.navigationController.navigationBar.hidden =
YES;

    
(3)导航条或者是导航条上的控件设置透明度是没有效果.

                
self.navigationController.navigationBar.alpha =
0;

     (4)设置导航条背景(必须得要使用默认的模式UIBarMetricsDefault)

                 当背景图片设置为Nil,系统会自动生成一张半透明的图片,设置为导航条背景

                 //设置透明

                
[self.navigationController.navigationBar
setBackgroundImage:[[UIImage
alloc] init]
forBarMetrics:UIBarMetricsDefault];

                
[self.navigationController.navigationBar
setShadowImage:[[UIImage
alloc]
init]];

    - UITabBarController

    
(1)基本使用

                
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   

                  
 
//1 创建窗口

                  
  self.window = [[UIWindow
alloc] initWithFrame:[UIScreen
mainScreen].bounds];

                    
self.window.backgroundColor = [UIColor
blueColor];

                     //2
设置窗口的根控制器

                     //UITabBarController默认显示的是第一个子控制器的View.

                  
  UITabBarController *tabVC = [[UITabBarController
alloc]
init];

                     //创建控制器

                    
UIViewController *vc1 = [[UIViewController
alloc]
init];

                  
  vc1.view.backgroundColor = [UIColor
redColor];

                  
  UINavigationController *nav = [[UINavigationController
alloc]
initWithRootViewController:vc1];

                  
  nav.tabBarItem.title  =
@"消息";

                  
  nav.tabBarItem.badgeValue =
@"10";

                  
  nav.tabBarItem.image = [UIImage
imageNamed:@"tab_recent_nor"];

                  
 
//添加子控制器

                     [tabVC
addChildViewController:nav];

   

                    
//创建控制器

                    
UIViewController *vc2 = [[UIViewController
alloc]
init];

                  
  vc2.view.backgroundColor = [UIColor
blueColor];

                  
  vc2.tabBarItem.title =
@"联系人";

                  
  vc2.tabBarItem.image = [UIImage
imageNamed:@"tab_buddy_nor"];

                  
  [tabVC addChildViewController:vc2];

                    
UIViewController *vc3 = [[UIViewController
alloc]
init];

                     vc3.view.backgroundColor
= [UIColor greenColor];

                     vc3.tabBarItem.title
= @"动态";

                     vc3.tabBarItem.image
= [UIImage imageNamed:@"tab_qworld_nor"];

                     [tabVC
addChildViewController:vc3];

                     //选中某一个子控制器

                     tabVC.selectedIndex
= 2;

                    
self.window.rootViewController = tabVC;

                    
//3 显示窗口

                  
  [self.window
makeKeyAndVisible];

                    
return
YES;

                 }

    - Modal

     (1)- (IBAction)modal:(id)sender
{

                  
  TwoViewController *twoVC = [[TwoViewController
alloc]
init];

                  
 
//当在modal时,会把窗口上面的View给移除,然后要modal控制器的view,给添加到窗口上

                  
 
//如果当一个控制器被销毁,那么它View的业务逻辑是没有办法处理

                  
 
//控制器被销毁,控制器的View不一定被销毁(只要有强指针指向它,就不会被销毁) 

                    
//[self presentViewController:twoVC animated:YES completion:^{
}];   

  
                  CGRect frame = twoVC.view.frame;

                     frame.origin.y
= [UIScreen mainScreen].bounds.size.height;

  
                  twoVC.view.frame = frame;

                     [[UIApplication
sharedApplication].keyWindow
addSubview:twoVC.view];

                    
self.twoVC = twoVC;

                     [UIView
animateWithDuration:0.5
animations:^{   

  
                      twoVC.view.frame =
self.view.frame;

  
                  }completion:^(BOOL finished) {

                         [self.view
removeFromSuperview];

                  
  }];

                 }

     (2)- (IBAction)dismiss:(id)sender
{

                     [self
dismissViewControllerAnimated:YES
completion:nil];  

                 }    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Objective-C OC ui