ios 控件   UIKit.framework

1.  IOS 認識

  1.info.plist:
     Bundle name:  應用名稱
     Bundle Identifirer:  應用唯一標識

// 獲取 Info.plist 路徑
   NSString* filepath=  [[NSBundle mainBundle] pathForResource:@"Info.plist" ofType:nil];
   // 獲取配置文件,保存字典中
    NSDictionary* dict= [NSDictionary dictionaryWithContentsOfFile:filepath];
    // 獲取當前版本號
    NSLog(@"dict=%@",dict[@"CFBundleShortVersionString

 2. UiApplication的常用屬性: 
    設置應用程序圖標
    設置狀態欄
    打開網頁、發短信、發郵件、打電話

3. UIApllication的 delegate 代理:
   1.  應用程序生命週期
   2.  系統事件
   3.  內存警告
https://www.jianshu.com/p/5b7c2e72a5f6

 2.  IOS 基礎控件   UIKit.framework

UiKit座標系:  [選中控件,屬性面板第6個]
1. 控件座標是相對座標: 現對於父控件的位置,每一個子控件
2. 左上角是原點 

1. 按下 option拖,複製控件
2. xcode11 拖線找不到控制器: https://blog.csdn.net/qq_20255275/article/details/102784747   
Touch up inside 事件,點擊事件,拖入頭文件中
屬性連線 :Referencing Outlets,拖入匿名分類中即可
3.刪除方法的時候,把連線的方法也刪除


   UIKit.frework  
1. 按下 option拖,複製控件
2. xcode11 拖線找不到控制器: https://blog.csdn.net/qq_20255275/article/details/102784747   
Touch up inside 事件,點擊事件,拖入頭文件中
屬性連線 :Referencing Outlets,拖入匿名分類中即可
3.刪除方法的時候,把連線的方法也刪除


UiKit座標系:  [選中控件,屬性面板第6個]
1. 控件座標是相對座標: 現對於父控件的位置,每一個子控件
2. 左上角是原點 

1. 案例1   求和, 如何拖控件、點擊方法
2. 案例2 設置隨機顏色
3. 動態生成View
4. 案例4:動畫
5. 案例5: 按鈕創建
6. 基本屬性控制api : 擴大、旋轉、縮放、平移
7 : 刪除指定tag  控件 、  刪除父控件下所有子控件

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

// 方法拖入頭文件即可,點擊事件
- (IBAction)jisuang:(id)sender;


@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
// 私有屬性 1
@property (weak, nonatomic) IBOutlet UITextField *text1;
// 私有屬性 2
@property (weak, nonatomic) IBOutlet UITextField *text2;
// 求和
@property (weak, nonatomic) IBOutlet UILabel *sumText;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (IBAction)jisuang:(UIButton* )sender {
    NSDate* now = [NSDate new];
    NSDateFormatter* nsformater= [NSDateFormatter new];
        nsformater.dateFormat=@"yyyy-MM-dd HH:mm:ss";
    NSString* str= [nsformater stringFromDate:now];
    // 獲取控件1  控件2的 文本值
    int text1= [self.text1.text intValue];
    int text2= [self.text2.text intValue];
    
    int sum =text1 + text2;
    // 1. 案例1   求和
    [self.sumText setText:[NSString stringWithFormat:@"%d",sum]];
    
    //  取消控制器View的編輯狀態
    //  隱藏鍵盤
    [self.view endEditing:YES];
    
    // 2. 案例2 設置隨機顏色
    // 獲取 父元素
    UIView* father= sender.superview;
    // 生成隨機數
    float randomR= arc4random_uniform(255)/255.0;
    float randomG= arc4random_uniform(255)/255.0;
    float randomB= arc4random_uniform(255)/255.0;
    // 隨機顏色,這裏參數是[0,1]
    UIColor* randomColor= [UIColor colorWithRed:randomR green:randomG blue:randomB alpha:1];

    father.backgroundColor= randomColor;
    NSLog(@"hel---------%@",str);
    
    // 3. 動態生成View
    UIView* addView= [UIView new];
    // 設置控件位置以及尺寸
    addView.frame= CGRectMake(0, 0, 30, 30);
    addView.backgroundColor=[UIColor redColor];
    // 添加控件
    [father addSubview:addView];
    
    // 案例4:動畫
    // 不能直接修改frame的 值
  //  addView.frame.origin.x=230;
    CGRect oldFrame = addView.frame;
    oldFrame.origin.x= 100;
    oldFrame.origin.y= 100;
    // 動畫方式1: 頭尾式動畫, ios13 已經廢棄
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:10];
//    [UIView setAnimationDelay:2];
//    // 賦值回去
//    addView.frame=oldFrame;
//    [UIView commitAnimations];
    
    // 動畫方式2:
    [UIView animateWithDuration:3 animations:^{
        // 動畫要改變的值
        addView.frame= oldFrame;
    }];
    
    
    // 案例5: 按鈕創建
    UIButton* button= [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(30, 30, 50, 50);
    /**
    UIButton控件狀態:
      default
      Highighted: 點擊
      Selected: 選擇
      Disabled: 禁用狀態
     */
    // 枚舉  提示按下 esc 鍵
    [button setTitle:@"點我" forState:UIControlStateNormal];
    UIImage* image= [UIImage imageNamed:@"btn_01.png"];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    
    [father addSubview:button];
    // 使用代碼添加點擊事件
    [button addTarget:self action:@selector(doSomeThings) forControlEvents:UIControlEventTouchUpInside
     ];
    
    
}

// 案例6: 基本屬性控制api   擴大、旋轉、縮放、平移
- (IBAction)startAnimation:(id)sender {
      
//    CGRect oldFrame = self.donghuaImage.frame;
//    // 中心點擴大
//    oldFrame.size.height= oldFrame.size.height+20;
//    oldFrame.size.width = oldFrame.size.height+20;
//    oldFrame.origin.x = oldFrame.origin.x -10 ;
//    oldFrame.origin.y = oldFrame.origin.y -10 ;
    // self.donghuaImage.frame= oldFrame;
    
    
    //使用 transfrom 屬性來設置按鈕 旋轉 , 旋轉必須是弧度
  //  self.donghuaImage.transform= CGAffineTransformMakeRotation(M_PI_4);
    // 累加 旋轉 , 在原始值的基礎上累加 self.donghuaImage.transform
    self.donghuaImage.transform= CGAffineTransformRotate(self.donghuaImage.transform, M_PI_4);
    
    
 //   CGAffineTransformMake(<#CGFloat a#>, <#CGFloat b#>, <#CGFloat c#>, <#CGFloat d#>, <#CGFloat tx#>, <#CGFloat ty#>)   // 參數最多,定製最強
  //  CGAffineTransformMakeRotation(<#CGFloat angle#>)  // 旋轉
  //  CGAffineTransformMakeScale(CGFloat sx, <#CGFloat sy#>)   縮放
  //   CGAffineTransformTranslate(<#CGAffineTransform t#>, <#CGFloat tx#>, <#CGFloat ty#>)  // 平移
}


//案例7 : 刪除指定tag  控件 、  刪除父控件下所有子控件
- (IBAction)clickBtn:(id)sender {
    // 刪除指定tag  控件
    UIButton* btn10= [self.whiteView viewWithTag:10];
  //  [btn10 removeFromSuperview];
    
    // 刪除所有子控件
    for (UIView* view in self.whiteView.subviews) {
//        if( [view isKindOfClass: [UIButton class]]){
//            continue;
//        }
        [view removeFromSuperview];
    }
}

-(void)doSomeThings{
    NSLog(@"點擊按鈕");
}
@end

效果:

3.   幀動畫

UIimage imageName 與 imageWithContentsOfFile 區別,播放幀動畫



#import "HomeController.h"

@interface HomeController ()
@property (weak, nonatomic) IBOutlet UIImageView *animation;

@end

@implementation HomeController

/**
 播放幀動畫:
 1.  UIimage imageName:圖片名,  這種方式加載圖片  會在內存中常駐 ,一般用於背景圖片,小箭頭  icon等等,  第一次加載以後,後面使用直接讀取緩存中
    對於需要釋放的圖片  使用 imageWithContentsOfFile  加載圖片,只有當沒有任何一種對象對他進行 強引用的時候,纔會釋放
  
 2.   項目中,  如果是幀動畫,需要播放完以後釋放,  使用  imageWithContentsOfFile
 不做緩存
 
 */


// 幀動畫  播放方式1:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
//    if([self.animation isAnimating]){
//        return;
//    };
//
//    NSMutableArray * arry=[NSMutableArray array];
//    for (int i=0; i< 26 ; i++) {
//     // 1. 拼接圖片名字
//        NSString* imageNameStr= [NSString stringWithFormat:@"angry_%02d",i];
//
//        NSLog(@"lujing==%@",imageNameStr);
//
//        // 2. 加載圖片
//       UIImage* image= [UIImage imageNamed:imageNameStr];
//
//       [arry addObject:image];
//    }
//
//    // 把加載好的圖片 設置給UIIMageView
//    self.animation.animationImages= arry;
//           // 開始播放動畫
//
//    // 設置播放動畫的細節
//    self.animation.animationDuration=2;
//    self.animation.animationRepeatCount=2;
//
//           [self.animation startAnimating];
    
}


// 幀動畫   播放方式 2
- (IBAction)clickAnimation:(id)sender {
    
    if([self.animation isAnimating]){
        return;
    };
    
    NSMutableArray * arry=[NSMutableArray array];
       for (int i=1 ; i<40 ; i++) {
        // 1. 拼接圖片名字
           NSBundle* mainBund=[NSBundle mainBundle];
           // 獲取 項目 下 沙盒路徑
 // 可以把圖片拷貝到項目根目錄下,項目根目錄下可以有文件夾,查看沙盒的時候文件夾會去掉
           NSString* imageNameStr= [NSString stringWithFormat:@"gun%03d.png",i];
           NSString* imagePath =[ mainBund pathForResource:imageNameStr ofType: nil];
           
           NSLog(@"lujing==%@",imagePath);
           
           // 2. 加載圖片a
    UIImage* image= [UIImage imageWithContentsOfFile:imagePath];
           [arry addObject:image];
       }
    
    self.animation.animationImages= arry;
    
    self.animation.animationDuration=40*0.1;
      self.animation.animationRepeatCount=2;
      
             [self.animation startAnimating];
      // 播放完成以後釋放 引用,纔可以釋放內存
    [self performSelector:@selector(cleanImage) withObject:nil afterDelay:40*0.1*2];
    
    
}

// 清除動畫引用 
-(void)cleanImage{
    self.animation.animationImages=nil;
}

@end

4.  九宮格實現

4.1.  通過代碼方式實現九宮格

#import "Home17Controller.h"
#import "ItemLogin.h"
#import "AppModel.h"

@interface Home17Controller ()

//  都聲明爲可變的,否則無法add 添加元素
@property(nonatomic,strong) NSMutableArray* dataArray;

@end


@implementation Home17Controller

// 1. 重寫get 方法從網絡獲取數據,懶加載避免多次調用
-(NSMutableArray*)dataArray{
    if(_dataArray == nil){
//       NSString* path= [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
//       _dataArray= [NSMutableArray arrayWithContentsOfFile:path];
        _dataArray= [NSMutableArray new];
        for (int i=0; i<12; i++) {
            AppModel* appModel= [AppModel new];
            appModel.lableName=[NSString stringWithFormat:@"xiao%d",i];
            [_dataArray addObject:appModel];
        }
       
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    CGFloat yellowViewWidth= 80;
    CGFloat yellowViewHeight= 90;
    
    CGFloat kColumn =3;
    
    CGFloat margin= (self.view.frame.size.width - kColumn* yellowViewWidth)/ (kColumn+1);
    
    for (int j=0; j< 4; j++) {  // 確定行
        for (int i=0; i< kColumn; i++) {  // 確定列
            // 每一個View的 x 、y 座標
            CGFloat yellowViewX= (i+1) * margin + i*yellowViewWidth;
            CGFloat yellowViewY= (j+1) * margin + j*yellowViewHeight;
       // 外部Item對應的View
               UIView* itemView= [[UIView alloc] initWithFrame:CGRectMake(yellowViewX, yellowViewY, yellowViewWidth, yellowViewHeight)];
            
            NSLog(@"%f,%f",yellowViewX,yellowViewY);
            itemView.backgroundColor=[UIColor blueColor];

            [self.view addSubview:  itemView];

            CGFloat imageWidth= 45;
            CGFloat topY = 10;
            // 1. 添加ImageView
            CGFloat imageViewX= (yellowViewWidth- imageWidth )/2;
            UIImageView* iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageViewX, topY, imageWidth, imageWidth)];
            [iconImage setBackgroundColor:[UIColor redColor]];

            [itemView addSubview:iconImage];

            // 2.添加lable, 獲取y 座標
            CGFloat labelY = CGRectGetMaxY(iconImage.frame);

            UILabel* label= [[UILabel alloc] initWithFrame:CGRectMake(0, labelY, itemView.frame.size.width, 15)];

            // label.backgroundColor=[UIColor blackColor];

            label.textAlignment= NSTextAlignmentCenter;
            label.text=[NSString stringWithFormat:@"a%d",i];

            // 加粗
         //   label.font= [UIFont systemFontOfSize:20];
            label.font= [UIFont boldSystemFontOfSize:20];
            [itemView addSubview:label];

            //3.  添加button
            CGFloat buttonX= ( itemView.frame.size.width-imageWidth)/2;
            CGFloat buttonY= CGRectGetMaxY(label.frame);
            UIButton* downloadButton= [[UIButton alloc] initWithFrame:CGRectMake(buttonX, buttonY, imageWidth, 20)];

            // 默認狀態
            [downloadButton setBackgroundImage:[UIImage imageNamed:@"buttongreen.png"]
                                      forState:UIControlStateNormal];

            // 點擊按下狀態
              [downloadButton setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted.png"] forState:UIControlStateHighlighted];

            [downloadButton setTitle:@"下載" forState:UIControlStateNormal];
            [itemView addSubview:downloadButton];
           
        }
    }
}


@end

4.2.  通過xib 的方式 實現

#import "Home17Controller.h"
#import "ItemLogin.h"
#import "AppModel.h"

@interface Home17Controller ()

//  都聲明爲可變的,否則無法add 添加元素
@property(nonatomic,strong) NSMutableArray* dataArray;
@end


@implementation Home17Controller

// 1. 重寫get 方法從網絡獲取數據,懶加載避免多次調用
-(NSMutableArray*)dataArray{
    if(_dataArray == nil){
//       NSString* path= [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
//       _dataArray= [NSMutableArray arrayWithContentsOfFile:path];
        _dataArray= [NSMutableArray new];
        for (int i=0; i<12; i++) {
            AppModel* appModel= [AppModel new];
            appModel.lableName=[NSString stringWithFormat:@"xiao%d",i];
            [_dataArray addObject:appModel];
        }
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    CGFloat yellowViewWidth= 80;
    CGFloat yellowViewHeight= 90;
    
    CGFloat kColumn =3;
    
    CGFloat margin= (self.view.frame.size.width - kColumn* yellowViewWidth)/ (kColumn+1);
    
    for (int j=0; j< 4; j++) {  // 確定行
        for (int i=0; i< kColumn; i++) {  // 確定列
            // 每一個View的 x 、y 座標
            CGFloat yellowViewX= (i+1) * margin + i*yellowViewWidth;
            CGFloat yellowViewY= (j+1) * margin + j*yellowViewHeight;
       
            // 一個xib中可能有多個UIView,返回UIView
                   // 安裝以後最終應用程序以nib結尾,所以loadNibName
            NSArray* itemXib= [[NSBundle mainBundle] loadNibNamed:@"itemLogin" owner:nil options:nil];
                    ItemLogin* xibView1= [itemXib firstObject];
                    int index= j* i + i;
                    NSLog(@"array:%@",self.dataArray);
                                          
                  //  設置model
                    xibView1.appModel = self.dataArray[index];
        
                    NSLog(@"---%@",xibView1);
                    [xibView1 setFrame:CGRectMake(yellowViewX, yellowViewY, yellowViewWidth,yellowViewHeight)];
                    [self.view addSubview:xibView1];
           
        }
    }
}
@end

Xib 佈局實現

設置freedom,不是默認的viewctroller視圖

 

xib 對應 ItemLogin.h 設置屬性,用於設置xib 控件數據


#import <UIKit/UIKit.h>

@class AppModel;

NS_ASSUME_NONNULL_BEGIN

@interface ItemLogin : UIView

@property(nonatomic,copy) AppModel* appModel;

@end

NS_ASSUME_NONNULL_END

ItemLogin.m

#import "ItemLogin.h"
#import "AppModel.h"

@interface   ItemLogin()
@property (weak, nonatomic) IBOutlet UIImageView *iconImgeView;

@property (weak, nonatomic) IBOutlet UILabel *lableText;

@end


@implementation ItemLogin

-(void)setAppModel:(AppModel *)appModel{
    
    _appModel= appModel;
    
    _lableText.text= _appModel.lableName;
    
};

@end

5.  對話框的使用

 // 對話框顯示
- (IBAction)clickMe:(id)sender {
    //  UIAlertControllerStyleAlert: 在屏幕中間顯示
   // UIAlertControllerStyleActionSheet: 從下面往上面顯示
    //  1. 實例化UIAlertController
    UIAlertController*  alertController= [UIAlertController alertControllerWithTitle:@"標題" message:@"內容" preferredStyle:UIAlertControllerStyleAlert];
   //2. 添加按鈕
    UIAlertAction* cancleAction= [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:cancleAction];
    //UIAlertActionStyleDefault
    // UIAlertActionStyleDestructive : 按鈕是紅色的
    UIAlertAction* sureAction= [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:sureAction];
    
    //3.  顯示
    [self presentViewController:alertController animated:YES completion:^{
        
    }];
}

6.  代理使用

FouterView.h: 

#import <UIKit/UIKit.h>
@class FooterView;
 // 1.定義協議
@protocol FooterViewDelegate <NSObject>
- (void)footerView:(FooterView *)footerView;
@end


@interface FooterView : UIView
  // 2. 代理屬性
@property (nonatomic, weak) id<FooterViewDelegate> delegate;

@end

 FouterView.m:  

- (IBAction)didClickLoadButton:(id)sender {
// 3. 通知代理(控制器), 插入新的數據
    if ([self.delegate respondsToSelector:@selector(footerView:)]) {
        [self.delegate footerView:self];
    }
}

ViewController.m

@interface ViewController ()<UITableViewDataSource,FooterViewDelegate>
- (void)viewDidLoad {

  FooterView *footerView = [[[NSBundle mainBundle] loadNibNamed:@"FooterView" owner:nil options:nil] lastObject];
   // 4. 設置控制器成爲footterView的代理
    footerView.delegate = self;
}

// 5. 代理回調,實現代理接口
 - (void)footerView:(FooterView *)footerView {

 }
@end 

 7.   通知

 Objective-c 通知原理:             發佈通知                                          

                                                                   觀察者1 
  被觀察者   ----------->通知中心    ---- > 觀察者2
                                                                   觀察者3


   1. 觀察者 註冊通知到通知中心 
 2. 被觀察者 發佈通知通過 通知 中心
 3. 當 觀察者 對象 被銷燬的 時候,
一定要從 通知中心 把他 給移除掉

案例:通知下載
DownLoadManager.h        DownLoadManager.m

#import <Foundation/Foundation.h>


// 觀察者

@interface DownLoadManager : NSObject

@property(nonatomic,copy) NSString* name;


-(void) receiveNotifiction :(NSNotification*) noti;

@end

 

#import "DownLoadManager.h"

@implementation DownLoadManager


-(void) receiveNotifiction :(NSNotification*) noti{
    
    NSLog(@"%@",noti);
    
    NSDictionary* dict= noti.userInfo;
    
    DownLoadManager* down= dict[@"manager"];
    
    NSLog(@"名稱:%@,下載進度%@", down.name, dict[@"degree"]);
    
    
}


// 3. 從 一旦 觀察者掛了,從通知中心 移除
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

@end

DownLoadDetail.h   DownLoadDetail.m

#import <Foundation/Foundation.h>


 // 觀察者
@interface DownLoadDetail : NSObject

@property(nonatomic,copy) NSString* name;

-(void) receiveNotifiction :(NSNotification*) noti;

@end


#import "DownLoadDetail.h"

@implementation DownLoadDetail


-(void) receiveNotifiction :(NSNotification*) noti{
    NSLog(@"%@",noti);
    
    NSDictionary* dict= noti.userInfo;
    
    DownLoadDetail* down= dict[@"manager"];
    
    NSLog(@"名稱:%@,下載進度%@", down.name, dict[@"degree"]);  
}
// 3. 從 一旦 觀察者掛了,從通知中心 移除
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

@end

main.m



#import <Foundation/Foundation.h>

#import "DownLoadItem.h"
#import "DownLoadManager.h"
#import "DownLoadDetail.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //1.  觀察者註冊  給被觀察者 , 觀察者 重寫通知方法
        
        /*
         addObserver : 觀察者
         selector : 接收到通知的時候,觀察者調用監聽者的方法
         name :  被觀察者通知的名稱, 如果系統通知,系統已經定義
         object :被觀察者實例對象,可以傳遞爲 nil
         
         原理:通知 中心發送通知,被被觀察者 根據name 接收
         如果 name 爲nil ,那麼 所有的通知都可以 接收
         */
        
        DownLoadManager* downloadManager= [[ DownLoadManager alloc] init];
              downloadManager.name=@"我是觀察者,下載列表界面";
        [[ NSNotificationCenter defaultCenter] addObserver:downloadManager selector:@selector(receiveNotifiction:) name:@"downloadManager" object:downloadManager];
        
        // 下載詳情界面
        DownLoadDetail* downloadDetail= [[ DownLoadDetail alloc] init];
              downloadDetail.name=@"我是觀察者,下載詳情界面";
        [[ NSNotificationCenter defaultCenter] addObserver:downloadDetail selector:@selector(receiveNotifiction:) name:@"downloadDetail" object:downloadDetail];
        
        // 2.  被觀察者 發佈通知,根據name 發送,name 唯一標識
        /**
        postNotificationNam : 被觀察者名稱,必須和註冊的時候名字保持一致
         object : 消息的發佈者
         userInfo : 自定義的消息
         */
        
        [[ NSNotificationCenter defaultCenter]  postNotificationName:@"downloadManager" object:downloadManager userInfo:@{@"manager":downloadManager,@"degree":@(50)}];
        
       [[ NSNotificationCenter defaultCenter]  postNotificationName:@"downloadDetail" object:downloadDetail userInfo:@{@"manager":downloadDetail,@"degree":@(150)}];
        
      
    
    }
    return 0;
}

輸出結果: 

2020-05-31 19:55:54.736225+0800 ObjectiveC通知[6865:328909] NSConcreteNotification 0x100613360 {name = downloadManager; object = <DownLoadManager: 0x100617210>; userInfo = {
    degree = 50;
    manager = "<DownLoadManager: 0x100617210>";
}}
2020-05-31 19:55:54.736605+0800 ObjectiveC通知[6865:328909] 名稱:我是觀察者,下載列表界面,下載進度50
2020-05-31 19:55:54.737077+0800 ObjectiveC通知[6865:328909] NSConcreteNotification 0x10051d200 {name = downloadDetail; object = <DownLoadDetail: 0x1006177a0>; userInfo = {
    degree = 150;
    manager = "<DownLoadDetail: 0x1006177a0>";
}}
2020-05-31 19:55:54.737166+0800 ObjectiveC通知[6865:328909] 名稱:我是觀察者,下載詳情界面,下載進度150
Program ended with exit code: 0


 實際應用: 
     鍵盤監聽: 被觀察者,系統定義


#import "NotificationController.h"

@interface NotificationController ()

@end

@implementation NotificationController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillApper:) name:UIKeyboardWillShowNotification object:nil];
    
}
//UIKeyboardWillShowNotification  鍵盤即將出現通知
-(void)keyboardWillApper:(NSNotification* ) noti{
    NSLog(@"%@",noti);
 /***
輸出結果 :
  NSConcreteNotification 0x6000030b7b40 {name = UIKeyboardWillShowNotification; userInfo = {
      UIKeyboardAnimationCurveUserInfoKey = 7;                 動畫頻率
      UIKeyboardAnimationDurationUserInfoKey = "0.25";     動畫時間
      UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 271}}";     鍵盤高度
      UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 871.5}";
      UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 600.5}";
      UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 271}}";  沒有彈出時候的位置
      UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 465}, {414, 271}}";  彈出以後位置
      UIKeyboardIsLocalUserInfoKey = 1;
  }}
  */
}


@end

  監聽文本內容變化: 

- (void)viewDidLoad {
    [super viewDidLoad];
// 監聽文本變化通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(valueChaged) name:UITextFieldTextDidChangeNotification object:self.textName];
}
 // 文本內容變化了
-(void)valueChaged{
    NSLog(@"%@",self.textName.text);
}
-(void)dealloc{
  // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

 

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