亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS(閉包)block傳值詳解

 更新時(shí)間:2018年02月12日 09:56:21   投稿:laozhang  
這篇文章主要介紹了iOS(閉包)block傳值的相關(guān)知識(shí)以及代碼分享,對(duì)此有興趣的朋友可以學(xué)習(xí)下。

在iOSAPP開(kāi)發(fā)的過(guò)程中 我們會(huì)用到很多需要傳值的地方 傳值的方式也多種多樣 有:代理傳值、通知傳值、KVC、KVO、block、單例 等。其中block 因?yàn)槠浜?jiǎn)潔實(shí)用規(guī)范的代碼 無(wú)疑是大牛們傳值的不二選擇 但對(duì)于初學(xué)者來(lái)說(shuō)要理解并能運(yùn)用 起初確實(shí)有些難以理解 以下我將細(xì)細(xì)的介紹下block

首先我總結(jié)了一下block的公式:

  1. 步驟1.block 的聲明 返回值類(lèi)型(^block 的名字)(參數(shù)列表);
  2. 步驟2.block 實(shí)現(xiàn) block的名字 = ^(參數(shù)列表)(){};
  3. 步驟3.block 的調(diào)用 block的名字();

下面是一個(gè)簡(jiǎn)單的block

//  聲明 
  void(^blockName)(int num,NSString *string); 
//  實(shí)現(xiàn) 
  blockName = ^(int num,NSString *string) 
  { 
    NSLog(@"%d,%@",num,string); 
  }; 
//  調(diào)用 
  blockName(520,@"phyone_"); 
} 

由該例可以看出block的代碼執(zhí)行順序和其他的代碼執(zhí)行順序不一樣 它是先執(zhí)行聲明代碼 然后執(zhí)行調(diào)用代碼 最后才執(zhí)行實(shí)現(xiàn)代碼 由此我們利用block的回調(diào)性質(zhì) 達(dá)到我們傳值效果 當(dāng)然這只是在一個(gè)頁(yè)面的一段代碼 只是說(shuō)明了它具有傳值的作用 下面我們來(lái)試試 多頁(yè)面之間的block反向傳值 由于是反向傳值 所以我們創(chuàng)建兩個(gè)ViewController (ViewController,Next_ViewController)先由ViewController跳到Next_ViewController里將里面的值反向傳給ViewController
以下是代碼

ViewController.m里面

- (void)viewDidLoad { 
  [super viewDidLoad]; 
  self.view.backgroundColor = [UIColor purpleColor]; 
 UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 100)]; 
  lable.backgroundColor = [UIColor brownColor]; 
  [self.view addSubview:lable]; 
   
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)]; 
  [self.view addGestureRecognizer:tap]; 
} 
- (void)tap 
{ 
  Next_ViewController *NVC = [[Next_ViewController alloc]init]; 
  NVC.block = ^(NSString *content){ 
    NSLog(@"%@",content); 
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 100)]; 
    [button setTitle:content forState:UIControlStateNormal]; 
    button.backgroundColor = [UIColor orangeColor]; 
    [self.view addSubview:button]; 
  }; 
  [self presentViewController:NVC animated:YES completion:nil]; 
} 

Next_ViewController.h里面

//block 是分配在棧里面的用copy 
@property(nonatomic,copy) void(^block)(NSString *content); 

Next_ViewController.m里面

<p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="font-size:12px;">{</span></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="font-size:12px;">  void(^imageNameBlock_1)(NSString *imageName);</span></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="font-size:12px;">  void(^lableBlock)(NSString *tontentText);</span></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="font-size:12px;">}</span></p> 
- (void)viewDidLoad { 
  [super viewDidLoad]; 
  self.view.backgroundColor = [UIColor orangeColor]; 
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
  button.frame = CGRectMake(100, 200, 100, 50); 
  [button setTitle:@"123456" forState:UIControlStateNormal]; 
  button.backgroundColor = [UIColor redColor]; 
  [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
  [self.view addSubview:button]; 
} 
- (void)buttonAction:(UIButton *)sender 
{ 
//  點(diǎn)擊按鈕 返回上一個(gè)頁(yè)面 同時(shí)通過(guò)block 把這個(gè)值傳到上一個(gè)頁(yè)面 
  self.block(sender.titleLabel.text); 
  [self dismissViewControllerAnimated:nil completion:nil]; 
}

另外:block 還有兩個(gè)問(wèn)題

1.怎么改變block里面的值

用上面的代碼 在ViewController.m viewDidLoad 里面 添加代碼

 1.局部變量 
 定義一個(gè)圖片名字的Block 
 */ 
//  局部變量 
   
  void(^imageNameBlock)(NSString *imageName); 
//  如果想修改Block里面的值 需添加__block修飾 
   
//  __block UIImage *image; 
  imageNameBlock = ^(NSString *imageName) 
  { 
    image = [UIImage imageNamed:imageName]; 
    self.view.backgroundColor = [UIColor colorWithPatternImage:image]; 
  }; 
  imageNameBlock(@"17.jpg"); 

就會(huì)出現(xiàn) Use of undeclared identifier 'image' 錯(cuò)誤情況 原因是我們?cè)噲D改變block里面的值 解決辦法就是 添加 __block 修飾(解注釋

__block UIImage *image;) 就行了

2.block的循環(huán)引用

用上面的代碼 在ViewController.m viewDidLoad 里面 添加代碼

//  全局變量 
//  block循環(huán) 引用 解決 我們用弱引用 __block 
  __weak ViewController *VC = self; 
   
  __block UIImage *image = nil; 
  imageNameBlock_1 = ^(NSString *imageName) 
  { 
    image = [UIImage imageNamed:imageName]; 
    VC.view.backgroundColor = [UIColor colorWithPatternImage:image]; 
  }; 
  UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 100)]; 
  lable.backgroundColor = [UIColor brownColor]; 
  [self.view addSubview:lable]; 
   
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)]; 
  [self.view addGestureRecognizer:tap]; 

再添加兩個(gè)方法

- (void)loadData 
{ 
  imageNameBlock_1(@"17.jpg"); 
} 
 
- (void)viewWillAppear:(BOOL)animated 
{ 
  [self loadData]; 
} 

當(dāng)出現(xiàn)循環(huán)引用是我們用__weak修飾下 但具體機(jī)制還不太懂 希望高人指點(diǎn),感謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論