iOS开发使用Block页面间传值

iOS传值使用Block传值。


iOS开发使用block传值很简单,如果你对函数指针的使用非常熟练,直接使用就可以了。如果还不是了解函数指针先了解函数指针。


typedef进行声明一个Block:

声明方法:  typedef void(^myBlock)(NSString *Str); void 是返回值,NSString是参数 myBlock是声明返回值为空,参数是NSString类型的类型的值。^这个东西是不是和*有点相似。


继续先声明一个Block变量: @property (nonatomic,copy)myBlock myBlockargs;

调用一下: self.myBlockargs(label.text);后面括号中的是要传给下一个页面的数据(label.text)


第一个页面调用:

 myView.myBlockargs = ^(NSString * str)

    {

        label.text = str;

    

    };

str中的值就是第二个页面传过来的值。



上源代码:第一个页面



.m文件


#import "ViewController.h"

#import "MyViewController.h"




@interface ViewController ()

{

    UILabel *label;

}

@property(nonatomic,strong)UIButton *testBtn;




@end



@implementation ViewController




- (void)viewDidLoad {

    

    [super viewDidLoad];

    

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100,100,60,30)];

    

    

    label = [[UILabel alloc]initWithFrame:CGRectMake(200, 100,100,30)];

    label.backgroundColor = [UIColor lightGrayColor];

    

    label.text = @"hello world";

    

    button.backgroundColor = [UIColor redColor];

   

    [button setTitle:@"hello" forState:UIControlStateNormal];

    

    

    [button addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    [self.view addSubview:label];

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

}

-(void)Click:(UIButton *)sender

{

    MyViewController *myView = [[MyViewController alloc]init];

    

    myView.view.backgroundColor = [UIColor lightGrayColor];

    

    myView.myBlockargs = ^(NSString * str)

    {

        label.text = str;

    

    };

    

    [self.navigationController pushViewController:myView animated:YES];


}




- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



.h文件


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController



@end





第二个页面:


.h文件

#import <UIKit/UIKit.h>

typedef void(^myBlock)(NSString *str);


@interface MyViewController : UIViewController


@property (nonatomic,copy)myBlock myBlockargs;

@end



.m文件

#import "MyViewController.h"


@interface MyViewController ()

{

    UILabel *label;

}


@end


@implementation MyViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 60,30)];

    button.backgroundColor = [UIColor redColor];

    [button setTitle:@"world" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(secondView:) forControlEvents:UIControlEventTouchUpInside];


    label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200,60,30)];

    label.backgroundColor = [UIColor redColor];

    label.text = @"second";

    

    

    

    [self.view addSubview:button];

    [self.view addSubview:label];


    

}

-(void)secondView:(UIButton *)sender

{

    label.text = @"Second View Value";

    

    //[self returnText:label.text];

    

    self.myBlockargs(label.text);


    

    

    [self.navigationController popViewControllerAnimated:YES];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




AppDelegate.m文件


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

    

    ViewController *view = [[ViewController alloc]init];

    

    UINavigationController *nva = [[UINavigationController alloc]initWithRootViewController:view];

    

    self.window.rootViewController = nva;

    

    self.window.backgroundColor = [UIColor whiteColor];

    

    [self.window makeKeyAndVisible];

    

    

    

    // Override point for customization after application launch.

    return YES;

}



运行结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值