视图控制器-(多视图跳转的两种方式)

来源:互联网 发布:kvm虚机网络配置 编辑:程序博客网 时间:2024/05/14 12:17

AppDelegate.m:

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

//启动完成
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"%s",__func__);
    return YES;
}
//取消活跃
- (void)applicationWillResignActive:(UIApplication *)application {
    NSLog(@"%s",__func__);

}
//进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"%s",__func__);

}
//进入前台
- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"%s",__func__);

}
//(激活)活跃状态
- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSLog(@"%s",__func__);

}
//关闭
- (void)applicationWillTerminate:(UIApplication *)application {
    NSLog(@"%s",__func__);

}

@end

ViewController.m:

#import "ViewController.h"
#import "SeconViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {//加载视图
    [super viewDidLoad];
    NSLog(@"%s",__func__);
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewWillAppear:(BOOL)animated//UIViewController对象的视图即将加入窗口时调用
{
    [super viewWillAppear:animated];
    
    NSLog(@"%s", __func__);
}

- (void)viewWillLayoutSubviews//view即将布局其Subviews。比如view的bounds改变了(例如状态栏从不显示到显示,视图方向变化),要调整Subviews的位置,在调整之前要做的一些工作就可以在该方法中实现。
{
    [super viewWillLayoutSubviews];
    
    NSLog(@"%s", __func__);
}

- (void)viewDidLayoutSubviews//view已经布局其Subviews。比如view的bounds改变了(例如状态栏从不显示到显示,视图方向变化),已经调整Subviews的位置,在调整完成之后要做的一些工作就可以在该方法中实现。
{
    [super viewDidLayoutSubviews];
    
    NSLog(@"%s", __func__);
}

- (void)viewDidAppear:(BOOL)animated//UIViewController对象的视图已经加入到窗口时调用;
{
    [super viewDidAppear:animated];
    
    NSLog(@"%s", __func__);
}

- (void)viewWillDisappear:(BOOL)animated//UIViewController对象的视图即将消失、被覆盖或是隐藏时调用;
{
    [super viewWillDisappear:animated];
    
    NSLog(@"%s", __func__);
}

- (void)viewDidDisappear:(BOOL)animated//UIViewController对象的视图已经消失、被覆盖或是隐藏时调用;
{
    [super viewDidDisappear:animated];
    
    NSLog(@"%s", __func__);
}
- (IBAction)didJumpClicked:(id)sender {
    SeconViewController *secondViewCtrl = [[SeconViewController alloc] init];//调用任何初始化方法, 都会调用它的默认初始化方法, default initializer/signature initializer.....viewCtrl的view为nil
    secondViewCtrl.name = @"页面一";
//    secondViewCtrl.view.backgroundColor = [UIColor greenColor];
////不要使用这样的方式
//    secondViewCtrl.label.text = @"Zhangsan";
    NSLog(@"%@",secondViewCtrl.label.text);
    NSLog(@"%s***",__func__);
    [self presentViewController:secondViewCtrl animated:YES completion:nil];//模态视图(modal)
    NSLog(@"%s&&&",__func__);
    
}

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

- (void)dealloc
{
    NSLog(@"%s", __func__);
}

@end

SecondeViewController.h

#import <UIKit/UIKit.h>

@interface SeconViewController : UIViewController
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)UILabel *label;
@end

SecondeViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%s+++",__func__);
    
    self.view.backgroundColor = [UIColor greenColor];
    _label = [[UILabel alloc] initWithFrame:CGRectMake(150, 20, 60, 25)];
    _label.backgroundColor = [UIColor redColor];
    _label.text = @"页面一";
    [self.view addSubview:_label];
    
    _button = [[UIButton alloc] initWithFrame:CGRectMake(150, 200, 40, 20)];
    [self.view addSubview:_button];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"%s+++",__func__);
}
- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    NSLog(@"%s+++",__func__);
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    NSLog(@"%s+++",__func__);
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"%s+++",__func__);
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSLog(@"%s+++",__func__);
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"%s+++",__func__);
}

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


- (IBAction)didReturnClicked:(id)sender {
    NSLog(@"---%s+++",__func__);
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"***%s+++",__func__);
    }];
    NSLog(@"xxx%s+++",__func__);
}

- (void)dealloc
{
    NSLog(@"%s", __func__);
}

0 0
原创粉丝点击