主要实现在一个控制器上添加导航控制器,对于初学者比较有用。
接着我前两篇的登陆继续做,要完成登录成功后显示出导航控制器Nav,再通过Nav里的跳转实现UITableView,效果如下
![](http://dl.iteye.com/upload/attachment/350737/72181071-6d80-308b-aba9-cf28c00ddd1a.png)
![](http://dl.iteye.com/upload/attachment/350739/1ac2ca41-c620-3709-8cfa-794b9f7b261f.png)
![](http://dl.iteye.com/upload/attachment/350741/8db56ff5-4de1-3346-80f1-305db849d008.png)
![](http://dl.iteye.com/upload/attachment/350743/f761e048-cef4-306b-a5ae-f705cd4750fc.png)
首先对登录界面进行一下修改,在LoginViewController.h中新添加一个输出口
- @interface _1_11LoginViewController : UIViewController {
- IBOutlet UITextField *namefield;
- IBOutlet UITextField *passwordfield;
- IBOutlet UINavigationController *rootController;
- }
- @property (nonatomic,retain) UITextField *namefield;
- @property (nonatomic,retain) UITextField *passwordfield;
- @property (nonatomic,retain) UINavigationController *rootController;
- -(IBAction)login;
- -(IBAction)namefieldEditing:(id)sender;
- -(IBAction)changeTextFile;
- -(IBAction)doneLogin;
- @end
- -(IBAction)login{
- if (namefield.text.length<4||passwordfield.text.length<4) {
- NSLog(@"++++++++++");
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrong"
- message:@"They are not long enough"
- delegate:self
- cancelButtonTitle:@"I konw"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
- }else {
- [self.view.window addSubview:rootController.view];
- }
- }
![](http://dl.iteye.com/upload/attachment/350763/77a4034e-dbea-3cd1-b863-1e6007724d33.png)
![](http://dl.iteye.com/upload/attachment/350768/1099b3d5-3bda-3430-b5fb-0c441d94d05c.png)
![](http://dl.iteye.com/upload/attachment/350770/60914650-94c7-354c-979a-c7776ed11de0.png)
目的是将导航器的方向定位success类和其视图。
我们再对success.xib进行设置,只加入一个按钮,并做相应的关联,对他的.h文件进行编码
- @interface successLogin : UIViewController {
- }
- -(IBAction)showPressed;
- @end
对.m文件进行编码
- #import "successLogin.h"
- #import "ListViewController.h"
- @implementation successLogin
- -(IBAction)showPressed{
- ListViewController *myListViewController = [[ListViewController alloc] initWithNibName:@"ListViewController" bundle:nil];
- [self.navigationController pushViewController:myListViewController animated:YES];
- [myListViewController release];
- }
下面创建最后一个视图,向视图中拖入一个TableView,选中该控件,按花+2键,将delegate和dataSource都与File‘s Owner连接
![](http://dl.iteye.com/upload/attachment/350781/4adf19df-6641-3f65-a8be-6d198a13fac0.png)
对.h文件进行编码
- @interface ListViewController : UIViewController
- <UITableViewDelegate,UITableViewDataSource>{
- IBOutlet UITableView *myTableView;
- NSArray *friendList;
- }
- @property (nonatomic,retain)UITableView *myTableView;
- @property (nonatomic,retain)NSArray *friendList;
- @end
对.m文件进行编码
- #import "ListViewController.h"
- @implementation ListViewController
- @synthesize friendList;
- @synthesize myTableView;
- - (void)viewDidLoad {
- [super viewDidLoad];
- NSArray *array= [[NSArray alloc] initWithObjects:@"劳尔",@"穆里尼奥",@"卡卡",@"罗尼",@"小贝",nil];
- self.friendList=array;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- - (void)viewDidUnload {
- [super viewDidUnload];
- }
- - (void)dealloc {
- [myTableView release];
- [friendList release];
- [super dealloc];
- }
- //添加每一行的信息
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *tag=@"tag";
- UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];
- if (cell==nil) {
- cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero
- reuseIdentifier:tag] autorelease];
- }
- [cell.textLabel setText:[self.friendList objectAtIndex:[indexPath row]]];
- UIImage *image=[UIImage imageNamed:@"30.png"];//每行添加图片
- cell.image=image;
- return cell;
- }
- //添加行数
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [self.friendList count];
- }
- //使列表重复出现次数
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 6;
- }
- //选中哪一行
- -(NSIndexPath *)tableView:(UITableView *)tableView
- willSelectRowAtIndexPath:(NSIndexPath *)indexpath
- {
- NSUInteger row=[indexpath row];
- if (row==0) {
- return nil;
- }
- return indexpath;
- }
- //选中之后的处理
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSUInteger row=[indexPath row];
- NSString *rowvalue=[friendList objectAtIndex:row];
- NSString *message=[[NSString alloc] initWithFormat:@"你选中了 %@", rowvalue];
- UIAlertView *alert=[[UIAlertView alloc]
- initWithTitle:@"恭喜"
- message:message
- delegate:nil
- cancelButtonTitle:@"知道了"
- otherButtonTitles:nil];
- [alert show];
- [message release];
- [alert release];
- }
- @end
完成了。