导航控制器Nav和UITableView的使用(转载)

主要实现在一个控制器上添加导航控制器,对于初学者比较有用。


接着我前两篇的登陆继续做,要完成登录成功后显示出导航控制器Nav,再通过Nav里的跳转实现UITableView,效果如下








首先对登录界面进行一下修改,在LoginViewController.h中新添加一个输出口
C代码   收藏代码
  1. @interface _1_11LoginViewController : UIViewController {  
  2.     IBOutlet UITextField *namefield;  
  3.     IBOutlet UITextField *passwordfield;  
  4.     IBOutlet UINavigationController *rootController;  
  5. }  
  6. @property (nonatomic,retain) UITextField *namefield;  
  7. @property (nonatomic,retain) UITextField *passwordfield;  
  8. @property (nonatomic,retain) UINavigationController *rootController;  
  9. -(IBAction)login;  
  10. -(IBAction)namefieldEditing:(id)sender;  
  11. -(IBAction)changeTextFile;  
  12. -(IBAction)doneLogin;  
  13. @end  
对.h文件在修改
C代码   收藏代码
  1. -(IBAction)login{  
  2.     if (namefield.text.length<4||passwordfield.text.length<4) {  
  3.         NSLog(@"++++++++++");  
  4.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrong"   
  5.                             message:@"They are not long enough"   
  6.                             delegate:self   
  7.                             cancelButtonTitle:@"I konw"   
  8.                             otherButtonTitles:nil];  
  9.         [alert show];  
  10.         [alert release];  
  11.     }else {  
  12.         [self.view.window addSubview:rootController.view];  
  13.     }     
  14. }  
再创建两个新类和相应的.xib文件,分别取名successLogin,listViewController。我们再去关注一下LoginViewController.xib文件,拖一个navigation Controller图标到nib主窗口中,那么则新弹出一个视图窗口,按住Ctrl将File‘s Owner拖向Navigation Controller,在选中nib主窗口的第二项
点选图中蓝色区域,再看他的控制器,分别对其修改为



目的是将导航器的方向定位success类和其视图。
我们再对success.xib进行设置,只加入一个按钮,并做相应的关联,对他的.h文件进行编码

C代码   收藏代码
  1. @interface successLogin : UIViewController {      
  2. }  
  3. -(IBAction)showPressed;  
  4. @end  

对.m文件进行编码
C代码   收藏代码
  1. #import "successLogin.h"  
  2. #import "ListViewController.h"  
  3. @implementation successLogin  
  4. -(IBAction)showPressed{  
  5.     ListViewController *myListViewController = [[ListViewController alloc] initWithNibName:@"ListViewController" bundle:nil];  
  6.     [self.navigationController pushViewController:myListViewController animated:YES];  
  7.     [myListViewController release];  
  8. }  

下面创建最后一个视图,向视图中拖入一个TableView,选中该控件,按花+2键,将delegate和dataSource都与File‘s Owner连接


对.h文件进行编码
C代码   收藏代码
  1. @interface ListViewController : UIViewController   
  2.         <UITableViewDelegate,UITableViewDataSource>{  
  3.             IBOutlet UITableView *myTableView;  
  4.             NSArray *friendList;  
  5. }  
  6. @property (nonatomic,retain)UITableView *myTableView;  
  7. @property (nonatomic,retain)NSArray *friendList;  
  8. @end  

对.m文件进行编码
C代码   收藏代码
  1. #import "ListViewController.h"  
  2. @implementation ListViewController  
  3. @synthesize friendList;  
  4. @synthesize myTableView;  
  5. - (void)viewDidLoad {  
  6.     [super viewDidLoad];  
  7.     NSArray *array= [[NSArray alloc] initWithObjects:@"劳尔",@"穆里尼奥",@"卡卡",@"罗尼",@"小贝",nil];  
  8.       
  9.     self.friendList=array;  
  10. }  
  11. - (void)didReceiveMemoryWarning {  
  12.     [super didReceiveMemoryWarning];  
  13. }  
  14. - (void)viewDidUnload {  
  15.     [super viewDidUnload];  
  16. }  
  17. - (void)dealloc {  
  18.     [myTableView release];  
  19.     [friendList release];  
  20.     [super dealloc];  
  21. }  
  22.   
  23. //添加每一行的信息  
  24. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  25. {  
  26.     static NSString *tag=@"tag";  
  27.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];  
  28.     if (cell==nil) {  
  29.         cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero  
  30.                                      reuseIdentifier:tag] autorelease];  
  31.     }  
  32.     [cell.textLabel setText:[self.friendList objectAtIndex:[indexPath row]]];  
  33.     UIImage *image=[UIImage imageNamed:@"30.png"];//每行添加图片  
  34.     cell.image=image;  
  35.     return cell;  
  36. }  
  37.   
  38. //添加行数  
  39. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  40. {  
  41.     return [self.friendList count];  
  42. }  
  43.   
  44. //使列表重复出现次数  
  45. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  46. {  
  47.     return 6;  
  48. }  
  49.   
  50.   
  51. //选中哪一行  
  52.   
  53. -(NSIndexPath *)tableView:(UITableView *)tableView  
  54.  willSelectRowAtIndexPath:(NSIndexPath *)indexpath  
  55. {  
  56.     NSUInteger row=[indexpath row];  
  57.     if (row==0) {  
  58.         return nil;  
  59.     }  
  60.     return indexpath;  
  61. }  
  62.   
  63. //选中之后的处理  
  64. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  65.   
  66. {  
  67.     NSUInteger row=[indexPath row];  
  68.     NSString *rowvalue=[friendList objectAtIndex:row];  
  69.     NSString *message=[[NSString alloc] initWithFormat:@"你选中了 %@", rowvalue];  
  70.     UIAlertView *alert=[[UIAlertView alloc]  
  71.                           
  72.                         initWithTitle:@"恭喜"  
  73.                           
  74.                         message:message  
  75.                           
  76.                         delegate:nil  
  77.                           
  78.                         cancelButtonTitle:@"知道了"  
  79.                           
  80.                         otherButtonTitles:nil];  
  81.       
  82.     [alert show];  
  83.       
  84.     [message release];  
  85.       
  86.     [alert release];  
  87.       
  88. }  
  89. @end  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值