/*
程序过程:
1。创建一个根视图,一个二级视图
2,根视图NavigationItem.title = Root 二级视图NavigationItem.title
= Second
根视图NavigationItem.rightButton入栈二级视图
3,
二级视图中创建三个buttonbutton一 button二 button三
三个button点击时间都是出栈。并把自己的button的
titel 赋给根视图的NavigationItem.title
4。当再次进入二级视图时,推断根视图的NavigationItem.title和哪个button的title一样。假设
一样。就把button的title颜色设置为红色。
**总结
1,从第一层向第二层正向传參时:
在第二层页面中创建一个接收这个參数的属性
在第一层向第二层跳转时 创建完第二层的实例对象,
*/
*/
<SendValue.h>
#import
<Foundation/Foundation.h>
@protocol
SendValue <NSObject]]>
- (void)sendBtnTitle:(NSString*)title;
@end
<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= [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
// Override point for customization after application launch.
self.window.backgroundColor=
[UIColorwhiteColor];
XSRootViewController*rootViewController = [[XSRootViewControlleralloc]init];
UINavigationController*navController = [[UINavigationControlleralloc]initWithRootViewController:rootViewController];
self.window.rootViewController=
navController;
[self.windowmakeKeyAndVisible];
return
YES;
}
"XSAppDelegate.h"
#import "XSRootViewController.h"
@implementation XSAppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.window= [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
// Override point for customization after application launch.
self.window.backgroundColor=
[UIColorwhiteColor];
XSRootViewController*rootViewController = [[XSRootViewControlleralloc]init];
UINavigationController*navController = [[UINavigationControlleralloc]initWithRootViewController:rootViewController];
self.window.rootViewController=
navController;
[self.windowmakeKeyAndVisible];
return
YES;
}
<XSRootViewController.m>
#import
"XSRootViewController.h"
#import "XSSecondViewController.h"
@interface XSRootViewController()
@end
@implementationXSRootViewController
- (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=
[UIColoryellowColor];
self.navigationItem.title=
@"Root";
UIBarButtonItem*btnItem = [[UIBarButtonItemalloc]initWithTitle:@"Push"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(btnClick:)];
self.navigationItem.rightBarButtonItem=
btnItem;
}
//#pragma mark --SendVaule
- (void)sendBtnTitle:(NSString*)title
{
self.navigationItem.title=
title;
}
- (void)btnClick:(UIBarButtonItem*)btnItem
{
XSSecondViewController*secondViewController = [[XSSecondViewControlleralloc]init];
secondViewController.delegate=
self;
secondViewController.currentTitle=
self.navigationItem.title;
[self.navigationControllerpushViewController:secondViewControlleranimated:YES];
}
"XSRootViewController.h"
#import "XSSecondViewController.h"
@interface XSRootViewController()
@end
@implementationXSRootViewController
- (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=
[UIColoryellowColor];
self.navigationItem.title=
@"Root";
UIBarButtonItem*btnItem = [[UIBarButtonItemalloc]initWithTitle:@"Push"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(btnClick:)];
self.navigationItem.rightBarButtonItem=
btnItem;
}
//#pragma mark --SendVaule
- (void)sendBtnTitle:(NSString*)title
{
self.navigationItem.title=
title;
}
- (void)btnClick:(UIBarButtonItem*)btnItem
{
XSSecondViewController*secondViewController = [[XSSecondViewControlleralloc]init];
secondViewController.delegate=
self;
secondViewController.currentTitle=
self.navigationItem.title;
[self.navigationControllerpushViewController:secondViewControlleranimated: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
<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
@implementationXSSecondViewController
- (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 = [[UIBarButtonItemalloc]initWithTitle:@"Pop"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(btnClick)];
self.navigationItem.leftBarButtonItem=
btnItem;
self.view.backgroundColor=
[UIColorblueColor];
UIButton*btn1 = [UIButtonbuttonWithType:UIButtonTypeSystem];
btn1.frame=
CGRectMake(10, 80, 300, 40);
[btn1 setTitle:@"按键一"forState:UIControlStateNormal];
[btn1 setBackgroundColor:[UIColorwhiteColor]];
[btn1 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
btn1.tag= 1;
//假设button的标题和属性中的_currentTitle同样,即和根页面中的导航条的title一样
if
([_currentTitleisEqualToString:btn1.currentTitle])
{
btn1.selected=
YES;
}
//假设selected为YES就运行setTitleColor
[btn1 setTitleColor:[UIColorredColor]forState:UIControlStateSelected];
[self.viewaddSubview:btn1];
UIButton*btn2 = [UIButtonbuttonWithType:UIButtonTypeSystem];
btn2.frame=
CGRectMake(10, 130, 300, 40);
[btn2 setTitle:@"按键二"forState:UIControlStateNormal];
[btn2 setBackgroundColor:[UIColorwhiteColor]];
[btn2 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
btn2.tag= 2;
//假设button的标题和属性中的_currentTitle同样,即和根页面中的导航条的title一样
if
([_currentTitleisEqualToString:btn2.currentTitle])
{
btn2.selected=
YES;
}
//假设selected为YES就运行setTitleColor
[btn2 setTitleColor:[UIColorredColor]forState:UIControlStateSelected];
[self.viewaddSubview:btn2];
UIButton*btn3 = [UIButtonbuttonWithType:UIButtonTypeSystem];
btn3.frame=
CGRectMake(10, 180, 300, 40);
[btn3 setTitle:@"按键三"forState:UIControlStateNormal];
[btn3 setBackgroundColor:[UIColorwhiteColor]];
[btn3 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
btn3.tag= 3;
//假设button的标题和属性中的_currentTitle同样,即和根页面中的导航条的title一样
if
([_currentTitleisEqualToString:btn3.currentTitle])
{
btn3.selected=
YES;
}
//假设selected为YES就运行setTitleColor
[btn3 setTitleColor:[UIColorredColor]forState:UIControlStateSelected];
[self.viewaddSubview:btn3];
}
- (void)btnClick
{
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}
- (void)btnClick:(UIButton*)btn
{
//取出button的标题
NSString*title = btn.currentTitle;
//推断代理中是否有sendBtnTitle:这个函数
if
([_delegate
respondsToSelector:@selector(sendBtnTitle:)]) {
//代理运行自己的sendBtnTitle函数,传參是title
[_delegatesendBtnTitle:title];
}
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}
"XSSecondViewController.h"
#import "XSRootViewController.h"
@interface XSSecondViewController()
@end
@implementationXSSecondViewController
- (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 = [[UIBarButtonItemalloc]initWithTitle:@"Pop"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(btnClick)];
self.navigationItem.leftBarButtonItem=
btnItem;
self.view.backgroundColor=
[UIColorblueColor];
UIButton*btn1 = [UIButtonbuttonWithType:UIButtonTypeSystem];
btn1.frame=
CGRectMake(10, 80, 300, 40);
[btn1 setTitle:@"按键一"forState:UIControlStateNormal];
[btn1 setBackgroundColor:[UIColorwhiteColor]];
[btn1 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
btn1.tag= 1;
//假设button的标题和属性中的_currentTitle同样,即和根页面中的导航条的title一样
if
([_currentTitleisEqualToString:btn1.currentTitle])
{
btn1.selected=
YES;
}
//假设selected为YES就运行setTitleColor
[btn1 setTitleColor:[UIColorredColor]forState:UIControlStateSelected];
[self.viewaddSubview:btn1];
UIButton*btn2 = [UIButtonbuttonWithType:UIButtonTypeSystem];
btn2.frame=
CGRectMake(10, 130, 300, 40);
[btn2 setTitle:@"按键二"forState:UIControlStateNormal];
[btn2 setBackgroundColor:[UIColorwhiteColor]];
[btn2 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
btn2.tag= 2;
//假设button的标题和属性中的_currentTitle同样,即和根页面中的导航条的title一样
if
([_currentTitleisEqualToString:btn2.currentTitle])
{
btn2.selected=
YES;
}
//假设selected为YES就运行setTitleColor
[btn2 setTitleColor:[UIColorredColor]forState:UIControlStateSelected];
[self.viewaddSubview:btn2];
UIButton*btn3 = [UIButtonbuttonWithType:UIButtonTypeSystem];
btn3.frame=
CGRectMake(10, 180, 300, 40);
[btn3 setTitle:@"按键三"forState:UIControlStateNormal];
[btn3 setBackgroundColor:[UIColorwhiteColor]];
[btn3 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
btn3.tag= 3;
//假设button的标题和属性中的_currentTitle同样,即和根页面中的导航条的title一样
if
([_currentTitleisEqualToString:btn3.currentTitle])
{
btn3.selected=
YES;
}
//假设selected为YES就运行setTitleColor
[btn3 setTitleColor:[UIColorredColor]forState:UIControlStateSelected];
[self.viewaddSubview:btn3];
}
- (void)btnClick
{
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}
- (void)btnClick:(UIButton*)btn
{
//取出button的标题
NSString*title = btn.currentTitle;
//推断代理中是否有sendBtnTitle:这个函数
if
([_delegate
respondsToSelector:@selector(sendBtnTitle:)]) {
//代理运行自己的sendBtnTitle函数,传參是title
[_delegatesendBtnTitle:title];
}
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。