`
yidongkaifa
  • 浏览: 4064440 次
文章分类
社区版块
存档分类
最新评论

Three20研究院之快速将Three20框架从IOS工程中移除(十)

 
阅读更多

IOS6的发布facebook决定放弃使用Three2o这套框架,Three20的官网已经很久没有更新了,目前只有GitHub上在维护,所以决定将现在工程中的Three2o框架移除。本篇文章MOMO就写写我在移除Three20框架的一些经验以及教训。

一.界面切换

在AppDelegate中打开程序的入口窗口,用过Three20的朋友看到下面的方式应该不陌生吧。

1 if(![navigator restoreViewControllers])
2 {
3 //打开上面设置的url
4 [navigator openURLAction:[TTURLAction actionWithURLPath:@"tt://Tab"]];
5
6 }

我们首先把它先改了,如果你的程序不需要导航栏可以不加UINavigationController。

1 UINavigationController *navController = [[[UINavigationController alloc]initWithRootViewController:[[[LoginViewController alloc]init] autorelease]]autorelease];
2
3 self.window.rootViewController = navController;
4
5 [self.window makeKeyAndVisible];

此时,入口视图方法我们已经修改完毕,接着我们修改二级菜单,子菜单之间的切换。

处理子菜单时Three20依然在AppDelegate中就声明了所有ViewController,并且依次给他们起了一个别名。

1 //常用的三种视图类型
2 [map from:@"tt://Schedule"toModalViewController:[ScheduleViewControllerclass]];
3 [map from:@"tt://Schedule"toModalViewController:[ScheduleViewControllerclass]];
4 [map from:@"tt://Schedule"toSharedViewController:[ScheduleViewControllerclass]];

这三种是最常用的类型,其实核心也无非就是两种,这里我直接写IOS源生的实现方式,记得把TTViewController换成UIViewController:

1.向右推出一个新ViewController

1 [self.navigationController pushViewController:[[[SignUpViewController alloc]init]autorelease] animated:YES];

默认的话进入新ViewController的时候左上角会有一个返回的按钮,点击该按钮后会返回到之前的界面。当然如果你是自定义返回按钮的话,需要写代码来手动返回。

在新推的界面中执行下面代码即可直接返回。

1 [self.navigationController popViewControllerAnimated:YES];

2.向上覆盖一个新ViewController

同样,如果不需要导航栏的话去掉UINavigationController即可。

1 UINavigationController *viewNavController = [[UINavigationController alloc] initWithRootViewController:[[[newViewController alloc]init]autorelease]];
2 [self presentViewController:viewNavController animated:YES completion:^{/* done */}];

正如上面所说这是一个向上覆盖的ViewController ,不需要时就需关闭它。

1 [self dismissViewControllerAnimated:YES completion:^{/* done */}];

二、自定义TabBar

Three20研究院之完全自定义TabBar(八)这是320实现自定义的方式,有兴趣的朋友去看看,这里我只说如何把Three20自定义的TabBar 用源生的方式来写。

.h文件

01 #import <Foundation/Foundation.h>
02 #import "define.h"
03 //根据你的情况编写头文件
04 #import "ChannelViewController.h"
05 #import "InvitationsViewController.h"
06 #import <UIKit/UIKit.h>
07 #import "Utility.h"
08 @interface UserCenterViewController : UITabBarController<UITabBarControllerDelegate>
09 {
10 }

.m文件

01 #import "UserCenterViewController.h"
02
03 @implementation UserCenterViewController
04
05 -(id)init
06 {
07 if(self = [super init])
08 {
09 //添加底层背景色
10
11 [self.view setFrame:CGRectMake(0, 0, 320, 460)];
12 UIView *tabBarView=[[[UIView alloc] initWithFrame:CGRectMake(0, 480-48, 320, 48)] autorelease];
13
14 [self.view addSubview:tabBarView];
15
16 UIImageView *backGroundView=[[[UIImageView alloc] initWithImage:[Utility getImageByName:@"tabBar_background":@"png"]] autorelease];
17 [tabBarView addSubview:backGroundView];
18
19 //添加tabbar
20 UIButton* button0 = [UIButton buttonWithType:UIButtonTypeCustom];
21 button0.frame = CGRectMake(0, 0, 160, 48);
22 [button0 setTitle:@"婚礼"forState:UIControlStateNormal];
23 [button0 setBackgroundImage:[Utility getImageByName:@"tk_03-l":@"png"] forState:UIControlStateNormal];
24 button0.tag = 0;
25 [button0 addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
26
27 [tabBarView addSubview:button0];
28
29 UIButton* button1 = [UIButton buttonWithType:UIButtonTypeCustom];
30 [button1 setTitle:@"邀请"forState:UIControlStateNormal];
31 [button1 setBackgroundImage:[Utility getImageByName:@"tk_03-l":@"png"] forState:UIControlStateNormal];
32 button1.frame = CGRectMake(160, 0, 160, 48);
33 button1.tag = 1;
34 [button1 addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
35 [tabBarView addSubview:button1];
36
37 ChannelViewController *channel = [[[ChannelViewController alloc]init]autorelease];
38 InvitationsViewController *invitation = [[[InvitationsViewController alloc]init]autorelease];
39
40 UINavigationController *navigationController= [[[UINavigationController alloc] initWithRootViewController:invitation]autorelease];
41
42 [navigationController viewDidAppear:YES];
43 [navigationController viewWillAppear:YES];
44
45 //把所有的viewcontroller添加至tabbar中
46 self.viewControllers = [[[NSMutableArray alloc]initWithObjects:channel,navigationController, nil]autorelease];
47
48 }
49
50 returnself;
51 }
52
53 -(void) dealloc
54 {
55 [super dealloc];
56 DLog(@"dealloc");
57 }
58
59 -(void)ButtonPressed:(id)buttonID
60 {
61 //点击不同的按钮进入不同的ViewController
62 UIButton *button = (UIButton *)buttonID;
63 self.selectedIndex =button.tag;
64 }
65
66 -(void)viewWillAppear:(BOOL)animated
67 {
68 [super viewWillAppear:animated];
69 [self.navigationController setNavigationBarHidden:YES animated:NO];
70 }
71
72 <strong>所谓自定义Tabbar原理就是自定义按钮,点击按钮后切换tabbar指向的viewcontroller即可。</strong>

接着再说说Tabbar界面切换的特殊情况,正常情况切换界面是在当前ViewController中完成的,可是UITabViewController都是集成了好几个子ViewController,如果切换界面的触发事件在这些子ViewController中的话,就需要在子ViewController中通知父UITabViewController然后在去切换。 不然的话TabBar的按钮是不会消失的。

1 [self.tabBarController.navigationController pushViewController:[[[ECardViewController alloc]init] autorelease] animated:YES];

、自定义UITableViewCell

Three20研究院之自定义TableView列表详解(二)这是之前我写过的一篇文章,但是在Three20中我是按这种方式实现自定义UITableViewCell,这里我只写如何在IOS源生下实现。

在UITableViewController的全局定义一个自定义Cell的数据源,以数组的形式保存着每一个cell的数据。

NSMutableArray*_dataSourses;

下面这段代码我上面提到的那篇文章中copy的。

01 #import <Three20/Three20.h>
02
03 @interface TableItem : TTTableLinkedItem {
04 //列表元素的文字
05 NSString *_title;
06 //列表元素的贴图
07 UIImage *_image;
08 //列表元素的背景颜色
09 UIColor *_backcolor;
10 }
11
12 @property (nonatomic, copy) NSString *title;
13 @property (nonatomic, copy) UIImage *image;
14 @property (nonatomic, copy) UIColor *backcolor;
15
16 //初始化赋值
17 + (id)itemWithTitle:(NSString *)title
18 image:(UIImage *)image backcolor:(UIColor *) backcolor;
19
20 @end

01 #import "TableItem.h"
02
03 @implementation TableItem
04
05 @synthesize title = _title,image = _image, backcolor = _backcolor;
06
07 + (id)itemWithTitle:(NSString *)title
08 image:(UIImage *)image backcolor:(UIColor *) backcolor {
09 //初始化
10 TableItem *item = [[[self alloc] init] autorelease];
11 item.title = title;
12 item.image = image;
13 item.backcolor = backcolor;
14 returnitem;
15 }
16
17 - (id)init
18 {
19 if(self = [super init])
20 {
21 _title = nil;
22 _image = nil;
23 _backcolor = nil;
24 }
25
26 returnself;
27 }
28
29 - (void)dealloc
30 {
31 [super dealloc];
32 TT_RELEASE_SAFELY(_title);
33 TT_RELEASE_SAFELY(_image);
34 TT_RELEASE_SAFELY(_backcolor);
35
36 }
37
38 @end

TableItem.h就是每一个Cell需要用到的对象,这里我们先把

@interface TableItem:TTTableLinkedItem修改成

@interface TableItem:NSObject

接着是Cell

01 #import <Three20/Three20.h>
02
03 @interface TableItemCell : TTTableLinkedItemCell {
04 //元素的名称
05 UILabel *_titleLabel;
06 //元素的贴图
07 UIImageView *_imageview;
08
09 }
10
11 @end

01 #import "TableItemCell.h"
02 #import "TableItem.h"
03 @implementation TableItemCell
04
05 + (CGFloat)tableView:(UITableView*)tableView rowHeightForObject:(id)item
06 {
07 //每个列表元素的高度
08 return80.0;
09 }
10
11 - (id)initWithStyle:(UITableViewCellStyle)style
12 reuseIdentifier:(NSString*)identifier
13 {
14 //初始化列表
15 if(self = [super initWithStyle:UITableViewCellStyleValue2
16 reuseIdentifier:identifier])
17 {
18 _item = nil;
19
20 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
21 //将文本框加入窗口
22 [self.contentView addSubview:_titleLabel];
23
24 _imageview = [[UIImageView alloc] initWithFrame:CGRectZero];
25 //将图片视图加入窗口
26 [self.contentView addSubview:_imageview];
27
28 }
29
30 returnself;
31 }
32
33 - (void)dealloc
34 {
35 TT_RELEASE_SAFELY(_titleLabel);
36 TT_RELEASE_SAFELY(_imageview);
37 [super dealloc];
38 }
39
40 - (void)layoutSubviews {
41 [super layoutSubviews];
42
43 //设置列表中的组件,并且组件的绘制区域
44 [_imageview setFrame:CGRectMake(5,5,70,70)];
45
46 [_titleLabel setFrame:CGRectMake(100,0,100,20)];
47
48 }
49
50 - (id)object
51 {
52 return_item;
53 }
54
55 - (void)setObject:(id)object {
56 if(_item != object) {
57 [super setObject:object];
58 //拿到列表中的数据
59 TableItem *item = object;
60 //绘制在屏幕中
61 [_titleLabel setText:item.title];
62 [_titleLabel setBackgroundColor:item.backcolor];
63
64 [_imageview setImage:item.image];
65 [_imageview setBackgroundColor:item.backcolor];
66 //设置列表的背景颜色
67 self.contentView.backgroundColor = item.backcolor;
68 //设置列表的选中颜色
69 self.selectionStyle=UITableViewCellSelectionStyleBlue;
70
71 }
72 }
73 @end

TableItemCell.h就是每一个Cell对象,自定义的布局就是在这里完成。

@interfaceTableItemCell:TTTableLinkedItemCell

修改成

@interfaceTableItemCell:UITableViewCell

此时,自定义Cell的布局我们有了,每一个Cell布局需要的对象我们也有了,下面我们要让UITbaleViewController来将这些Cell绘制出来。
UITbaleViewController中加入代码
1 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3 //绘制每一个Cell的高度,我们的项目需要用到自定义高度,大家可根据自己的情况来设置cell的高度
4 //_dataSourses就是我们需要维持的全局cell数据源数组,
5 //这么写的意思就是拿到每一个数据源数组中的高度数据。
6 TimeLineTableItem *item = [_dataSourses objectAtIndex:indexPath.row];
7
8 returnitem.itemHeight;
9 }

1 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2 {
3 //cell的数量
4 return[_dataSourses count];
5 }

01 -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
02 {
03 //绘制cell
04 staticNSString *CellTableIdentifier = @"TimeLineTabeIndentifier";
05 //查看cell是否复用
06 TimeLineTableItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
07 if(cell == nil)
08 {
09 //cell没有复用时候创建
10 cell = [[[TimeLineTableItemCell alloc]initWithStyle:UITableViewCellStyleDefault :CellTableIdentifier ]autorelease];
11 //在这里将数据源传递进cell中通知cell绘制
12 [cell drawCell:[_dataSourses objectAtIndex:indexPath.row]];
13 }else
14 {
15 //cell复用了,这里就不用创建新的cell
16 //直接将数据源传递进cell中通知cell绘制
17 [cell drawCell:[_dataSourses objectAtIndex:indexPath.row]];
18 }
19
20 returncell;
21
22 }

注:TimeLineTableItem、TimeLineTableItemCell 是我项目中用到的对象,大家不必拘泥于名子如何来起,原理白即可。

另外cell还具有一个刷新的功能,当数据源_dataSourses发生改变后手动调用如下方法即可刷新Cell。程序会重新执行上述三个方法来刷新Cell。

1 [self.tableView reloadData];

一般在Three20工程中将这三部做完剩下的就是一些琐碎的活,在将工程中所有TT开头的对象引用全部去掉,谁都不愿理干体力活,可是脏活累活还是得干,呵呵! 仔细一下很快就把Three20框架移除了。

如下图所示,这里的thrr20路径一定要删除,不然还是会有诡异的错误,呵呵、

最后在fromwork中将所有three20的framwork全部删除,运行一下祝你成功喔,嘿嘿!


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics