toSharedViewController不会重用现有的控制器

时间:2012-02-12 03:31:30

标签: iphone objective-c ios xcode three20

我的网址映射如下:

[map from:@"tt://webPage/(initWithPage:)" toSharedViewController:[WebPageController class]];

WebPageController

- (id) initWithPage:(WebPage)page
{
    if (self = [super init])
    {
    ...

然后我在代码中多次调用了网址

tt://webPage/1
tt://webPage/2
tt://webPage/1 (still called the initWithPage: everytime, not cached)

为什么它没有被缓存,因为它是一个SharedViewController?

2 个答案:

答案 0 :(得分:4)

我相信会发生这种情况,因为在iOS 5上TTNaviagtor已被破坏。请参阅https://github.com/facebook/three20/pull/719/files。您是否尝试在iOS 4上运行相同的代码并获得相同的结果?

我建议您停止使用TTNaviagtor。您仍然可以通过在本机ios方法中推送和弹出TTViewController来使用three20库。

以下是替换应用代理中TTNaviagtor的示例:

@interface AppDelegate : NSObject <UIApplicationDelegate> {

 UIWindow* _window;
 TTBaseNavigationController* _masterNavController;
 WebPageController* _web1Controller;
 WebPageController* _web2Controller;
}

@property(nonatomic, retain) UIWindow* window;
@property(nonatomic, retain) TTBaseNavigationController* masterNavController;
@property(nonatomic, retain) WebPageController* web1Controller;
@property(nonatomic, retain) WebPageController* web2Controller;

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation AppDelegate

@synthesize window = _window;

@synthesize masterNavController = _masterNavController;
@synthesize web1Controller = _web1Controller;
@synthesize web2Controller = web2Controller;

///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  _window = [[UIWindow alloc] initWithFrame:TTScreenBounds()];


    TTViewController* controller = [[[MasterViewController alloc] init] autorelease];
    _masterNavController = [[TTBaseNavigationController alloc] initWithRootViewController:controller];
    [_window addSubview:_masterNavController.view];    
  }

  [_window makeKeyAndVisible];

  return YES;
}

然后您可以将TTViewController(或您自己的TTViewController的子类)推送到_masterNavController。就个人而言,我认为TTNavigator是一个糟糕的设计模式,苹果以不同的心态设计了他们的导航系统。

答案 1 :(得分:0)

为什么不进入代码并检查发生了什么?

我相信这些对象是在TTURLMap的objectForURL:query:pattern:中创建的,你可以设置一个断点,看看为什么要创建一个新点而不是重新使用旧点。

这是objectForURL:query:pattern:与我的评论

的实施
- (id)objectForURL: (NSString*)URL
             query: (NSDictionary*)query
           pattern: (TTURLNavigatorPattern**)outPattern {
  id object = nil;
  if (_objectMappings) {
    // _objectMappings is a NSMutableDictionary and use to cache shared object
    object = [_objectMappings objectForKey:URL]; 
    // if object not found than check does _objectMappings contains it with right key
    if (object && !outPattern) {
      return object;
    }
  }

  NSURL* theURL = [NSURL URLWithString:URL];
  TTURLNavigatorPattern* pattern  = [self matchObjectPattern:theURL];
  if (pattern) {
    if (!object) {
      // object is not found in the mapping dictionary so create new one, this should only happen once for shared object
      object = [pattern createObjectFromURL:theURL query:query]; 
    }
    // make sure navigationMode is TTNavigationModeShare
    if (pattern.navigationMode == TTNavigationModeShare && object) {
       // cache shared object in the mapping dictionary so next time it will re-use the cached one
      [self setObject:object forURL:URL];
      // if setObject:forURL: is not working than the shared object will not be cached
    }
    if (outPattern) {
      *outPattern = pattern;
    }
    return object;

  } else {
    return nil;
  }
}
相关问题