IOS UI-Block

来源:互联网 发布:mac 口红brave 编辑:程序博客网 时间:2024/06/17 02:14

在Xib下建立工程,创建两个页面,有两个button,点击第一个页面的button push 出第二个页面,并且利用block传值,把第一个页面的button值传给第二页 再把第二个页面button名字传给第一页

程序如下:

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate :UIResponder <UIApplicationDelegate>

@property (strong,nonatomic) UIWindow *window;

@end

APPDelegate.m

#import "AppDelegate.h"

#import "MainViewController.h"

@interfaceAppDelegate ()


@end


@implementation AppDelegate


- (void)dealloc

{

    [_windowrelease];

    [superdealloc];

}

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

    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    [_windowrelease];

    

    MainViewController *mainVC = [[MainViewControlleralloc]init];

    UINavigationController *naviVC = [[UINavigationControlleralloc]initWithRootViewController:mainVC];

    self.window.rootViewController = naviVC;

    [mainVCrelease];

    [naviVCrelease];

    

    returnYES;

}

MainViewController.h

#import <UIKit/UIKit.h>


@interface MainViewController :UIViewController


@end

MainViewController.m

#import "MainViewController.h"

#import "SecondViewController.h"

@interface MainViewController ()

- (IBAction)buttonAction:(UIButton *)sender;


@end


@implementation MainViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view from its nib.

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


- (IBAction)buttonAction:(UIButton *)sender {

    NSLog(@"测试");

    //Block 的基本形式

    //无返回值 无参数

    void (^block1) (void) = ^(void){

        NSLog(@"adas");

    };

    //给block变量赋值

    block1();//block在方法里面回车

    //无返回值有参数

    void(^block2)(NSInteger num) = ^(NSInteger num){

        NSLog(@"无返回值,有参数");

        NSLog(@"%ld",num);

    };

    block2(5);

    //参数是字符串的block ,有一个字符串参数的block变量

    void (^block3) (NSString *str) = ^(NSString *str){

//        NSLog(@"芊芊");

        NSLog(@"%@",str);

        //利用字符串参数改变button的标题

        [sender setTitle:strforState:UIControlStateNormal];

    };

//    //调用block改变button标题

//    block3(@"芊芊");

//  让第二个页面接收block

    

    

    

    

    SecondViewController *secVC = [[SecondViewControlleralloc]init];

    //从前往后传值2

        secVC.name = sender.currentTitle;

    //2.3 在第一个页面,将定义好的block变量给第二页

    secVC.block = block3;

    [self.navigationControllerpushViewController:secVC animated:YES];

    [secVC release];

}

@end

SecondViewController.h

#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController

//从前往后传值1

@property (nonatomic,retain)NSString *name;

//给第二个页面写一个block

//2.1

//block的属性需要copy修饰,将block变量从栈区拷贝到堆区

@property (nonatomic,copy)void(^block) (NSString *str);

@end

SecondViewContrller.m

#import "SecondViewController.h"

//函数

//返回值 方法名 (类名)

//调用 方法名();

//把两个函数的方法名擦掉 是类型

//地址 把名字去掉加*       指针(* p)

//block 就是把*换成^

@interface SecondViewController ()

@property (retain,nonatomic) IBOutletUITextField *textField;

- (IBAction)buttonAction:(UIButton *)sender;


@end


@implementation SecondViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view from its nib.

    //从前往后传值3 接收

    self.textField.text =self.name;

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


- (IBAction)buttonAction:(UIButton *)sender {

    [self.navigationControllerpopToRootViewControllerAnimated:YES];

    //2.2

    //调用block,将修改后的textField中的字符串用于改变第一个页面的button标题

    self.block(_textField.text);

}

- (void)dealloc {

    [_textField release];

    [super dealloc];

}

@end

SecondViewController.xib


MainViewController.xib


最后运行效果:














0 0
原创粉丝点击