iOS基石—— 傳值

1.正向傳值 — 屬性傳值/方法傳值

SecondViewController *secondVC = [[SecondViewController alloc]init];
NSString *str = @"hello";

2.反向傳值

     1.代理
//主動方 要傳遞值的一方
    //1.聲明協議
    @protocol giveDataDelegate <NSObject>
    //2.擬定一個方法  被動方需要什麼 參數就是什麼
    - (void)giveDataFrom:(NSString *)str andColor:(UIColor *)color;
    @end
    @interface NextViewController : UIViewController
    //3.聲明一個持有協議的id指針  屬性修飾符weak/assign
    @property (nonatomic,weak) id<giveDataDelegate>delegate;
     //傳遞數值
    [self.delegate giveDataFrom:@"I AM BACK" andColor:[UIColor yellowColor]];

//接收數據方
//1.遵守協議
     @interface ViewController ()<giveDataDelegate>
//2.實現協議方法
    - (void)giveDataFrom:(NSString *)str andColor:(UIColor *)color{
    //主動方要求被動方要做的事情
    …
    }
//3.設置代理
    nextVC.delegate = self;

     2.block
 //主動方 要傳遞值的一方
    //typedef 定義一個新的類型 名字ColorBlock
        typedef void(^ColorBlock)(NSString *,UIColor *);
        @property (nonatomic,copy) ColorBlock block;

  //接收數據方
    NextViewController *nextVC = [[NextViewController alloc]init];
    nextVC.block = ^(NSString *str , UIColor *color){
        //主動方讓被動方做的事情
        //數據回調回來之後 需要處理的事情
        …
    };

3.使用單例傳值

+(SingleClass *)defaultSingle{
    //首先需要一個靜態變量  初始值是nil
    static SingleClass *obj = nil;//可以拿到上一次的值  //當第二次....第n次調用這個方法 obj值就是第一次創建的對象的指針xxxxx000000  靜態變量相當於全局變量 程序退出時靜態變量纔會消失  也可以放在外面
    //判斷
    if (obj == nil) {
        ///如果爲空 創建對象
        obj = [[SingleClass alloc]init];
    }
    return obj;//xxxxx000000
}

4.通知中心

//發送數據方
//通知中心 只有一個 --- 單例對象
//拿到通知中心
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

//發佈通知(發送消息)
[center postNotificationName:@"頻道1" object:nil userInfo:@{@"label":@"爸爸回來了"}];

//[center postNotificationName:@"頻道1" object:@"爸爸去哪了"];
//要傳遞的數據設置成字典的value

//接收數據方
//拿到通知中心
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

//註冊監聽者(觀察者)
[center addObserver:self selector:@selector(changeLabel:) name:@"頻道1" object:nil];//object通常是nil
//Observer 通過 name(頻道) 接收數據 接收成功調用selector

- (void)changeLabel:(NSNotification *)noti{
NSLog(@"第二個界面:名字 %@ 發佈者 %@ 數據 %@",noti.name,noti.object,noti.userInfo);
UILabel *label = (id)[self.view viewWithTag:200];
label.text = noti.userInfo[@"label"];
}
//移除掉所有監聽器
[[NSNotificationCenter defaultCenter] removeObserver:self];


實例:鍵盤收放

    —系統自動發佈通知   

//註冊  監聽鍵盤彈起的時候
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//註冊  監聽鍵盤落下的時候
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];

//鍵盤將要彈起
- (void)keyboardWillShow:(NSNotification *)noti{

    //拿到鍵盤彈起的時間  時間是秒
    NSTimeInterval interval = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    // NSNumber of double

    //拿到鍵盤彈起後的Rect
    NSValue *value = noti.userInfo[UIKeyboardFrameEndUserInfoKey];
    //NSValue of CGRect
    //非對象類型用getValue來提取
    CGRect rect = [value CGRectValue];


    [UIView animateWithDuration:interval animations:^{

        UIView *redView = (id)[self.view viewWithTag:100];
        CGRect redRect = redView.frame;
        //屏幕高度 - 鍵盤高度 - 自身高度
        redRect.origin.y = [UIScreen mainScreen].bounds.size.height - rect.size.height - redRect.size.height;
        redView.frame = redRect;
    }];

}
  1. 單例對象
    uiwindows
    // AppDelegate 也是一個單例
    // NSFileManager //文件管理
    // NSNotificationCenter //通知中心
    // UIApplication
    // NSUserDefaults //應用程序設置
    // NSURLCache //url緩存
    // NSHTTPCookieStorage 應用程序cookies池
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章