假设
界面1为 ViewController01 : UIViewController 界面2为 ViewController02 : UIViewController
其中界面1为项目的rootViewController.
AppDelegate.h代码
- #import <UIKit/UIKit.h>
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
- @property (strong, nonatomic) UIWindow *window;
- @property (strong, nonatomic) NSString* localImagePath;
- @end
AppDelegate.m的部分代码
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- ViewController01* mViewController01 = [[ViewController01 alloc]init];
- self.window.rootViewController = mViewController01;
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- return YES;
- }
ViewController01.h代码
- #import <UIKit/UIKit.h>
- @interface ViewController01 : UIViewController
- @property (strong,nonatomic) NSMutableDictionary* parameter;
- @end
ViewController01.m代码
- #import "ViewController01.h"
- #import "MyViewTool.h"
- #import "ViewController02.h"
- @interface ViewController01 ()
- @end
- @implementation ViewController01
- - (void)viewDidLoad {
- NSLog(@"viewDidLoad():视图1");
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [self initViews];
- [self initParameter];
- }
- -(void)viewDidAppear:(BOOL)animated{
- NSLog(@"viewDidAppear():视图1");
- [super viewDidAppear:animated];
- //接收到的参数
- NSLog(@"viewDidAppear():视图1,收到的参数:from=%@",[self.parameter objectForKey:@"from"]);
- }
- - (void)initViews{
- CGSize size = [MyViewTool loadScreenSize];
- CGFloat width = size.width;
- UILabel* label = [MyViewTool createLabel:CGRectMake(10, 40, width-20, 100) withText:@"视图1" withBgcolor:[UIColor yellowColor]];
- UIButton* btn = [MyViewTool createButton:CGRectMake(label.frame.origin.x, label.frame.origin.y+label.frame.size.height+20, 200, 50) withDelegate:self withAction:@selector(buttonClick:) withTitle:@"按钮1:点击打开视图2"
- withBgColor:[UIColor lightGrayColor]];
- btn.center = CGPointMake(width/2, btn.center.y);
- [self.view addSubview:label];
- [self.view addSubview:btn];
- }
- #pragma mark 初始化要传递的参数
- - (void)initParameter{
- self.parameter = [[NSMutableDictionary alloc]init];
- }
- -(void)buttonClick : (UIButton*) sender {
- NSLog(@"buttonClick:%@",sender.titleLabel.text);
- //设置传递参数的数据
- [self.parameter setObject:@"我是视图1设置的参数" forKey:@"from"];
- //打开 ViewController02
- ViewController02* mViewController02 = [[ViewController02 alloc]init];
- mViewController02.parameter=self.parameter;
- [self presentViewController:mViewController02 animated:YES completion:^{
- NSLog(@"presentViewController成功");
- }];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
ViewController02.h代码
- #import <UIKit/UIKit.h>
- @interface ViewController02 : UIViewController
- @property (strong,nonatomic) NSMutableDictionary* parameter;
- @end
ViewController02.m代码
- #import "ViewController02.h"
- #import "MyViewTool.h"
- @interface ViewController02 ()
- @end
- @implementation ViewController02
- - (void)viewDidLoad {
- NSLog(@"viewDidLoad():视图2");
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [self initViews];
- }
- -(void)viewDidAppear:(BOOL)animated{
- NSLog(@"viewDidAppear():视图2");
- [super viewDidAppear:animated];
- //接收到的参数
- NSLog(@"viewDidAppear():视图2,收到的参数:from=%@",[self.parameter objectForKey:@"from"]);
- }
- - (void)initViews{
- CGSize size = [MyViewTool loadScreenSize];
- CGFloat width = size.width;
- UILabel* label = [MyViewTool createLabel:CGRectMake(10, 40, width-20, 100) withText:@"视图2" withBgcolor:[UIColor yellowColor]];
- UIButton* btn = [MyViewTool createButton:CGRectMake(label.frame.origin.x, label.frame.origin.y+label.frame.size.height+20, 200, 50) withDelegate:self withAction:@selector(buttonClick:) withTitle:@"按钮2:点击返回视图1" withBgColor:[UIColor lightGrayColor]];
- btn.center = CGPointMake(width/2, btn.center.y);
- [self.view addSubview:label];
- [self.view addSubview:btn];
- }
- -(void)buttonClick : (UIButton*) sender {
- NSLog(@"buttonClick:%@",sender.titleLabel.text);
- //设置返回给视图1的参数
- [self.parameter setObject:@"我是视图2设置的参数" forKey:@"from"];
- [self dismissViewControllerAnimated:YES completion:^{
- NSLog(@"dismissViewControllerAnimated成功");
- }];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
项目源代码参见附近中的demo010.zip