UIViewController-1:
UIViewController 介绍:
UIViewController 模式对话窗口跳转:
(1)创建一个继承于UIViewController类
(2)
#import "ccyViewController.h"
#import "childViewController.h"
@interface ccyViewController ()
@end
@implementation ccyViewController
- (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 orangeColor];
//视图控制器的位置
NSLog(@"x = %f",self.view.frame.origin.x);
NSLog(@"y = %f",self.view.frame.origin.y);
NSLog(@"w = %f",self.view.frame.size.width);
NSLog(@"h = %f",self.view.frame.size.height);
/*
x = 0.000000
y = 20.000000 - 视图控制器 不包括 状态栏的视图
w = 320.000000
h = 460.000000
*/
//视图跳转
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 30, 300, 30);
[btn setTitle:@"视图控制窗口切换" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)btnClick
{
//每次按下会新建一个
childViewController * chvn = [[childViewController alloc] init];
//切换方式,默认是从下想上。
/*
UIModalTransitionStyleCoverVertical = 0, - 默认 - 从下向上移动
UIModalTransitionStyleFlipHorizontal, - 横向翻转
UIModalTransitionStyleCrossDissolve, - 渐变cross
UIModalTransitionStylePartialCurl, - 翻书
*/
chvn.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:chvn animated:YES completion:^{
}];
[chvn release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "childViewController.h"
#import "ccyViewController.h"
@interface childViewController ()
@end
@implementation childViewController
- (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 greenColor];
//视图跳转
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 30, 300, 30);
[btn setTitle:@"关闭模式对话窗口" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)btnClick
{
//关闭模式对话窗口
[self dismissViewControllerAnimated:YES completion:^{
}];
//区别 [新增一个,这样多出来一个,并不是回到以前那个窗口,这个是错的]
//ccyViewController * chvn = [[ccyViewController alloc] init];
//[self presentViewController:chvn animated:YES completion:^{
//}];
//[chvn release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "ccyAppDelegate.h"
#import "ccyViewController.h"//增加头文件
@implementation ccyAppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
ccyViewController *cvm = [[ccyViewController alloc] init];
//这个是错误的,实际计数器加1是cvm.view,不是cvm
//[self.window addSubview:cvm.view];
//[cvm release];
// 设置一个视图控制器为主视图控制器 [默认颜色 是透明]
self.window.rootViewController = cvm;
[cvm release];
self.window.backgroundColor = [UIColor redColor];
[self.window makeKeyAndVisible];
return YES;
}