iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (3) 深入Segue,Class,Protocol,Delegate的基本使用
继续《iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (2)》的内容,如果想从头学习Storyboard,请参考《iOS 5 Storyboard 学习之 UITableViews》
1 首先我们添加一个“添加按钮”
2 随后托一个“Table View Controller”,然后配置它
然后embed in Navigation Controller
从左上角的加号按钮 Control+Drog 到新的“Navigation Controller”上然后选择Modal
把这个Segue的Identifier设置成“AddPlayer”
之后的样子是这样的
3 建立新的“PlayerDetailsViewController”subclass是”UITableViewController“
然后绑定到”Table View Controller“上去
增加两个”Bar Button Items“
然后链接Done 和 Cancel 的 outlet
4 现在用Delegate写按钮
修改“PlayerDetailsViewController.h”
#import <UIKit/UIKit.h>
@class PlayerDetailsViewController;
@protocol PlayerDetailsViewControllerDelegate <NSObject>
- (void)playerDetailsViewControllerDidCancel:(PlayerDetailsViewController *)controller;
- (void)playerDetailsViewControllerDidDone:(PlayerDetailsViewController *)controller;
@end
@interface PlayerDetailsViewController : UITableViewController
@property (nonatomic,weak) id <PlayerDetailsViewControllerDelegate> delegate;
- (IBAction)cancel:(id)sender;
- (IBAction)done:(id)sender;
@end
看看上边都干了些什么
1 Class
2 建立Procotol PlayerDetailsViewControllerDelegate
3 创建delegate
在“PlayerDetailsViewController.m”中写按钮事件
@synthesize delegate;
-(IBAction)cancel:(id)sender
{
[self.delegate playerDetailsViewControllerDidCancel:self];
}
-(IBAction)done:(id)sender
{
[self.delegate playerDetailsViewControllerDidDone:self];
}
然后在“PlayerViewController.h”添加
#import "PlayerDetailsViewController.h"
@interface PlayersViewController : UITableViewController<PlayerDetailsViewControllerDelegate>
在“PlayerViewController.m”添加
#pragma mark - PlayerDetailsViewControllerDelegate
- (void)playerDetailsViewControllerDidCancel:(PlayerDetailsViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)playerDetailsViewControllerDidDone:(PlayerDetailsViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
注意这里的“ [self dismissViewControllerAnimated:YES completion:nil];”是iOS 5里用来代替“ dismissModalViewControllerAnimated”的。
在“PlayerViewController.m”写AddPlayer Segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"AddPlayer"])
{
UINavigationController *navigationController = segue.destinationViewController;
PlayerDetailsViewController *playerDetailsViewController =
[[navigationController viewControllers] objectAtIndex:0];
playerDetailsViewController.delegate = self;
}
}
如果不加入上边这段Segue代码,那么AddPlayer页是可以打开的,但是无法通过点击按钮关闭的。
5 点击“MainStoryboard.storyboard”编辑“Add Player”页
向cell里边拖一个“Text Field”然后去掉边框,把“Text Field”拖到cell一样大小,下面的“类别”做一样的动作。
拖动用户名的“Text Field”到“PlayerDetailViewController.h”,然后在name栏内填写“nameTextField”,选择Connect,之后xcode会建立一条语句,也同时会在“PlayerDetailViewController.m”中建立synthesized和viewDidUnload,非常方便。
@property (strong, nonatomic) IBOutlet UITextField *nameTextField;
@property (strong, nonatomic) IBOutlet UILabel *detailLabel;
由于table view controller 使用了static cells所以就不需要数据源(data source) ,那么我们可以把“PlayerDetailViewController.h”内从“#pragma mark - Table view data source”到 “#pragma mark - Table view delegate”的代码都删除掉。
6 然后在“PlayerDetailViewController.h”加入
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
if(indexPath.section ==0)
[self.nameTextField becomeFirstResponder];
}
以上代码很简单就是当我们进入”添加用户“页面,点击第一个文字输入框启动” nameTextField“然后弹出键盘。
7 现在我们要修改这个”Done“按钮的功能,刚才我们把它定义为和cacel,现在我们真正的赋予它功能,以下一共三步:
首先在PlayerDetailViewController.h”中修改
@class PlayerDetailsViewController;
@class Player;
@protocol PlayerDetailsViewControllerDelegate <NSObject>
- (void)playerDetailsViewControllerDidCancel:(PlayerDetailsViewController *)controller;
- (void)playerDetailsViewController:(PlayerDetailsViewController *)controller didAddPlayer:(Player*)player;
@end
然后在PlayerDetailViewController.m”中修改按钮事件
#import "Player.h"
-(IBAction)done:(id)sender
{
Player *player =[[Player alloc] init];
player.name = self.nameTextField.text;
player.game =@"Chess";
player.rating =1;
[self.delegate playerDetailsViewController:self didAddPlayer:player];
}
最后我们在“ PlayerViewController.m”中把Done按钮部分替换掉
#pragma mark - PlayerDetailsViewControllerDelegate
- (void)playerDetailsViewControllerDidCancel:(PlayerDetailsViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)playerDetailsViewController:(PlayerDetailsViewController *)controller
didAddPlayer:(Player *)player
{
[self.players addObject:player];
NSIndexPath*indexPath =[NSIndexPath indexPathForRow:[self.players count]-1 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self dismissViewControllerAnimated:YES completion:nil];
}
目前我们完成了,最后我们加入类型的选择就基本上完成了
--EOF--