您的位置:首页 > 产品设计 > UI/UE

UI_UIViewController视图控制器_推出新视图presentViewController(模态推出)

2014-10-14 19:31 471 查看
控件关键字: presentViewController

本篇主要介绍UIViewController 的架构, 数据处理的逻辑关系. 之后会介绍一种推出新视图的方法,不同于(UINavigation, 视图导航)

下面是一个ViewController.m文件

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
// 指定初始化方法
// 无论调用系统的哪个初始化方法,都会调用这个初始化方法
NSLog(@"%s", __func__);

}
return self;
}

- (void)loadView
{
// 系统调用这个方法产生一个View, 作为自己自带的视图
[super loadView];
NSLog(@"%s", __func__);
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

// 视图将要出现
NSLog(@"%s", __func__);
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// 视图已经出现
NSLog(@"%s", __func__);

}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

// 控制器自带的视图创建完毕就会调用这个方法
// 视图创建的代码写在这个地方

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

// 当App的内存过高, 系统会调用这个方法
// 可以在这个方法中释放掉一些可以重复生成的对象(数据/视图...)
}


推出新视图presentViewController
Xcode工程中, 建立两个UIViewController



在主视图中加入一个Button, 设置Button一个点击事件, 推出第二视图(SecondViewController)

MainViewController.m 文件中, 导入第二视图的.h 头文件

#import "MainViewController.h"
#import "SecondViewController.h"


创建Button

- (void)viewDidLoad
{

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColor yellowColor];
button.frame = CGRectMake(20, 20, 280, 50);
[self.view addSubview:button];
[button setTitle:@"消息" forState:UIControlStateNormal];

[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

}


Button事件

- (void)buttonClicked:(UIButton *)button
{

// 模态推出Model

// 推出一个新的界面(viewController)

// 1.创建新页面
SecondViewController *secondVC = [[SecondViewController alloc] init];

// 设置推出方法(枚举)
[secondVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

// 2.推出
// 参数1, 要推出的新视图控制器(viewController)
// 参数2, 是不是带动画效果
[self presentViewController:secondVC animated:YES completion:^{
// 推出新视图之后,要执行的代码

}];

// 3.内存管理
[secondVC release];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐