IOS成长之路-导航栏的实现

创建这样一个项目:


在Model中创建一个简单的 view 界面,而  ViewController  类是一个表格的界面


创建表格的界面:

第一步:



第二步:

注意:  Subclass of  的选项要选择 UITableViewController



然后下一步,最后点击create就可以了


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


在ViewController这个界面中创建导航添加进去

  1. /*AppDelegate.h*/  
  2.   
  3. #import <UIKit/UIKit.h>  
  4.   
  5. @class ViewController;  
  6. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  7. @property (strong, nonatomic) UIWindow *window;  
  8.   
  9. //添加导航控制器  
  10. @property (strong,nonatomic) UINavigationController *iNav;  
  11.   
  12. //添加ViewController界面,添加这个界面的目的是为了把导航加入到这个界面中  
  13. @property (strong,nonatomic) ViewController *viewController;  
  14.   
  15. @end  

  1. /*AppDelegate.m*/  
  2.   
  3. #import "AppDelegate.h"  
  4. #import "ViewController.h"  
  5. @implementation AppDelegate  
  6.   
  7. @synthesize window = _window;  
  8. //-----------------  
  9. @synthesize iNav = _iNav;  
  10. @synthesize viewController = _viewController;  
  11. //-----------------  
  12. - (void)dealloc  
  13. {  
  14.     [_iNav release];  
  15.     [_viewController release];  
  16.       
  17.     [_window release];  
  18.     [super dealloc];  
  19. }  
  20.   
  21. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  22. {  
  23.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  24.     // Override point for customization after application launch.  
  25.     self.window.backgroundColor = [UIColor whiteColor];  
  26.     [self.window makeKeyAndVisible];  
  27.       
  28.     /*---------------添加的内容----------------*/  
  29.     self.viewController = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];  
  30.       
  31.     //initWithRootViewController  基于哪个页面导航(就是把导航加到哪个页面中)  
  32.     self.iNav = [[UINavigationController alloc]initWithRootViewController:self.viewController];  
  33.       
  34.     //添加到window中  
  35.     [self.window addSubview:self.iNav.view];  
  36.     //或下面这种方法  
  37.     //self.window.rootViewController = self.iNav;  
  38.       
  39.       
  40.     return YES;  
  41. }  



然后在 ViewController.m 类中为导航栏写一个title

在    - (void)viewDidLoad  这个方法中添加:

  1. self.title = @"这是一个导航";  

到这里就实现了在这个界面中添加一个导航栏。

如图:



=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


但是到这里并没有体现出导航的作用,所以,我们进行下面的操作:

这个界面是一个表格界面,在这个界面中添加内容,然后通过它切换到另外一个界面中,从而实现导航的作用:通过导航返回第一个界面中


还是在  ViewController.m  这个类中:

在  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  方法中的  

return cell;  前加入这样一段代码:

  1. //输入内容  
  2.     cell.textLabel.text = @"helloworld";  
  3.     //得到每一行的索引值  int num = indexPath.row;  

在这个类中有这样两个方法,分别是设置有几块区域和每块区域有几行

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  2. {  
  3.     // Return the number of sections.  
  4.     // 返回有几块区域  
  5.     return 1;  
  6. }  
  7.   
  8. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  9. {  
  10.     // Return the number of rows in the section.  
  11.     return 2;// 返回一个区域有几行  
  12. }  


这个时候的界面是:





点击  helloworld  进入下一个界面,就是 UiViewController.xib 界面中,在这个界面中有一个 label 控件


还是在ViewController.m这个类中实现切换界面的操作:

 
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.       
  4.     UiViewController *viewController = [[UiViewController alloc]initWithNibName:@"UIViewController" bundle:nil];  
  5.     //navigationController 是从 UIViewController 中继承下来的属性,通过这个属性来调用它的压栈方法,来实现界面的切换  
  6.     /* 
  7.      @property(nonatomic,readonly,retain) UINavigationController *navigationController; // If this view controller has been pushed onto a navigation controller, return it. 
  8.      */  
  9.     [self.navigationController pushViewController:viewController animated:YES];  
  10.       
  11.       
  12.       
  13.     // Navigation logic may go here. Create and push another view controller.  
  14.     /* 
  15.      <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
  16.      // ... 
  17.      // Pass the selected object to the new view controller. 
  18.      [self.navigationController pushViewController:detailViewController animated:YES]; 
  19.      [detailViewController release]; 
  20.      */  
  21. }  

注意:不要忘记添加头文件 :

#import"UiViewController.h"


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

好了,这样就实现了导航的作用了



点击helloworld 进入下一个界面。




进入到第二个界面中可以看到,导航栏的位置上有一个返回的按钮,点击就可以返回第一个界面了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值