您的位置:首页 > 移动开发 > IOS开发

IOS 学习九 Navigation Controller 的使用示例

2015-04-30 16:42 190 查看
1.新建Single View Application项目命名为RecipeBook

2.选中当前的ViewController,点击菜单Editor-Embed in-Navigation Controller, 这样把当前ViewController转为Navigation Controller

3.在第二个ViewController里,放一个TableView,修改ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>

@end


4.选择ViewController.m,定义一个可变数组:

这里直接把后面代码都贴上了:

#import "ViewController.h"

@interface ViewController ()
{
NSArray *recipes;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
recipes=[NSArray arrayWithObjects:@"Egg",@"mushroom",@"Full bread",nil];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [recipes count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIndentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIndentifier];
if(cell==nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIndentifier];

}
cell.textLabel.text=[recipes objectAtIndex:indexPath.row];
return cell;

}
@end


5.选中Table View,右键拖动到ViewController



弹出的菜单里有dataSource delegate,操作两次都选择上。

6.设置Title



7.添加ViewController,放一个label



设置TableView





把TableView和新的ViewController建立关联

在TableView上点右键拖到新的viewcontroller上,选择Show

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