⼀、模态viewController
1、介绍
程序中切换⻚⾯,可以使⽤UINavigationController。通过导航功能实现⻚⾯切换。使用 pushViewController:animated:该方法显示的视图具有层级关系;而使用模态视图控制器presentViewController:animated:completion显示的视图与之前的视图控制器平级(或者说是两个不相干的层级)。
使用场景
临时展⽰⼀些内容。例如:程序中⽤户登录,通讯录中添加联系⼈等等。
UIImagePickerController。调⽤系统相册、照相机。
2、方法
显示:
presentViewController:animated:completion:
弹出:
dismissViewControllerAnimated:completion:
弹出的两种实现⽅式,在模态controller中使⽤:
[self dismissViewControllerAnimated:YES completion:nil];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
使⽤self调⽤⽅法,系统会使⽤self.presentingViewController调⽤这个⽅
法。
3、属性
modalPresentationStyle 模态controller显⽰样式
modalTransitionStyle 模态显⽰动画样式
⼆、单例
在一个应用程序中,可能会有一些信息在程序运行期间一直存在,比如:登录信息、通讯录中的联系人信息等,单例为此类信息的访问提供了便捷。
1、单例的特性
在内存中只有一个对象,节省空间
单例对象存储在堆区,操作单例对象的变量存储在静态区,程序关闭后由系统⾃动回收
可全局访问
避免频繁的创建销毁对象,可以提⾼性能
生命周期与应用程序相同
降低模块之间的耦合度,降低代码的复杂度。
2、单例初始化方法
规范写法:
standardxxx
onlyxxx
defaultxxx
sharedxxx
三、综合使用
新建RootViewController:UIViewController作为NavigationController的rootViewController。再将NavigationController添加为window的rootViewController。设置RootViewController的leftBar和rightBar分别以模态视图控制器和普通视图控制器的方式推出ModalViewController和FirstViewController。
在ModalViewController中初始化单例DataHandle,在FirstViewController中显示单例内容。
代码如下:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RootViewController *rootVC = [[RootViewController alloc] init];
UINavigationController *NC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = NC;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
单例
DataHandle.h
@interface DataHandle : NSObject
@property(nonatomic, retain) NSString *text;
+ (DataHandle *)shareInstance;
@end
DataHandle.m
@implementation DataHandle
static DataHandle *handle = nil;
+ (DataHandle *)shareInstance
{
//同步锁(为了避免多个线程同时访问同一个资源,导致资源不统一)
@synchronized (self){
if (!handle) {
handle = [[DataHandle alloc] init];
}
}
return handle;
}
@end
模态
ModalViewController.m
@interface ModalViewController ()
@property(nonatomic, retain) UITextField *textField;
@end
@implementation ModalViewController
- (void)dealloc
{
[_textField release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor magentaColor];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 30)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textField];
[_textField release];
[self createBarButtonItems];
}
- (void)createBarButtonItems
{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Dismiss" style:UIBarButtonItemStylePlain target:self action:@selector(didClickLeftBI:)];
}
- (void)didClickLeftBI:(UIBarButtonItem *)BI
{
//模态返回
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"模态返回");
}];
//获取单例对象
DataHandle *handle = [DataHandle shareInstance];
//为单例对象赋值
handle.text = _textField.text;
}
@end
RootViewController.m
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Modal" style:UIBarButtonItemStylePlain target:self action:@selector(toModalViewController:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"PUSH" style:UIBarButtonItemStylePlain target:self action:@selector(toNextView:)];
self.title = @"RootVC";
self.view.backgroundColor = [UIColor blueColor];
// Do any additional setup after loading the view.
}
- (void)toModalViewController:(UIBarButtonItem *)BI
{
ModalViewController *modalVC = [[ModalViewController alloc] init];
//模态推出modalVC,推出的modalVC和navigationController平级
//设置modalTransition
modalVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//给modalVC添加navigationController
UINavigationController *NC = [[UINavigationController alloc] initWithRootViewController:modalVC];
[self presentViewController:NC animated:YES completion:^{
NSLog(@"模态推出");
}];
[modalVC release];
NSLog(@"feqr");
}
- (void)toNextView:(UIBarButtonItem *)BI
{
FirstViewController *firstVC = [[FirstViewController alloc] init];
[self.navigationController pushViewController:firstVC animated:YES];
}
@end
FirstViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
//获取单例对象
DataHandle *dataHandle = [DataHandle shareInstance];
self.title = dataHandle.text;
self.view.backgroundColor = [UIColor redColor];
// Do any additional setup after loading the view.
}