zoukankan      html  css  js  c++  java
  • ios开发 NavigationController的使用

    以上就是导航栏的效果,导航栏在项目中应用很广泛,需要熟练掌握。

    新建项目,选择“Empty Application”,项目命名为:NavigationControllerTest

      新建一个UIViewController视图,命名为HomeViewConroller

    修改AppDeledate.h和AppDolegate.m源代码

    思路: 将home"push到”navigationController中,再将navigationController.View 添加到window中


    //  AppDelegate.m 

    #import <UIKit/UIKit.h>
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    {
        UINavigationController *navigationController;
    }

    @property (strong, nonatomic) UIWindow *window;

    @end 

    //  AppDelegate.m
    #import "AppDelegate.h"
    #import "HomeViewController.h"

    @implementation AppDelegate

    @synthesize window = _window;



    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        
        self.window.backgroundColor = [UIColor whiteColor];
        
        navigationController = [[UINavigationController alloc] init];
        HomeViewController *home = [[HomeViewController alloc] init];
        
        home.title = @"备忘录";
        
        [navigationController pushViewController:home animated:NO];
        [self.window addSubview:navigationController.view];
        [home release];
        
        [self.window makeKeyAndVisible];
        return YES;
    }

    - (void)dealloc
    {
        [navigationController release];
        [_window release];
        [super dealloc];
    }


    @end

    效果如下图:

     

    上面的只是一个页面,下面新建一个UIViewController视图“SecondViewController”,使项目在两个页面间切换。 

            在 HomeViewController.xib上添加button “进入第二个视图”

     HomeViewController中添加 - (IBAction)displaySecondView:(id)sender方法。


     - (void)displaySecondView:(id)sender

    {
        SecondViewController *secondViewConroller = [[SecondViewController alloc] init];
        //向视图询问它的导航控制器,因为在AppDelegate.m中我们已经在navigationController中添加了home,
        
    //所以这里我们询问home的导航控制器就会返回先前navigationController指针,如果没有就返回空
        [self.navigationController pushViewController:secondViewConroller animated:YES];
        secondViewConroller.title = @"第二个视图";
        [secondViewConroller release];
    }

    效果如下:

     

     当切换到SencondViewController,导航栏自动显示返回第一个视图的按钮。

    在导航栏上实现左右两个按钮。

    这个任务主要由UINavigationController上面的UINavigationItem来实现。
    在 UINavigationItem上添加UIBarButtonItem。

     在HomeViewController.m中修改- (void)viewDidLoad的代码:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"触摸" style:UIBarButtonItemStyleBordered target:self action:@selector(leftBarBtnClicked:)];
      self.navigationItem.leftBarButtonItem = leftBarBtn;   
     [leftBarBtn release];}
    - (void)leftBarBtnClicked:(id)sender
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"左边的BarButton被点击!" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

      效果如下图:

     

    还有一些按钮按钮和行为,系统已经帮我们定义好了,Edit和Done按钮行为,我们只要实现它就好了 。

    下面的和上面的原理一样,还是看代码吧,代码胜于一切华丽的言语。 

     代码如下:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"触摸" style:UIBarButtonItemStyleBordered target:self action:@selector(leftBarBtnClicked:)];
        self.navigationItem.leftBarButtonItem = leftBarBtn;    
           [leftBarBtn release];
        
        UIBarButtonItem *addBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBarBtnClicked:)];
        self.navigationItem.rightBarButtonItem = addBarBtn; 
        [addBarBtn release];
        
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
        
        NSArray *segmentButtons = [NSArray arrayWithObjects:@"升序"@"降序", nil];
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentButtons];
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        self.navigationItem.titleView = segmentedControl;
        [segmentedControl release];
        //导航到另一个视图后,修改返回的按钮的显示文字,默认是当前视图的导航标题,如“备忘录”
        UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
        self.navigationItem.backBarButtonItem = backBarBtn;
        [backBarBtn release];
                                       
    }

    - (void)leftBarBtnClicked:(id)sender
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"左边的BarButton被点击!" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

    - (void)addBarBtnClicked:(id)sender
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"右边的addBarButton被点击!" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
        [super setEditing:editing animated:animated];
        if (editing)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"edit" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"done" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }

    - (void)segmentAction:(id)sender
    {
        switch ([sender selectedSegmentIndex])
        {
            case 0:
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"升序" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
                [alert show];
                [alert release];
                break;
            }
            case 1:
            {
                UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"降序" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
                [alert1 show];
                [alert1 release];
                break;
            }
            default:
                break;
        } }

     

     

  • 相关阅读:
    php操作zip压缩文件
    php图像处理(thinkphp框架有相对强大的图像处理功能)
    php实现希尔排序(总结)
    PHP glob() 函数详解
    php资源类型变量
    dump var_dump print print_r的区别
    php实现遍历文件目录
    php常用函数
    php全局变量的使用
    php函数按地址传递参数(php引用)
  • 原文地址:https://www.cnblogs.com/hanjun/p/2772428.html
Copyright © 2011-2022 走看看