IOS StoryBoard页面传值,在没有StoryBoard之前,xib直接传值我们可以通过协议,下面我们主要说一下StoryBoard界面切换的传值
StoryBoard传值方法一(使用segue传值)
首先新创建一个Project,创建完成以后,打开StoryBoard,在当前的viewcontroller中分别拖放一个UIButton,一个UILabel,一个UITextField,然后向StoryBoard中拖放一个viewcontroller,在新拖放的viewcontroller上面拖放两个UITextFiled,一个UILabel,一个UIButton,拖放完成以后如下图:
在第一个UIViewController的类中定义一个UITextField,指向界面上的UITextField,然后新建一个类SecondViewController,指向第二个viewcontroller,由于第二个viewcontroller界面上有两个UITextField,我们在SecondViewController的头文件中声明两个UITextField,分别指向界面上的UITextField,假设我们要从第一个viewcontroller向界面传递三个参数,分别是字符串name,字符串price,键值对dic,那么我们在SecondViewController.h中声明,同时在.m文件中实现,SecondViewController的代码如下:
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property(nonatomic,strong)IBOutlet UITextField *nameField;
@property(nonatomic,strong)IBOutlet UITextField *priceField;
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *price;
@property(nonatomic,strong)NSDictionary *dic;
-(IBAction)BtnGoToNext:(id)sender;
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize name;
@synthesize price;
@synthesize dic;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.nameField.text = name;
self.priceField.text =price;
NSLog(@"dic:%@",dic);
[super viewDidLoad];
// Do any additional setup after loading the view.
}
//使用通知传递参数,发送广播
-(void)BtnGoToNext:(id)sender{
//three 是连接线的identifier,如果是tablecell的click时间,这儿可以传当前点击的cell
[self performSegueWithIdentifier:@"three" sender:self];
NSArray *array =[NSArray arrayWithObjects:@"one",@"two",@"three", nil];
NSNotification *notification =[NSNotification notificationWithName:@"notification1" object:array];
[[NSNotificationCenter defaultCenter] postNotification:notification];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification2" object:self.priceField.text];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end</span>
打开第一个viewcontroller对应的类文件,代码如下:
<span style="font-family:Comic Sans MS;font-size:14px;">#import <UIKit/UIKit.h>
@interface LTViewController : UIViewController
@property(nonatomic,strong)IBOutlet UITextField *txtField;
@end
#import "LTViewController.h"
@interface LTViewController ()
@end
@implementation LTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//怎样跳转到随意一个页面
//在有可能进行上级跳转的ViewController文件里加上以下代码,函数名称任起:
#pragma mark 定义这个函数,别的ViewController在Exit的时候就能直接跳到这了
//在想要跳转view的Exit上右键,选择这个goHome函数,拉到想要运行的button上,就能够实现跳转了(只需要有一个UIStoryboardSegue的参数)
- (IBAction)goHome:(UIStoryboardSegue *)segue
{
//segue.identifier
[[segue sourceViewController] class];
}
//使用segue传递参数
//注意:发送数据时,[send setValue:msg forKey:@"name"]; 这个"name"名称一定要与跳转后界面的声明的类型对象的命名一致,不然的话,跳转后的界面是收不到传递的值的。
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSString *content = self.txtField.text;
UIViewController *next = segue.destinationViewController;
//UIButton *btn =(UIButton *)sender;//跳转触发的对象
//segue.identifier
if([next respondsToSelector:@selector(setName:)]){//setName:是下一个viewcontroller的中定义的属性自动生成的方法名
[next setValue:content forKey:@"name"];
[next setValue:@"10.3" forKey:@"price"];
NSDictionary *dictionary =[NSDictionary dictionaryWithObjectsAndKeys:@"one",@"key",@"three",@"sex",nil];
[next setValue:dictionary forKey:@"dic"];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我们在第一个类型重写prepareForSegue方法,在第二个类的didViewLoad方法中接收传递过来的参数
让第一个viewcontroller上面的Button指向第二个viewcontroller,如下图:
这样就完成了参数的传递
StoryBoard传值方法二(使用广播)
向storyboard上面再次拖放一个viewcontroller,在上面拖放一个UILabel,一个UITextField,拖放完成以后如下图:
然后新建一个类,继承自UIViewController,命名为ThreeViewController,指向第三个viewcontroller,我们在.h文件中声明一个UITextField,指向页面的UITextField,我们在该类的ViewDidLoad函数中声明观察者,代码如下:
我们通过performSegueWithIdentifier完成界面的跳转,我们在SecondViewController声明Button的点击方法,如下图:
让第二个viewcontroller上Button为Go To Three的按钮点击指向执行BtnGoToNext,让第二个viewcontroller的manual指向第三个,设置连接线的identifier为three,BtnGoToNext的实现如下图:
这样就完成了参数的传递