在第一层向第二层跳转时 创建完第二层的实例对象,
*/
<SendValue.h>
#import
<Foundation/Foundation.h>
@protocol
SendValue <NSObject]]>
- (void)sendBtnTitle:(NSString
*)title;
@end
<XSAppDelegate.m>
#import
"XSAppDelegate.h"
#import "XSRootViewController.h"
@implementation XSAppDelegate
- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window
= [[UIWindow
alloc]
initWithFrame:[[UIScreen
mainScreen]
bounds]];
// Override point for customization after application launch.
self.window.backgroundColor
= [UIColor
whiteColor];
XSRootViewController
*rootViewController = [[XSRootViewController
alloc]
init];
UINavigationController
*navController = [[UINavigationController
alloc]
initWithRootViewController:rootViewController];
self.window.rootViewController
= navController;
[self.window
makeKeyAndVisible];
return
YES;
}
<XSRootViewController.m>
#import
"XSRootViewController.h"
#import "XSSecondViewController.h"
@interface XSRootViewController
()
@end
@implementation
XSRootViewController
- (id)initWithNibName:(NSString
*)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self
= [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if
(self) {
// Custom initialization
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor
= [UIColor
yellowColor];
self.navigationItem.title
=
@"Root";
UIBarButtonItem
*btnItem = [[UIBarButtonItem
alloc]
initWithTitle:@"Push"
style:UIBarButtonItemStylePlain
target:self
action:@selector(btnClick:)];
self.navigationItem.rightBarButtonItem
= btnItem;
}
//#pragma mark --SendVaule
- (void)sendBtnTitle:(NSString
*)title
{
self.navigationItem.title
= title;
}
- (void)btnClick:(UIBarButtonItem
*)btnItem
{
XSSecondViewController
*secondViewController = [[XSSecondViewController
alloc]
init];
secondViewController.delegate
=
self;
secondViewController.currentTitle
=
self.navigationItem.title;
[self.navigationController
pushViewController:secondViewController
animated:YES];
}
<XSSecondViewController.h>
#import
<UIKit/UIKit.h>
#import "SendValue.h"
@interface
XSSecondViewController :
UIViewController
//定义代理
@property
(nonatomic,assign)
id<SendValue> delegate;
//创建一个正向传值的属性,
@property
(nonatomic,copy)NSString
*currentTitle;
@end
<XSSecondViewController.m>
#import
"XSSecondViewController.h"
#import "XSRootViewController.h"
@interface XSSecondViewController
()
@end
@implementation
XSSecondViewController
- (id)initWithNibName:(NSString
*)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self
= [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if
(self) {
// Custom initialization
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading the view.
UIBarButtonItem
*btnItem = [[UIBarButtonItem
alloc]
initWithTitle:@"Pop"
style:UIBarButtonItemStylePlain
target:self
action:@selector(btnClick)];
self.navigationItem.leftBarButtonItem
= btnItem;
self.view.backgroundColor
= [UIColor
blueColor];
UIButton
*btn1 = [UIButton
buttonWithType:UIButtonTypeSystem];
btn1.frame
=
CGRectMake(10, 80, 300, 40);
[btn1 setTitle:@"按键一"
forState:UIControlStateNormal];
[btn1 setBackgroundColor:[UIColor
whiteColor]];
[btn1 addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
btn1.tag
= 1;
//如果按钮的标题和属性中的_currentTitle相同,即和根页面中的导航条的title一样
if
([_currentTitle
isEqualToString:btn1.currentTitle]) {
btn1.selected
=
YES;
}
//如果selected为YES
就执行setTitleColor
[btn1 setTitleColor:[UIColor
redColor]
forState:UIControlStateSelected];
[self.view
addSubview:btn1];
UIButton
*btn2 = [UIButton
buttonWithType:UIButtonTypeSystem];
btn2.frame
=
CGRectMake(10, 130, 300, 40);
[btn2 setTitle:@"按键二"
forState:UIControlStateNormal];
[btn2 setBackgroundColor:[UIColor
whiteColor]];
[btn2 addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
btn2.tag
= 2;
//如果按钮的标题和属性中的_currentTitle相同,即和根页面中的导航条的title一样
if
([_currentTitle
isEqualToString:btn2.currentTitle]) {
btn2.selected
=
YES;
}
//如果selected为YES
就执行setTitleColor
[btn2 setTitleColor:[UIColor
redColor]
forState:UIControlStateSelected];
[self.view
addSubview:btn2];
UIButton
*btn3 = [UIButton
buttonWithType:UIButtonTypeSystem];
btn3.frame
=
CGRectMake(10, 180, 300, 40);
[btn3 setTitle:@"按键三"
forState:UIControlStateNormal];
[btn3 setBackgroundColor:[UIColor
whiteColor]];
[btn3 addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
btn3.tag
= 3;
//如果按钮的标题和属性中的_currentTitle相同,即和根页面中的导航条的title一样
if
([_currentTitle
isEqualToString:btn3.currentTitle]) {
btn3.selected
=
YES;
}
//如果selected为YES
就执行setTitleColor
[btn3 setTitleColor:[UIColor
redColor]
forState:UIControlStateSelected];
[self.view
addSubview:btn3];
}
- (void)btnClick
{
[self.navigationController
popToRootViewControllerAnimated:YES];
}
- (void)btnClick:(UIButton
*)btn
{
//取出按钮的标题
NSString
*title = btn.currentTitle;
//判断代理中是否有sendBtnTitle:这个函数
if
([_delegate
respondsToSelector:@selector(sendBtnTitle:)]) {
//代理
执行自己的sendBtnTitle
函数,传参是title
[_delegate
sendBtnTitle:title];
}
[self.navigationController
popToRootViewControllerAnimated:YES];
}