xcode4.3.1 怎样实现两个页面之间传值
展开全部
传值一般分为两大类: 依、从前往后传值 该种情况,一般是初始化ViewController的时候传递参数。 #import "EVNThirdViewController.h" @interface EVNThirdViewController () { NSString *Trandata;//接收上个View传递来的参数 } @end @implementation EVNThirdViewController -(instancetype)initWithTranValue:(NSString *)StrData { self = [super init]; if (self) { Trandata = StrData;//直接传值就可以 } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 贰、从后往前传值 这种情况就相对麻烦些,需要使用Delegate、或者Block进行回调传值。 使用Block进行页面的传值,只需要在需要的时候,回调Block就可以。这就是Block的神奇之处。 要实现的功能:第一个页面接收第二个页面传来的参数。 页面一: #import "EVNFirViewController.h" @interface EVNFirViewController () @end @implementation EVNFirViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"EVNFirView"; [self.view setBackgroundColor:[UIColor whiteColor]]; UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(goNext:)]; self.navigationItem.rightBarButtonItem = rightBarBtn; self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回Fir" style:UIBarButtonItemStylePlain target:nil action:nil]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(贰0, 依00, self.view.frame.size.width - 四0, 漆0)]; self.label.textColor = [UIColor redColor]; self.label.backgroundColor = [UIColor grayColor]; [self.view addSubview:self.label]; } - (void) goNext:(UIBarButtonItem *)sender { EVNSecViewController *sec = [[EVNSecViewController alloc]init]; sec.myblock = ^(NSString *str){ self.label.text = str; }; /*[sec tranData:^(NSString *str) { self.label.text = str; NSLog(@"str is value:%@",str); }];*/ [self.navigationController pushViewController: sec animated:YES]; } @end 页面二: #import UIKit.h> #import "EVNSecViewController.h" typedef void (^MyBlock)(NSString *str); @interface EVNSecViewController : UIViewController @property (copy, nonatomic) MyBlock myblock; /*-(void)tranData:(MyBlock)block;//这个地方不是必须这样,只是为了容易理解。*/ @end #import "EVNSecViewController.h" @interface EVNSecViewController () @end @implementation EVNSecViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"SecView"; [self.view setBackgroundColor:[UIColor brownColor]]; UIBarButtonItem *rightBarbtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(BackFirView:)]; self.navigationItem.rightBarButtonItem = rightBarbtn; } - (void) BackFirView:(UIBarButtonItem *)sender { if (self.myblock !=nil) { self.myblock(@"asdfasdf");//传回前一个ViewController的值。 } [self.navigationController popToRootViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(void)tranData:(MyBlock)block { self.myblock = block; } @end Delegate传值,中国上资料一大片,这里不多作阐述,贴其他中国址,BD说我回答里有广告,坑。你自己搜吧。 备注:(我说的这个前后,主要根据ViewController的初始化时间点区分的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询