iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (4) 完成

长平狐 发布于 2012/08/22 16:34
阅读 808
收藏 0

继续《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”


屏幕快照 2012-03-12 下午4.12.47.png

设置“Table View Controller”的cell 的 Style为Basic

屏幕快照 2012-03-12 下午3.30.36.png

2 建立“ GamePickerViewController”,Class 是 Table View Controller

屏幕快照 2012-03-07 下午10.43.37.png

然后编辑 “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 =[tableView dequeueReusableCellWithIdentifier:@"GameCell"];

cell.textLabel.text =[games objectAtIndex:indexPath.row];

return cell;

}

这里别忘了给Cell设置Identifier为“GameCell”


屏幕快照 2012-03-12 下午3.38.01.png

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 =[tableView dequeueReusableCellWithIdentifier:@"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并且把值传回去


屏幕快照 2012-03-12 下午4.13.49.png

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

}

看下最后的结果


屏幕快照 2012-03-12 下午4.52.42.png屏幕快照 2012-03-12 下午4.52.47.png

完整的代码在这里获取 https://github.com/xxd/Storyboard-Multi-Nav

--EOF--


原文链接:http://www.cnblogs.com/buro79xxd/archive/2012/03/13/2393910.html
加载中
返回顶部
顶部