ToSharedViewController does not reuse an existing controller

My URL map is as follows:

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

and in WebPageController

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

Then I called the url several times in my code

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

Why is it not cached since it is a SharedViewController?

+8
ios objective-c iphone xcode three20
source share
2 answers

I believe this is happening to you because the TTNaviagtor broken on iOS 5. see https://github.com/facebook/three20/pull/719/files . Have you tried using the same code on iOS 4 with the same result?

My recommendation to you is to stop using TTNaviagtor . You can still use the three20 library by clicking and pressing TTViewController in the native ios method.

Here is an example of replacing TTNaviagtor in application deletion:

 @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; 

and

 /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// @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; } 

then you can click and put any TTViewController (or your own TTViewController subclasses) into _masterNavController . Personally, I think that TTNavigator is a bad design model, and the apple has developed its navigation system in different ways.

+4
source share

why not go into the code and check what happens?

I believe that objects are created in TTURLMap objectForURL:query:pattern: you can set a breakpoint and see why a new one is created, reuse the old one instead.

this is an implementation of objectForURL:query:pattern: with my comment

 - (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; } } 
0
source share

All Articles