注意:工程中没有使用系统默认的ViewController.m
目标动作传值
//AppDelegate.m文件
#import "AppDelegate.h"
#import "OneViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
OneViewController *oneViewController = [[OneViewController alloc]init];
//设置oneViewController为导航控制器的根视图
UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:oneViewController];
self.window.rootViewController = nvc;
return YES;
}
//OneViewController.m文件
#import "OneViewController.h"
#import "TwoViewController.h"
@interface OneViewController ()
@end
@implementation OneViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}
//创建第二个导航控制器页面 实现目标动作方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
TwoViewController *twoViewController = [[TwoViewController alloc]init];
[self.navigationController pushViewController:twoViewController animated:YES];
[twoViewController addTarget:self action:@selector(titleNameBack:)];
}
//传值
- (void)titleNameBack:(NSString *)string{
self.navigationItem.title = string;
}
//SecondController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//创建返回按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(btnClick)];
}
//调用目标动作机制
- (void)btnClick{
[self.target performSelector:self.action withObject:@"第二个页面"];
[self.navigationController popViewControllerAnimated:YES];
}
//保存全局变量
- (void)addTarget:(id)target action:(SEL)action{
_target = target;
_action = action;
}