效果:
页面跳转后传值:
页面1 跳转到 页面2
代码:
1.AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
//@property (strong, nonatomic)UINavigationController *navigationController;
@end
2.AppDelegate.m
#import "AppDelegate.h"
#import "FirstViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIWindow *window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
FirstViewController *viewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
//这种方式行不通,必须直接把页面1赋值给window的根控制器
// _navigationController = [[UINavigationController alloc]initWithRootViewController:viewController];
// [self.window addSubview:_navigationController.view];
// self.window.rootViewController = _navigationController;
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
3.新建一个传值的代理
PassValueDelegateView.h
#import <Foundation/Foundation.h>
//声明代理
@protocol PassValueDelegateView <NSObject>
//定义一个代理方法用来传值
-(void)passValue: ( NSString * ) value;
@end
4.FirstViewController.h
#import <UIKit/UIKit.h>
#import "PassValueDelegateView.h"
@interface FirstViewController : UIViewController{
NSObject <PassValueDelegateView> *delegate;
}
//定义代理的属性,下面的id意味着任何类型都可以充当这个代理,但是必须遵守代理协议
@property (assign, nonatomic) id<PassValueDelegateView> passDelegate;
@property (weak, nonatomic) IBOutlet UITextField *firstField;
- (void)pressToSecond:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *next;
@end
5.页面跳转 +代理传值(注意:必须先页面跳转,再传值)
FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "AppDelegate.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.next addTarget:self action:@selector(pressToSecond:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)pressToSecond:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
// 页面跳转:
// 方法1:(传不了值,不知道为什么???)
// AppDelegate *myDelegate = [[UIApplication sharedApplication]delegate];
// [myDelegate.navigationController pushViewController:secondView animated:YES];
// 方法2:这种方法已经被下面的取代了,不过也可以起跳转的作用
// [self presentModalViewController:secondView animated:YES];
// 方法3:必须用这种方法跳转,传值成功
[self presentViewController:secondView animated:YES completion:nil];
// 设置secondView为当前控制器的传值代理(必须满足遵守代理协议,.m文件实现代理方法)
self.passDelegate = secondView;
// 调用传值的代理方法,跳转到secondViewController.m (此方法的实现在secondViewController.m里)
[self.passDelegate passValue:_firstField.text];
}
@end
6. secondViewController继承代理
SecondViewController.h
#import <UIKit/UIKit.h>
#import "PassValueDelegateView.h"
@interface SecondViewController : UIViewController<PassValueDelegateView>//意味着可以使用代理的任何属性和方法,实现代理方法
@property (weak, nonatomic)NSString *data;
@property (weak, nonatomic) IBOutlet UITextField *secondField;
- (IBAction)Back:(id)sender;
@end
7.SecondViewController实现代理的方法
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)passValue:(NSString *)value{
_data = value;
self.data = value;
self.secondField.text = value;
NSLog(@"收到的值%@ 和secondField = %@",value,_secondField.text);
}
- (IBAction)Back:(id)sender {
//页面跳转回去
[self dismissViewControllerAnimated:YES completion:nil];
}
@end