您的位置:首页 > 产品设计 > UI/UE

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

2011-10-11 09:46 411 查看
主要实现在一个控制器上添加导航控制器,对于初学者比较有用。

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









首先对登录界面进行一下修改,在LoginViewController.h中新添加一个输出口

C代码


@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

对.h文件在修改

C代码


-(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];
}
}

再创建两个新类和相应的.xib文件,分别取名successLogin,listViewController。我们再去关注一下LoginViewController.xib文件,拖一个navigation Controller图标到nib主窗口中,那么则新弹出一个视图窗口,按住Ctrl将File‘s Owner拖向Navigation Controller,在选中nib主窗口的第二项


点选图中蓝色区域,再看他的控制器,分别对其修改为





目的是将导航器的方向定位success类和其视图。

我们再对success.xib进行设置,只加入一个按钮,并做相应的关联,对他的.h文件进行编码

C代码


@interface successLogin : UIViewController {
}
-(IBAction)showPressed;
@end

对.m文件进行编码

C代码


#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连接



对.h文件进行编码

C代码


@interface ListViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>{
IBOutlet UITableView *myTableView;
NSArray *friendList;
}
@property (nonatomic,retain)UITableView *myTableView;
@property (nonatomic,retain)NSArray *friendList;
@end

对.m文件进行编码

C代码


#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

完成了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: