导航控制器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
完成了。
接着我前两篇的登陆继续做,要完成登录成功后显示出导航控制器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
完成了。
相关文章推荐
- IOS UITableView表视图和导航控制器的交互使用
- (转载)IOS编程教程(十):使用StoryBoard来建立导航控制器和表视图
- iOS开发——UI进阶篇(八)pickerView简单使用,通过storyboard加载控制器,注册界面,通过xib创建控制器,控制器的view创建,导航控制器的基本使用
- 修改UITableViewStyleGrouped形式的tableview的背景色 此博文包含图片 (2013-09-10 11:11:57)转载▼ 分类: IOS笔记 在使用UITable
- uitableview+nav----2级视图控制器的内容
- 转载:使用UITableView实现树视图
- iOS UI-表格控制器(UITableView)-基本使用
- (转载)UITableView使用详解
- (转载) UITableView 系列一 :基本使用方法 (显示,删除,添加图片,添加样式等
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
- 使用 SVWebViewController 推出浏览器控制器
- UITableView使用大全-表视图合集-持续更新
- UITableView 使用总结
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- iOS中分段控制器与UIScrollView结合使用
- MVC控制器传递多个Model到视图,使用ViewData, ViewBag, 部分视图, TempData, ViewModel, Tuple
- IOS开发UI篇—导航控制器属性和基本使用
- 转载大神IOS开发系列【8】--UITableView全面解析
- 关于UITableView中的 TableFooterView的使用介绍