zoukankan      html  css  js  c++  java
  • IOS开发-UI学习-UINavigationController(导航控制器)的使用

    UINavigationController是IOS 中常用的功能,基本用法如下:

    1、在AppDelegate.m中添加如下代码:

     1 #import "AppDelegate.h"
     2 #import "MainViewController.h"
     3 
     4 @interface AppDelegate ()
     5 
     6 @end
     7 
     8 @implementation AppDelegate
     9 
    10 
    11 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    12    
    13     
    14 //   注意:如果保留storyboard中的viewcontroller的话,就不用第16行到20行的创建window的语句
    15     
    16 //    创建window,设置背景色
    17     self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    18     self.window.backgroundColor = [UIColor colorWithRed:0.638 green:0.876 blue:1.000 alpha:1.000];
    19 //    让当前window称为主窗口
    20     [self.window makeKeyAndVisible];
    21     
    22     
    23     
    24     
    25 //    设置window的根视图
    26     MainViewController *mainVC = [[MainViewController alloc]init];
    27     
    28 //    设置导航控制器的根视图为mainviewcontroller类的实例mainVC
    29     UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:mainVC];
    30     
    31 //    设置window的根视图为nav
    32     self.window.rootViewController = nav;
    33     
    34     
    35 //    给导航栏着色
    36     nav.navigationBar.barTintColor = [UIColor colorWithRed:1.000 green:0.400 blue:1.000 alpha:1.000];
    37     
    38 //    给导航栏添加图片
    39     [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"barimage"] forBarMetrics:UIBarMetricsDefault];
    40     
    41     
    42     
    43     
    44     return YES;
    45 }

    注意:使用以上功能时先在Main.storyboard中删除viewcontroller,然后添加十六到二十行语句,如果不删除Main.storyboard中的viewcontroller的话,就不需要使用十六到二十行多语句来添加window。

    新建MainViewController类,继承自UIViewController,然后在MainViewController.m中添加以下代码:

     1 #import "MainViewController.h"
     2 #import "FirstViewController.h"
     3 @interface MainViewController ()
     4 
     5 @end
     6 
     7 @implementation MainViewController
     8 
     9 - (void)viewDidLoad {
    10     [super viewDidLoad];
    11     
    12 //    设置标题
    13 //    self.navigationItem.title = @"微信";
    14     
    15 //    设置按钮
    16     UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    17     btn1.backgroundColor = [UIColor redColor];
    18     [btn1 addTarget:self action:@selector(gotofirst) forControlEvents:UIControlEventTouchUpInside];
    19     [self.view addSubview:btn1];
    20     
    21     
    22 //    自定义标题(按钮、可点击)
    23     UIButton *titlebtn = [UIButton buttonWithType:UIButtonTypeCustom];
    24     titlebtn.frame = CGRectMake(0, 0, 100, 44);
    25     [titlebtn setTitle:@"我可以点击" forState: UIControlStateNormal];
    26     [titlebtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    27     titlebtn.titleLabel.font = [UIFont systemFontOfSize:16];
    28     [titlebtn addTarget:self action:@selector(titlebtnAction) forControlEvents:UIControlEventTouchUpInside];
    29     self.navigationItem.titleView = titlebtn;
    30     
    31     
    32 //    自定义左、右按键
    33     UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"左边" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction)];
    34     
    35     UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"右边" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction)];
    36     
    37 //    self.navigationItem.leftBarButtonItem = leftItem;
    38 //    self.navigationItem.rightBarButtonItem = rightItem;
    39     
    40 //    使用数组给右边添加多个按钮
    41     self.navigationItem.rightBarButtonItems = @[leftItem,rightItem];
    42     
    43     
    44     
    45     
    46 
    47 }
    48 
    49 -(void)rightItemAction{
    50      NSLog(@"右边按钮被点击了");
    51 }
    52 
    53 
    54 -(void)leftItemAction{
    55     NSLog(@"左边按钮被点击了");
    56 }
    57 
    58 -(void)gotofirst{
    59     FirstViewController *firstVC = [[FirstViewController alloc]init];
    60     [self.navigationController pushViewController:firstVC animated:YES];
    61     
    62 }
    63 -(void)titlebtnAction{
    64     NSLog(@"我被点击了");
    65 }
    66 
    67 
    68 
    69 - (void)didReceiveMemoryWarning {
    70     [super didReceiveMemoryWarning];
    71     // Dispose of any resources that can be recreated.
    72 }
    73 
    74 /*
    75 #pragma mark - Navigation
    76 
    77 // In a storyboard-based application, you will often want to do a little preparation before navigation
    78 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    79     // Get the new view controller using [segue destinationViewController].
    80     // Pass the selected object to the new view controller.
    81 }
    82 */
    83 
    84 @end

    新建FirstViewController类,然后在FirstViewController.m中添加以下代码:

     1 #import "FirstViewController.h"
     2 #import "MainViewController.h"
     3 
     4 @interface FirstViewController ()
     5 
     6 @end
     7 
     8 @implementation FirstViewController
     9 
    10 - (void)viewDidLoad {
    11     [super viewDidLoad];
    12     
    13     self.view.backgroundColor = [UIColor blackColor];
    14     
    15     
    16     
    17     //    设置按钮
    18     UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    19     btn1.backgroundColor = [UIColor redColor];
    20     [btn1 addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
    21     [self.view addSubview:btn1];
    22 }
    23 
    24 
    25 -(void)goback{
    26     
    27 //按指定返回
    28 ////    返回根视图
    29 //    [self.navigationController popToRootViewControllerAnimated:YES];
    30 //    
    31 ////    返回上一个视图
    32 //    [self.navigationController popViewControllerAnimated:YES];
    33     
    34     
    35     
    36 //通过循环比较返回
    37     for (UIViewController *tmp in self.navigationController.viewControllers) {
    38         if ([tmp isKindOfClass:[MainViewController class]]) {
    39             [self.navigationController popToViewController:tmp animated:YES];
    40         }
    41     }
    42     
    43     
    44 }
    45 
    46 
    47 - (void)didReceiveMemoryWarning {
    48     [super didReceiveMemoryWarning];
    49     // Dispose of any resources that can be recreated.
    50 }
    51 
    52 /*
    53 #pragma mark - Navigation
    54 
    55 // In a storyboard-based application, you will often want to do a little preparation before navigation
    56 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    57     // Get the new view controller using [segue destinationViewController].
    58     // Pass the selected object to the new view controller.
    59 }
    60 */
    61 
    62 @end

    通过以上代码就可以实现两个viewcontroller的切换,是通过导航控制器实现的。

  • 相关阅读:
    ajax提交form表单
    数组算法
    option标签如何获取显示信息
    web.xml的作用
    getServletContext()和getServletConfig()及JAVA当前路径解决
    js提交表单
    Apache Commons fileUpload实现文件上传
    笔记本电脑突然没有声音
    作业调度方案题解
    VScode运行python文件无反应(使用Code Runner)
  • 原文地址:https://www.cnblogs.com/jiwangbujiu/p/5356222.html
Copyright © 2011-2022 走看看