前段时间心情很郁闷,所以很久都没有更新博客了。今天突然想写点东西,就写点把。IOS6的发布 facebook决定放弃使用Three2o这套框架,Three20的官网已经很久没有更新了,目前只有GitHub上在维护,所以决定将现在工程中的Three2o框架移除。本篇文章MOMO就写写我在移除Three20框架的一些经验以及教训。
一.界面切换
在AppDelegate中打开程序的入口窗口,用过Three20的朋友看到下面的方式应该不陌生吧。
if (![navigator restoreViewControllers])
{
//打开上面设置的url
[navigator openURLAction:[TTURLAction actionWithURLPath:@"tt://Tab"]];
}
我们首先把它先改了,如果你的程序不需要导航栏可以不加UINavigationController。
UINavigationController *navController = [[[UINavigationController alloc]initWithRootViewController:[[[LoginViewController alloc]init] autorelease]]autorelease];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
此时,入口视图方法我们已经修改完毕,接着我们修改二级菜单,子菜单之间的切换。
处理子菜单时Three20依然在AppDelegate中就声明了所有ViewController,并且依次给他们起了一个别名。
//常用的三种视图类型
[map from:@"tt://Schedule" toModalViewController:[ScheduleViewController class]];
[map from:@"tt://Schedule" toModalViewController:[ScheduleViewController class]];
[map from:@"tt://Schedule" toSharedViewController:[ScheduleViewController class]];
这三种是最常用的类型,其实核心也无非就是两种,这里我直接写IOS源生的实现方式,记得把TTViewController换成UIViewController:
1.向右推出一个新ViewController
[self.navigationController pushViewController:[[[SignUpViewController alloc]init]autorelease] animated:YES];
默认的话进入新ViewController的时候左上角会有一个返回的按钮,点击该按钮后会返回到之前的界面。当然如果你是自定义返回按钮的话,需要写代码来手动返回。
在新推的界面中执行下面代码即可直接返回。
[self.navigationController popViewControllerAnimated:YES];
2.向上覆盖一个新ViewController
同样,如果不需要导航栏的话去掉UINavigationController即可。
UINavigationController *viewNavController = [[UINavigationController alloc] initWithRootViewController:[[[newViewController alloc]init]autorelease]];
[self presentViewController:viewNavController animated:YES completion:^{/* done */}];
正如上面所说这是一个向上覆盖的ViewController ,不需要时就需关闭它。
[self dismissViewControllerAnimated:YES completion:^{/* done */}];
二、自定义TabBar
Three20研究院之完全自定义TabBar(八) 这是320实现自定义的方式,有兴趣的朋友去看看,这里我只说如何把Three20自定义的TabBar 用源生的方式来写。
.h文件
#import <Foundation/Foundation.h>
#import "define.h"
//根据你的情况编写头文件
#import "ChannelViewController.h"
#import "InvitationsViewController.h"
#import <UIKit/UIKit.h>
#import "Utility.h"
@interface UserCenterViewController : UITabBarController<UITabBarControllerDelegate>
{
}
.m文件
#import "UserCenterViewController.h"
@implementation UserCenterViewController
-(id)init
{
if (self = [super init])
{
//添加底层背景色
[self.view setFrame:CGRectMake(0, 0, 320, 460)];
UIView *tabBarView=[[[UIView alloc] initWithFrame:CGRectMake(0, 480-48, 320, 48)] autorelease];
[self.view addSubview:tabBarView];
UIImageView *backGroundView=[[[UIImageView alloc] initWithImage:[Utility getImageByName:@"tabBar_background":@"png"]] autorelease];
[tabBarView addSubview:backGroundView];
//添加tabbar
UIButton* button0 = [UIButton buttonWithType:UIButtonTypeCustom];
button0.frame = CGRectMake(0, 0, 160, 48);
[button0 setTitle:@"婚礼" forState:UIControlStateNormal];
[button0 setBackgroundImage:[Utility getImageByName:@"tk_03-l":@"png"] forState:UIControlStateNormal];
button0.tag = 0;
[button0 addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[tabBarView addSubview:button0];
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setTitle:@"邀请" forState:UIControlStateNormal];
[button1 setBackgroundImage:[Utility getImageByName:@"tk_03-l":@"png"] forState:UIControlStateNormal];
button1.frame = CGRectMake(160, 0, 160, 48);
button1.tag = 1;
[button1 addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[tabBarView addSubview:button1];
ChannelViewController *channel = [[[ChannelViewController alloc]init]autorelease];
InvitationsViewController *invitation = [[[InvitationsViewController alloc]init]autorelease];
UINavigationController *navigationController= [[[UINavigationController alloc] initWithRootViewController:invitation]autorelease];
[navigationController viewDidAppear:YES];
[navigationController viewWillAppear:YES];
//把所有的viewcontroller添加至tabbar中
self.viewControllers = [[[NSMutableArray alloc]initWithObjects:channel,navigationController, nil]autorelease];
}
return self;
}
-(void) dealloc
{
[super dealloc];
DLog(@"dealloc");
}
-(void)ButtonPressed:(id)buttonID
{
//点击不同的按钮进入不同的ViewController
UIButton *button = (UIButton *)buttonID;
self.selectedIndex =button.tag;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
<strong>所谓自定义Tabbar原理就是自定义按钮,点击按钮后切换tabbar指向的viewcontroller即可。</strong>
接着再说说Tabbar界面切换的特殊情况,正常情况切换界面是在当前ViewController中完成的,可是UITabViewController都是集成了好几个子ViewController,如果切换界面的触发事件在这些子ViewController中的话,就需要在子ViewController中通知父UITabViewController然后在去切换。 不然的话TabBar的按钮是不会消失的。
[self.tabBarController.navigationController pushViewController:[[[ECardViewController alloc]init] autorelease] animated:YES];
三、自定义UITableViewCell
Three20研究院之自定义TableView列表详解(二)这是之前我写过的一篇文章,但是在Three20中我是按这种方式实现自定义UITableViewCell,这里我只写如何在IOS源生下实现。
在UITableViewController的全局定义一个自定义Cell的数据源,以数组的形式保存着每一个cell的数据。
NSMutableArray *_dataSourses;
下面这段代码我上面提到的那篇文章中copy的。
#import <Three20/Three20.h>
@interface TableItem : TTTableLinkedItem {
//列表元素的文字
NSString *_title;
//列表元素的贴图
UIImage *_image;
//列表元素的背景颜色
UIColor *_backcolor;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) UIImage *image;
@property (nonatomic, copy) UIColor *backcolor;
//初始化赋值
+ (id)itemWithTitle:(NSString *)title
image:(UIImage *)image backcolor:(UIColor *) backcolor;
@end
#import "TableItem.h"
@implementation TableItem
@synthesize title = _title,image = _image, backcolor = _backcolor;
+ (id)itemWithTitle:(NSString *)title
image:(UIImage *)image backcolor:(UIColor *) backcolor {
//初始化
TableItem *item = [[[self alloc] init] autorelease];
item.title = title;
item.image = image;
item.backcolor = backcolor;
return item;
}
- (id)init
{
if (self = [super init])
{
_title = nil;
_image = nil;
_backcolor = nil;
}
return self;
}
- (void)dealloc
{
[super dealloc];
TT_RELEASE_SAFELY(_title);
TT_RELEASE_SAFELY(_image);
TT_RELEASE_SAFELY(_backcolor);
}
@end
TableItem.h就是每一个Cell需要用到的对象,这里我们先把
@interface TableItem:TTTableLinkedItem修改成
@interface TableItem:NSObject
接着是Cell
#import <Three20/Three20.h>
@interface TableItemCell : TTTableLinkedItemCell {
//元素的名称
UILabel *_titleLabel;
//元素的贴图
UIImageView *_imageview;
}
@end
#import "TableItemCell.h"
#import "TableItem.h"
@implementation TableItemCell
+ (CGFloat)tableView:(UITableView*)tableView rowHeightForObject:(id)item
{
//每个列表元素的高度
return 80.0;
}
- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString*)identifier
{
//初始化列表
if (self = [super initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:identifier])
{
_item = nil;
_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
//将文本框加入窗口
[self.contentView addSubview:_titleLabel];
_imageview = [[UIImageView alloc] initWithFrame:CGRectZero];
//将图片视图加入窗口
[self.contentView addSubview:_imageview];
}
return self;
}
- (void)dealloc
{
TT_RELEASE_SAFELY(_titleLabel);
TT_RELEASE_SAFELY(_imageview);
[super dealloc];
}
- (void)layoutSubviews {
[super layoutSubviews];
//设置列表中的组件,并且组件的绘制区域
[_imageview setFrame:CGRectMake(5,5,70,70)];
[_titleLabel setFrame:CGRectMake(100,0,100,20)];
}
- (id)object
{
return _item;
}
- (void)setObject:(id)object {
if (_item != object) {
[super setObject:object];
//拿到列表中的数据
TableItem *item = object;
//绘制在屏幕中
[_titleLabel setText:item.title];
[_titleLabel setBackgroundColor:item.backcolor];
[_imageview setImage:item.image];
[_imageview setBackgroundColor:item.backcolor];
//设置列表的背景颜色
self.contentView.backgroundColor = item.backcolor;
//设置列表的选中颜色
self.selectionStyle=UITableViewCellSelectionStyleBlue;
}
}
@end
TableItemCell.h就是每一个Cell对象,自定义的布局就是在这里完成。
@interface TableItemCell:TTTableLinkedItemCell
修改成
@interface TableItemCell:UITableViewCell
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//绘制每一个Cell的高度,我们的项目需要用到自定义高度,大家可根据自己的情况来设置cell的高度
//_dataSourses就是我们需要维持的全局cell数据源数组,
//这么写的意思就是拿到每一个数据源数组中的高度数据。
TimeLineTableItem *item = [_dataSourses objectAtIndex:indexPath.row];
return item.itemHeight;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//cell的数量
return [_dataSourses count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//绘制cell
static NSString *CellTableIdentifier = @"TimeLineTabeIndentifier";
//查看cell是否复用
TimeLineTableItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
if (cell == nil)
{
//cell没有复用时候创建
cell = [[[TimeLineTableItemCell alloc]initWithStyle:UITableViewCellStyleDefault :CellTableIdentifier ]autorelease];
//在这里将数据源传递进cell中通知cell绘制
[cell drawCell:[_dataSourses objectAtIndex:indexPath.row]];
}else
{
//cell复用了,这里就不用创建新的cell
//直接将数据源传递进cell中通知cell绘制
[cell drawCell:[_dataSourses objectAtIndex:indexPath.row]];
}
return cell;
}
注:TimeLineTableItem、TimeLineTableItemCell 是我项目中用到的对象,大家不必拘泥于名子如何来起,原理白即可。
另外cell还具有一个刷新的功能,当数据源_dataSourses发生改变后手动调用如下方法即可刷新Cell。程序会重新执行上述三个方法来刷新Cell。
[self.tableView reloadData];
一般在Three20工程中将这三部做完剩下的就是一些琐碎的活,在将工程中所有TT开头的对象引用全部去掉,谁都不愿理干体力活,可是脏活累活还是得干,呵呵! 仔细一下很快就把Three20框架移除了。
如下图所示,这里的thrr20路径一定要删除,不然还是会有诡异的错误,呵呵、
最后在fromwork中将所有three20的framwork全部删除,运行一下祝你成功喔,嘿嘿!
- 本文固定链接: https://www.xuanyusong.com/archives/1833
- 转载请注明: 雨松MOMO于 雨松MOMO程序研究院 发表



好吧。。
大清早的,跑来支持下,加油!加油!
呵呵 小马