public
Last active

Passing objects in URLs via userInfo

  • Download Gist
TTTableViewDelegate+URLAdditions.h
Objective-C
1 2 3 4 5 6 7 8 9
//
// TTTableViewDelegate+URLAdditions.h
//
// Original work by Mike DeSaro of Prime31 Studios
// Gist created by Michael Grinich
 
@interface TTTableViewDelegate(URLAdditions)
 
@end
TTTableViewDelegate+URLAdditions.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
//
// TTTableViewDelegate+URLAdditions.m
//
// Original work by Mike DeSaro of Prime31 Studios
// Gist created by Michael Grinich
 
#import "TTTableViewDelegate+URLAdditions.h"
 
 
@implementation TTTableViewDelegate(URLAdditions)
 
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
id<TTTableViewDataSource> dataSource = (id<TTTableViewDataSource>)tableView.dataSource;
id object = [dataSource tableView:tableView objectForRowAtIndexPath:indexPath];
 
// Added section to automatically wrap up any TTTableItem userInfo objects. If it is a dictionary, it gets sent directly
// If it is not, it is put in a dictionary and sent as they __userInfo__ key
if( [object isKindOfClass:[TTTableLinkedItem class]] ) {
TTTableLinkedItem* item = object;
 
if( item.URL && [_controller shouldOpenURL:item.URL] ) {
// If the TTTableItem has userInfo, wrap it up and send it along to the URL
if( item.userInfo ) {
NSDictionary *userInfoDict;
 
// If userInfo is a dictionary, pass it along else create a dictionary
if( [item.userInfo isKindOfClass:[NSDictionary class]] ) {
userInfoDict = item.userInfo;
} else {
userInfoDict = [NSDictionary dictionaryWithObject:item.userInfo forKey:@"__userInfo__"];
}
 
[[TTNavigator navigator] openURLAction:[[[TTURLAction actionWithURLPath:item.URL]
applyQuery:userInfoDict]
applyAnimated:YES]];
} else {
TTOpenURL( item.URL );
}
}
 
if( [object isKindOfClass:[TTTableButton class]] ) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
else if( [object isKindOfClass:[TTTableMoreButton class]] ) {
TTTableMoreButton* moreLink = (TTTableMoreButton*)object;
moreLink.isLoading = YES;
TTTableMoreButtonCell* cell
= (TTTableMoreButtonCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.animating = YES;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
 
if( moreLink.model )
[moreLink.model load:TTURLRequestCachePolicyDefault more:YES];
else
[_controller.model load:TTURLRequestCachePolicyDefault more:YES];
}
}
 
[_controller didSelectObject:object atIndexPath:indexPath];
}
 
@end

In your mapped view, use the following code to retrieve the userInfo object.

- (id)initWithNavigatorURL:(NSURL *)URL query:(NSDictionary *)query { 
    if (self = [self initWithNibName:nil bundle:nil]) {
        id myPassedObject = [query objectForKey:@"__userInfo__"];
        // do the rest of your initlization
    }
    return self;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.