今天我们来学习一下iOS开发的另一个重要的类:UIViewController。熟练掌握UIView和UIViewcontroller这两个重要的类,是成为优秀的iOS developer的前提。
UIViewcontroller其实就是控制页面的切换,下面我们以一个最简单的两页切换的demo来感受下UIVIewcontroller的工作工程。
这里做一下解释,在前面的一个demo中,因为简单,所以LZ直接在appDelegate里面对页面进行了编辑,但是最好还是将每个界面都进行封装,这样便于以后管理。所以今天我们不在appDelegate里面编辑页面,而是新建了两个XXViewController,进行编辑。
UIViewAppDelegate.h代码如下:
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface UIViewAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) FirstViewController *firstViewController;
UIViewAppDelegate.m代码如下:
#import "UIViewAppDelegate.h"
@implementation UIViewAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstViewController *firstViewController = [[FirstViewController alloc]init];
self.window.rootViewController = firstViewController;
[self.window makeKeyAndVisible];
return YES;
}
。。。。。。。。。。
FirstViewController.m代码如下
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//初始化标签
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x-30, self.view.center.y-100, 60, 30)];
[label1 setText:@"第一页"];
label1.backgroundColor = [UIColor whiteColor];
[self.view addSubview:label1];
//加入一个button,定义一个触发事件,此事件就是一旦点击button,就执行ChangeAction事件
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"跳转" forState:UIControlStateNormal];
[button sizeToFit];
button.center = CGPointMake(160, 400);
[button addTarget:self action:@selector(ChangeAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//初始化secondViewController
- (void)ChangeAction{
NSLog(@"no.1");
SecondViewController *secondViewController = [[SecondViewController alloc] init];
[self presentViewController:secondViewController animated:YES completion:nil ];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecondViewController.m代码如下
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x-30, self.view.center.y-100, 60, 30)];
[label1 setText:@"第二页"];
[self.view addSubview:label1];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"跳转" forState:UIControlStateNormal];
[button sizeToFit];
button.center = CGPointMake(160, 400);
[button addTarget:self action:@selector(ChangeAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)ChangeAction{
//返回上一页面
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
效果图如下: