导航控制器Nav和UITableView的使用

[size=x-large]接着我前两篇的登陆继续做,要完成登录成功后显示出导航控制器Nav,再通过Nav里的跳转实现UITableView,效果如下[/size]
[img]http://dl.iteye.com/upload/attachment/350737/72181071-6d80-308b-aba9-cf28c00ddd1a.png[/img]

[img]http://dl.iteye.com/upload/attachment/350739/1ac2ca41-c620-3709-8cfa-794b9f7b261f.png[/img]

[img]http://dl.iteye.com/upload/attachment/350741/8db56ff5-4de1-3346-80f1-305db849d008.png[/img]

[img]http://dl.iteye.com/upload/attachment/350743/f761e048-cef4-306b-a5ae-f705cd4750fc.png[/img]
[size=x-large]首先对登录界面进行一下修改,在LoginViewController.h中新添加一个输出口[/size]
@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
[size=x-large]对.h文件在修改[/size]
-(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];
}
}
[size=x-large]再创建两个新类和相应的.xib文件,分别取名successLogin,listViewController。我们再去关注一下LoginViewController.xib文件,拖一个navigation Controller图标到nib主窗口中,那么则新弹出一个视图窗口,按住Ctrl将File‘s Owner拖向Navigation Controller,在选中nib主窗口的第二项[/size]
[img]http://dl.iteye.com/upload/attachment/350763/77a4034e-dbea-3cd1-b863-1e6007724d33.png[/img][size=x-large]点选图中蓝色区域,再看他的控制器,分别对其修改为[/size]
[img]http://dl.iteye.com/upload/attachment/350768/1099b3d5-3bda-3430-b5fb-0c441d94d05c.png[/img]

[img]http://dl.iteye.com/upload/attachment/350770/60914650-94c7-354c-979a-c7776ed11de0.png[/img]
[size=x-large]目的是将导航器的方向定位success类和其视图。
我们再对success.xib进行设置,只加入一个按钮,并做相应的关联,对他的.h文件进行编码[/size]
@interface successLogin : UIViewController {	
}
-(IBAction)showPressed;
@end

[size=x-large]对.m文件进行编码[/size]
#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];
}

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

[img]http://dl.iteye.com/upload/attachment/350781/4adf19df-6641-3f65-a8be-6d198a13fac0.png[/img]
[size=x-large]对.h文件进行编码[/size]
@interface ListViewController : UIViewController 
<UITableViewDelegate,UITableViewDataSource>{
IBOutlet UITableView *myTableView;
NSArray *friendList;
}
@property (nonatomic,retain)UITableView *myTableView;
@property (nonatomic,retain)NSArray *friendList;
@end

[size=x-large]对.m文件进行编码[/size]

#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

[size=x-large]完成了。[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值