传值有三种方式:
1.设置代理传值
2.属性船只
3.block传值
前两种前面已经给你介绍过,下面是block传值的实例程序
把第一个页面的button的内容传给第二页的textField,第二页textField的内容改变再传给第一页
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
MainViewController *mainVC = [[MainViewController alloc]init];
UINavigationController *naviVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
self.window.rootViewController = naviVC;
[mainVC release];
[naviVC release];
return YES;
}
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@end
- (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:str forState:UIControlStateNormal];
};
// //调用block改变button标题
// block3(@"芊芊");
// 让第二个页面接收block
SecondViewController *secVC = [[SecondViewController alloc]init];
//从前往后传值2
secVC.name = sender.currentTitle;
//2.3 在第一个页面,将定义好的block变量给第二页
secVC.block = block3;
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
}
@end
#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
#import "SecondViewController.h"
//函数
//返回值 方法名 (类名)
//调用 方法名();
//把两个函数的方法名擦掉 是类型
//地址 把名字去掉加* 指针(* p)
//block 就是把*换成^
@interface SecondViewController ()
@property (retain, nonatomic) IBOutlet UITextField *textField;
- (IBAction)buttonAction:(UIButton *)sender;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//从前往后传值3 接收
self.textField.text = self.name;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// 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.navigationController popToRootViewControllerAnimated:YES];
//2.2
//调用block,将修改后的textField中的字符串用于改变第一个页面的button标题
self.block(_textField.text);
}
- (void)dealloc {
[_textField release];
[super dealloc];
}
@end