黑马程序员--UI学习之用代码向你展示视图控制器(UIViewController)和导航控制器(UINavigationController)配合单例的使用

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

主视图控制器UIViewController

main

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    MyViewController *mvc=[[MyViewController alloc]init];
    //设置一个视图控制器为主视图控制器】
    self.window.rootViewController=mvc;//将mvc设置为主视图控制器
    
    
    self.window.backgroundColor = [UIColor greenColor];
    [self.window makeKeyAndVisible];
    return YES;
}

MyViewController.h

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController

@end

MyViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor greenColor];//self是视图控制器
    NSLog(@"%f",self.view.frame.origin.x);
    NSLog(@"%f",self.view.frame.origin.y);
    NSLog(@"%f",self.view.frame.size.height);
    NSLog(@"%f",self.view.frame.size.width);

    UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame=CGRectMake(10, 30, 300, 30);
    [btn setTitle:@"窗体跳转" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];  //每个视图控制器都有一个view和一个control
}
- (void)btnClick{
    SubViewController *svc=[[SubViewController alloc]init];   //因为点击按钮进入下一页时都会实例化一个新的view,所以下一个界面的内容都是新的,如果不想让它刷新,那么就要设置一个全局变量
    svc.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;//设置翻页方式
    [self presentViewController:svc animated:YES completion:^{}]; //进入下一页

}<span style="color:#c91b13;">
</span>

2.导航控制器 UINavigationController,与单例在页面中的应用(红色标注是和单例有关的内容)
main
#import "LJLAppDelegate.h"
#import "MainViewController.h"
#import "SubViewController.h"
@implementation LJLAppDelegate
<span style="color:#ff0000;">@synthesize str=_str;</span>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
 //创建一个导航控制器的实力
    MainViewController *mvc=[[MainViewController alloc]init];

//会使mvc的引用计数加1
    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:mvc];

    self.window.rootViewController=navController;
    
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

MainViewController.m

#import "MainViewController.h"

#import "SubViewController.h"
#import "LJLAppDelegate.h"
@interface MainViewController ()

@end

@implementation MainViewController
@synthesize label=_label;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated{
    
    LJLAppDelegate *appdelegate=[UIApplication sharedApplication].delegate;
    
    _label.text=appdelegate.str;
    
    [super viewWillAppear:animated];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor greenColor];
    

//*****通往下一页的按钮
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setTitle:@"push到v2" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.frame=CGRectMake(10, 50, 300, 50);
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    
    _label = [[UILabel alloc]init];            //在首页建立一个标签,在第二页创建一个TextView
    _label.textColor=[UIColor yellowColor];
    _label.backgroundColor=[UIColor blueColor];
    _label.frame=CGRectMake(10, 100, 300, 80);
    _label.font=[UIFont boldSystemFontOfSize:40];
    _label.text=@"eoreri";
    [self.view addSubview:_label];
    
}
- (void)btnClick:(UIButton*)btn{
    SubViewController *svc = [[SubViewController alloc]init];
    //取到导航控制器的实例
    //导航控制器对视图控制器执行入栈操作的一种方法
    [self.navigationController pushViewController:svc animated:YES];

}

MainViewController.h

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController
{
    UILabel *label;
}
@property(retain,nonatomic) UILabel *label;
@end

SubViewController.h

#import <UIKit/UIKit.h>

@interface SubViewController : UIViewController

@end

SubViewController.m

#import "SubViewController.h"
#import "SubViewController1.h"
@interface SubViewController ()

@end

@implementation SubViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor redColor];

//创建一个按钮进入下一个界面
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 50, 150, 40);
    [btn setTitle:@"进入第三个视图" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
//创建一个按钮返回上一层
    UIButton *btn2= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame=CGRectMake(30, 100, 300, 50);
    [btn2 setTitle:@"退出" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(btn2Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
}
- (void)btn2Click{

    [self.navigationController popViewControllerAnimated:YES];//返回上一层 
}


- (void)btnClick{
    SubViewController1 *svc=[[SubViewController1 alloc]init];
    [self.navigationController pushViewController:svc animated:YES];//进入下一页
    

}<span style="color:#c32275;">
</span>

SubViewController1.h

#import <UIKit/UIKit.h>

@interface SubViewController1 : UIViewController
<span style="color:#ff0000;">{
    UITextView *tf;
}</span>
<span style="color:#cc0000;">@property UITextView *tf;</span>
@end

SubViewController1.m

#import "SubViewController1.h"
#import "MainViewController.h"
#import "LJLAppDelegate.h"

@interface SubViewController1 ()

@end

@implementation SubViewController1
<span style="color:#ff0000;">@synthesize tf=_tf;</span>


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor brownColor];
//创建按钮返回
    UIButton *btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame=CGRectMake(40, 80, 70, 40);
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnOnclick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    //返回首页
    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame=CGRectMake(50, 300, 300, 50);
    [btn1 setTitle:@"跳到首页" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(btn1Onclick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
<span style="color:#ff6600;">    </span>
<span style="color:#ff6600;">创建文本输入框
    _tf=[[UITextView alloc]init];
    _tf.frame=CGRectMake(10, 150, 300, 80);
    _tf.textColor=[UIColor blackColor];
    [self.view addSubview:_tf];</span>
    
    
    
}
#pragma mark 跳转到指定视图控制器
- (void)btn1Onclick{
<span style="color:#ff6600;">    LJLAppDelegate *appdelegate=[UIApplication sharedApplication].delegate;//单例就是一个路由器的感觉
    appdelegate.str=_tf.text;</span>
    
    NSArray *array=self.navigationController.viewControllers;//从栈中得到所有的视图
    [self.navigationController popToViewController:[array objectAtIndex:0] animated:YES];//返回到指定的图层
}
- (void)btnOnclick{

    [self.navigationController popToRootViewControllerAnimated:YES];//返回到首页
    

}








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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值