先说单例,单例有两个好处
1.传值
2.节约内存
在本程序中,单例类的命名为DateHandle
代码如下.h代码如下
#import <Foundation/Foundation.h>
@interface DateHandle : NSObject
//需要一个属性来存
@property(nonatomic,retain)NSString * text;
#pragma mark------------获取单例对象(整个应用程序中,有且只有一个,与应用程序的生命周期一样)
+(DateHandle *)sharedInstance;
@end
.m代码如下
#import "DateHandle.h"
@implementation DateHandle
//如果以standardxxx , onlyxxxx , defaultxxx , sharedxxx 开头的方法,都是获得单例对象。都是类方法
static DateHandle *dateHandle = nil; //必须写在外面,不然就创建了另一个对象
+(DateHandle *)sharedInstance
{
//为空时创建,不为空时,直接返回
//为了避免多个线程同时访问同一个资源,导致资源不统一
@synchronized(self){
if (dateHandle == nil) {
dateHandle = [[DateHandle alloc]init];
}
}
return dateHandle;
}
@end
这里面写的单例类不算很完整,还要更改其它的几个方法,例如copyWithZone等等。
然后本程序的结构为,先创建一个根视图控制器rootViewControoler。又创建了ModelViewController。model与UINavigationController是平级的,model推出的方法用
[self presentViewController:Nc animated:YES completion:^{
NSLog(@"这是模态推出的界面");
}];
模态返回用
//模态返回
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"模态返回");
}];
RootViewController.m中的代码如下
#import "RootViewController.h"
#import "FirstViewController.h"
#import "ModalViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//创建左右导航button
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"PUSH" style:UIBarButtonItemStyleDone target:self action:@selector(clickPUSH:)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"MODEL" style:UIBarButtonItemStyleDone target:self action:@selector(clickMODEL:)];
self.navigationItem.title = @"RootVC";
self.navigationItem.rightBarButtonItem = leftButton;
self.navigationItem.leftBarButtonItem = rightButton;
[leftButton release];
[rightButton release];
// Do any additional setup after loading the view.
}
-(void)clickMODEL:(UIBarButtonItem *)button{
ModalViewController *modealVC = [[ModalViewController alloc]init];
//单例对象使用的场景
//1.当切换页面时,音乐不停止播放
//2.当多个页面使用同一个资源的话
//模态推出modalVC,推出的modalVC 和navigationController是平级的,没有上下级关系
//设置modalTransition
modealVC.modalTransitionStyle = arc4random()%4;
//给modealVC加一个导航控制器
UINavigationController *Nc = [[UINavigationController alloc]initWithRootViewController:modealVC];
[self presentViewController:Nc animated:YES completion:^{
NSLog(@"这是模态推出的界面");
}];
[modealVC release];
NSLog(@"%@",self.navigationController.viewControllers);
}
-(void)clickPUSH:(UIBarButtonItem *)button{
FirstViewController *firstVC = [[FirstViewController alloc]init];
firstVC.modalTransitionStyle = arc4random()%3;
[self.navigationController pushViewController:firstVC animated:YES];
}
ModelViewController中的代码如下
#import "ModalViewController.h"
#import "DateHandle.h"
@interface ModalViewController ()
@property(nonatomic,retain)UITextField * textField;
@end
@implementation ModalViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textField];
[_textField release];
#pragma mark ------------创建BarButtonItem
[self createBarButtonItems];
// Do any additional setup after loading the view.
}
-(void)createBarButtonItems{
UIBarButtonItem *leftBI = [[UIBarButtonItem alloc]initWithTitle:@"Dismiss" style:UIBarButtonItemStylePlain target:self action:@selector(didClickLeftBI:)];
self.navigationItem.leftBarButtonItem = leftBI;
}
-(void)didClickLeftBI:(UIBarButtonItem *)button{
//模态返回
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"模态返回");
}];
//获取单例对象
DateHandle *dateHandle = [DateHandle sharedInstance];
//将textField的值存入单例对象中
dateHandle.text = _textField.text;
}