iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (4) 完成
来源:互联网 发布:伊藤园三得利 知乎 编辑:程序博客网 时间:2024/05/24 02:52
继续《iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (3) 深入Segue,Class,Protocol,Delegate的基本使用》的内容,如果想从头学习Storyboard,请参考《iOS 5 Storyboard 学习之 UITableViews》
1 我们来建立最后一个TableViewController,用来控制类别的选择。托一个“Table View Controller”,从类别 Control+Drog 到新的“Navigation Controller”上然后选择“Push”,Segue的identifier为“PickGame”
设置“Table View Controller”的cell 的 Style为Basic
2 建立“ GamePickerViewController”,Class 是 Table View Controller
然后编辑 “GamePickerViewController.h”
#import <UIKit/UIKit.h>
@interface GamePickerViewController : UITableViewController {
NSArray *games;
}
@end
“GamePickerViewController.m”
-(void)viewDidLoad
{
[super viewDidLoad];
games =[NSArray arrayWithObjects:@"游泳",@"篮球",@"足球",@"象棋",@"国际象棋",@"Dota",nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
games =nil;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return[games count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell =[tableViewdequeueReusableCellWithIdentifier:@"GameCell"];
cell.textLabel.text =[games objectAtIndex:indexPath.row];
return cell;
}
这里别忘了给Cell设置Identifier为“GameCell”
3 一切配置都完成了,我们开始写Delegate类似我们在Player View那里做的一样。
3.1 首先在“ GamePickerViewController.m”中修改为:
@class GamePickerViewController;
@protocol GamePickerViewControllerDelegate <NSObject>
-(void)gamePickerViewController:(GamePickerViewController *)controller didSelectGame:(NSString *)game;
@end
@interface GamePickerViewController : UITableViewController
@property (nonatomic,weak) id <GamePickerViewControllerDelegate> delegate;
@property (nonatomic,strong) NSString *game;
@end
3.2 然后在“ GamePickerViewController.m”中添加和修改
@implementation GamePickerViewController
{
NSArray*games;
NSUInteger selectedIndex;
}
@synthesize delegate,game;
-(void)viewDidLoad
{
[super viewDidLoad];
games =[NSArray arrayWithObjects:@"游泳",@"篮球",@"足球",@"象棋",@"国际象棋",@"Dota",nil];
selectedIndex =[games indexOfObject:self.game];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell =[tableViewdequeueReusableCellWithIdentifier:@"GameCell"];
cell.textLabel.text =[games objectAtIndex:indexPath.row];
if(indexPath.row == selectedIndex)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
#pragma mark - Table view delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(selectedIndex != NSNotFound){
UITableViewCell *cell =[tableView cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:selectedIndex inSection:0]];
cell.accessoryType = UITableViewCellAccessoryNone;
}
selectedIndex = indexPath.row;
UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *theGame =[games objectAtIndex:indexPath.row];
[self.delegate gamePickerViewController:self didSelectGame:theGame];
}
3.3 回顾一下
3.3.1 可以看出Delegate中唯一的一个方法就是传递选择的类型
3.3.2 选择的类型,会在self.game中,那么它会在ViewDidLoad的时候加载
3.3.3 我们利用“ selectedIndex”会知道我们选择的类型是在self.game中的哪一个
3.3.4 在“cell.textLabel.text =[games objectAtIndex:indexPath.row];”这里我们给Text赋值
3.3.5 然后我们到了最后一步,当我们选择一个cell之后我们回退到上一个view并且把值传回去
4但是目前它是上边这样的,不能回退,所以最后一步我们就让它把值传回去
4.1 回到“PlayerDetailsViewController.h”改为
#import "GamePickerViewController.h"
@interface PlayerDetailsViewController : UITableViewController<GamePickerViewControllerDelegate>
4.2 在 PlayerDetailsViewController.h”中加入Segue部分内容,它是负责把值传递回去的
@implementation PlayerDetailsViewController
{
NSString*game;
}
4.3 在 PlayerDetailsViewController.h”中修改
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"PickGame"])
{
GamePickerViewController *gamePickerViewController =
segue.destinationViewController;
gamePickerViewController.delegate = self;
gamePickerViewController.game = game;
}
}
-(id)initWithCoder:(NSCoder*)aDecoder
{
if((self =[super initWithCoder:aDecoder]))
{
NSLog(@"init PlayerDetailsViewController");
game =@"国际象棋";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.detailLabel.text = game;
}
#pragma mark - GamePickerViewControllerDelegate
- (void)gamePickerViewController:(GamePickerViewController *)controller didSelectGame:(NSString *)theGame
{
game = theGame;
self.detailLabel.text = game;
[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)done:(id)sender
{
Player *player =[[Player alloc] init];
player.name = self.nameTextField.text;
player.game = game;
player.rating =1;
[self.delegate playerDetailsViewController:self didAddPlayer:player];
}
看下最后的结果
完整的代码在这里获取 https://github.com/xxd/Storyboard-Multi-Nav
--EOF--
- iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (4) 完成
- iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (1)
- iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (3)
- iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (2) 代码部分
- iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (3) 深入Segue,Class,Protocol,Delegate的基本使
- iOS storyboard创建Navigation Controller
- IOS学习之——快速创建当前视图的Navigation,TabBar Controller
- storyboard 添加Navigation Controller
- iOS学习- 05 Navigation Controller
- Tutorial: Storyboard in XCode 4.2 with Navigation Controller and Tabbar Controller (Part 1)
- Tutorial: Storyboard in XCode 4.2 with Navigation Controller and Tabbar Controller (Part 2)(中文注解)
- IOS学习——Navigation Controller
- Navigation Controller 学习
- navigation controller学习总结
- Storyboard快速创建带有Navigation的Controller
- IOS 学习九 Navigation Controller 的使用示例
- iOS开发之导航栏Navigation Controller的使用
- 点击button实现Storyboard中TabBar Controller的tab切换
- Lua中的require
- 用LED矩阵屏 显示网络摄像头图像
- mysql查询结果添加序列号
- Android代码规范
- 题目忘了..
- iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (4) 完成
- C++类模板详解
- 20141211,微软12月10日发布7个安全补丁
- Valid signing identity not found解决办法(原有IDP私钥丢失)及Certificate、App ID、Devices、Provisioning Profiles之间区别
- 题目忘了
- mysql千万级测试1亿数据的分页分析测试
- 在html中ajax请求其他电脑上项目的数据
- 你不知道的数据库9大安全问题 !
- wait时间到或被中断唤醒时,仍然需要等待获取锁。