iOS学习笔记5-UIViewController(1)


          今天我们来学习一下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

效果图如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值