Iphone和android手机的一个不同的地方是,大部分的android手机都有返回键,而Iphone只有一个home键,所以我们会发现在Iphone的大部分应用中会在顶部有一个导航条,比如系统的设置界面,该导航条完全按照栈的方式来管理,所以可以很方便的实现后退的操作:

iOS UISearchController如何使用 apple universal control_iPhone开发

iOS UISearchController如何使用 apple universal control_iPhone开发_02

 

总结一下导航条的使用;

导航栏这个控件称为UINavigationController,常常用来作根视图控制器,生成对象后可以用该对象push UIViewController的对象,这样该UIViewController的对象就加到导航条的下部了,可以给视图控制器加title,会显示在导航栏上,也可以修改返回键的title,如右上图的左上角的setting按钮,如果默认的话是没有viewController的title的,返回键的title会是viewController的名字,接下来我们要用Xcode4.3中最基础的模板EmptyApplication来从头创建一个最原始的导航demo;

首先,新建一个project,选择EmptyApplication模板;

iOS UISearchController如何使用 apple universal control_导航条_03

这样生成后是只有委托类的:

iOS UISearchController如何使用 apple universal control_导航条_04

然后new两个ViewController出来,为了方便操作记得要附上xib文件;起名为FirstViewController,SecondViewController,在FirstViewController的xib文件中拖上一个button,连接一个nextClick方法用来切换到下一个视图;

iOS UISearchController如何使用 apple universal control_导航条_05

现在开始操作代码

AppDelegate.h:


[plain] view plain copy print ?


1. <SPAN style="FONT-SIZE: 18px">#import <UIKit/UIKit.h>  
2.   
3. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
4.   
5. @property (strong, nonatomic) UIWindow *window;  
6. @property (strong, nonatomic)  UINavigationController *naviController;  
7. @end</SPAN>


#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic)  UINavigationController *naviController;
@end


AppDelegate.m:

[plain] view plain copy print ?


    1. <SPAN style="FONT-SIZE: 18px">#import "AppDelegate.h"  
    2. #import "FirstViewController.h"  
    3. @implementation AppDelegate  
    4.   
    5. @synthesize window = _window;  
    6. @synthesize naviController;  
    7.   
    8. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    9. {  
    10.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
    11.       
    12.     //生成一个ViewControlle对象作为导航栏的第一个视图;  
    13.     FirstViewController *firstView = [[FirstViewController alloc]init];  
    14.       
    15.     naviController = [[UINavigationController alloc]initWithRootViewController:firstView];  
    16.     //将该导航栏作为根视图控制器;  
    17.     self.window.rootViewController = naviController ;  
    18.     [self.window makeKeyAndVisible];  
    19.     return YES;  
    20. }  
    21. </SPAN>


    #import "AppDelegate.h"
    #import "FirstViewController.h"
    @implementation AppDelegate
    
    @synthesize window = _window;
    @synthesize naviController;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        
        //生成一个ViewControlle对象作为导航栏的第一个视图;
        FirstViewController *firstView = [[FirstViewController alloc]init];
        
        naviController = [[UINavigationController alloc]initWithRootViewController:firstView];
        //将该导航栏作为根视图控制器;
        self.window.rootViewController = naviController ;
        [self.window makeKeyAndVisible];
        return YES;
    }


    FirstViewController.h:

    [plain] view plain copy print ?

    1. <SPAN style="FONT-SIZE: 18px">#import <UIKit/UIKit.h>  
    2.   
    3. @interface FirstViewController : UIViewController  
    4. - (IBAction)nextClick:(id)sender;  
    5.   
    6. @end</SPAN>

     


    #import <UIKit/UIKit.h>
    
    @interface FirstViewController : UIViewController
    - (IBAction)nextClick:(id)sender;
    
    @end


    FirstViewController.m:

    [plain] view plain copy print ?


    1. <SPAN style="FONT-SIZE: 18px">#import "FirstViewController.h"  
    2. #import "SecondViewController.h"  
    3. @interface FirstViewController ()  
    4.   
    5. @end  
    6.   
    7. @implementation FirstViewController  
    8.   
    9. - (IBAction)nextClick:(id)sender {  
    10.     SecondViewController *secondView = [[SecondViewController alloc]init];  
    11.     [self.navigationController pushViewController:secondView animated:YES];  
    12. }</SPAN>


    #import "FirstViewController.h"
    #import "SecondViewController.h"
    @interface FirstViewController ()
    
    @end
    
    @implementation FirstViewController
    
    - (IBAction)nextClick:(id)sender {
        SecondViewController *secondView = [[SecondViewController alloc]init];
        [self.navigationController pushViewController:secondView animated:YES];
    }


    上面只是贴出了需要修改的代码,其余自动生成的都不用管,甚至SecondViewController都没有进行操作,所以我们会看到这样一种最简单的导航控制效果;

    iOS UISearchController如何使用 apple universal control_#import_06

    iOS UISearchController如何使用 apple universal control_导航栏_07

    关键字:UINavigationController , IOS ,Iphone 开发 ,导航控制器 



                    Iphone和android手机的一个不同的地方是,大部分的android手机都有返回键,而Iphone只有一个home键,所以我们会发现在Iphone的大部分应用中会在顶部有一个导航条,比如系统的设置界面,该导航条完全按照栈的方式来管理,所以可以很方便的实现后退的操作:

    iOS UISearchController如何使用 apple universal control_iPhone开发

    iOS UISearchController如何使用 apple universal control_iPhone开发_02

     

    总结一下导航条的使用;

    导航栏这个控件称为UINavigationController,常常用来作根视图控制器,生成对象后可以用该对象push UIViewController的对象,这样该UIViewController的对象就加到导航条的下部了,可以给视图控制器加title,会显示在导航栏上,也可以修改返回键的title,如右上图的左上角的setting按钮,如果默认的话是没有viewController的title的,返回键的title会是viewController的名字,接下来我们要用Xcode4.3中最基础的模板EmptyApplication来从头创建一个最原始的导航demo;

    首先,新建一个project,选择EmptyApplication模板;

    iOS UISearchController如何使用 apple universal control_导航条_03

    这样生成后是只有委托类的:

    iOS UISearchController如何使用 apple universal control_导航条_04

    然后new两个ViewController出来,为了方便操作记得要附上xib文件;起名为FirstViewController,SecondViewController,在FirstViewController的xib文件中拖上一个button,连接一个nextClick方法用来切换到下一个视图;

    iOS UISearchController如何使用 apple universal control_导航条_05

    现在开始操作代码

    AppDelegate.h:


    [plain] view plain copy print ?


    1. <SPAN style="FONT-SIZE: 18px">#import <UIKit/UIKit.h>  
    2.   
    3. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
    4.   
    5. @property (strong, nonatomic) UIWindow *window;  
    6. @property (strong, nonatomic)  UINavigationController *naviController;  
    7. @end</SPAN>


    #import <UIKit/UIKit.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    @property (strong, nonatomic)  UINavigationController *naviController;
    @end


    AppDelegate.m:

    [plain] view plain copy print ?


    1. <SPAN style="FONT-SIZE: 18px">#import "AppDelegate.h"  
    2. #import "FirstViewController.h"  
    3. @implementation AppDelegate  
    4.   
    5. @synthesize window = _window;  
    6. @synthesize naviController;  
    7.   
    8. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    9. {  
    10.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
    11.       
    12.     //生成一个ViewControlle对象作为导航栏的第一个视图;  
    13.     FirstViewController *firstView = [[FirstViewController alloc]init];  
    14.       
    15.     naviController = [[UINavigationController alloc]initWithRootViewController:firstView];  
    16.     //将该导航栏作为根视图控制器;  
    17.     self.window.rootViewController = naviController ;  
    18.     [self.window makeKeyAndVisible];  
    19.     return YES;  
    20. }  
    21. </SPAN>


    #import "AppDelegate.h"
    #import "FirstViewController.h"
    @implementation AppDelegate
    
    @synthesize window = _window;
    @synthesize naviController;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        
        //生成一个ViewControlle对象作为导航栏的第一个视图;
        FirstViewController *firstView = [[FirstViewController alloc]init];
        
        naviController = [[UINavigationController alloc]initWithRootViewController:firstView];
        //将该导航栏作为根视图控制器;
        self.window.rootViewController = naviController ;
        [self.window makeKeyAndVisible];
        return YES;
    }


    FirstViewController.h:

    [plain] view plain copy print ?

    1. <SPAN style="FONT-SIZE: 18px">#import <UIKit/UIKit.h>  
    2.   
    3. @interface FirstViewController : UIViewController  
    4. - (IBAction)nextClick:(id)sender;  
    5.   
    6. @end</SPAN>


    #import <UIKit/UIKit.h>
    
    @interface FirstViewController : UIViewController
    - (IBAction)nextClick:(id)sender;
    
    @end


    FirstViewController.m:

    [plain] view plain copy print ?

    1. <SPAN style="FONT-SIZE: 18px">#import "FirstViewController.h"  
    2. #import "SecondViewController.h"  
    3. @interface FirstViewController ()  
    4.   
    5. @end  
    6.   
    7. @implementation FirstViewController  
    8.   
    9. - (IBAction)nextClick:(id)sender {  
    10.     SecondViewController *secondView = [[SecondViewController alloc]init];  
    11.     [self.navigationController pushViewController:secondView animated:YES];  
    12. }</SPAN>

    #import "FirstViewController.h"
    #import "SecondViewController.h"
    @interface FirstViewController ()
    
    @end
    
    @implementation FirstViewController
    
    - (IBAction)nextClick:(id)sender {
        SecondViewController *secondView = [[SecondViewController alloc]init];
        [self.navigationController pushViewController:secondView animated:YES];
    }


    上面只是贴出了需要修改的代码,其余自动生成的都不用管,甚至SecondViewController都没有进行操作,所以我们会看到这样一种最简单的导航控制效果;

    iOS UISearchController如何使用 apple universal control_#import_06

    iOS UISearchController如何使用 apple universal control_导航栏_07

    关键字:UINavigationController , IOS ,Iphone 开发 ,导航控制器