App竞品技术分析 (5)数据采集工具

1 页面跳转器

  页面跳转器是页面打点的前提。

  对于Android而言,有Intent来帮助我们进行页面跳转和传值。但是你会发现,想从A页面跳转到B页面,在A页面要声明B页面的实例,这是一个强引用,如下所示:

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);

  对于iOS而言,就连Intent这样的机制都没有了。我们不但要在A页面声明B页面实例,还要通过为B设置属性的方式,进行页面间传值。如下所示:

- (void) jumpTo {
    APageViewController* b = [[APageViewController alloc] init];
    b.version = "5.1";
    [self.navigationController pushViewController: b animated: YES];
    [b release];
}

  我们一直在强调解耦,但是在iOS和Android的页面传值上却不遵守这个原则。于是很多公司开始致力于解决这个问题。写一个Navigator类,通过使用反射技术可以接触页面间的耦合性,这样我们就可以把所有的页面都定义在一个XML配置文件中,每个节点包括该页面的key、对应的类名称、打开方式。

  我们先解决iOS的页面传参。使用一个字典作为页面间参数传递的载体,为此,在ViewController的基类中定义一个字典参数,这样在Navigator反射的时候,将传递进来的参数设置给页面实例即可,如下所示,分别是Navigator的h和m文件:

#import <Foundation/Foundation.h>

@interface Navigator : NSObject {

}

+ (Navigator *)sharedInstance;

+ (void)navigateTo:(NSString *)viewController;
+ (void)navigateTo:(NSString *)viewController withData:(NSDictionary *)param;

@end
#import "Navigator.h"
#import "BaseViewController.h"
#import "SynthesizeSingleton.h"

@implementation Navigator

SYNTHESIZE_SINGLETON_FOR_CLASS(Navigator);

+ (void)navigateTo:(NSString *)viewController {
    [self navigateTo:viewController withData:nil];
}

+ (void)navigateTo:(NSString *)viewController withData:(NSDictionary *)param {
    BaseViewController * classObject = (BaseViewController *)
    [[NSClassFromString(viewController) alloc] init];
    classObject.param = param;
    [classObject.navigationController
    pushViewController:classObject animated:YES];
    [classObject release];
}

@end

  为了解决页面间传参的问题,我们需要在BaseViewController中增加一个params属性,这是一个字典,在跳转前把要传递的属性塞进去,在跳转后把字典中的值再取出来:

@interface BaseViewController : UIViewController {

NSDictionary* _param;

}

@property (nonatomic, retain) NSDictionary* param;

  那么在使用时就非常简单了,如下所示

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值