Three20 : how to pass a class of objects between 2 views
Asked Answered
R

4

7

I have a TTableView. The items in this table a mapped to an url, so that when I click on an item, another view appear with informations about this item. All these informations are attributes of a class. So, how can I build my TTableTextItem URL in order to transmit the class containing informations to the view responsible for the display of these informations ?

Thanks in advance.

Residentiary answered 16/10, 2010 at 14:10 Comment(0)
D
13

One way of doing it is to use a TTURLAction. When the user selects a row in your table, which will call your didSelectObject (of TTTableViewController) method, extract the object or set of objects you want to pass and build a TTURLAction like this:

TTURLAction *action =  [[[TTURLAction actionWithURLPath:@"tt://showUser"] 
    applyQuery:[NSDictionary dictionaryWithObject:user forKey:@"kParameterUser"]]
            applyAnimated:YES];

Then open the action:

[[TTNavigator navigator] openURLAction:action];

The controller you want to open as a result of this action should be registered in your TTURLMap and should have a constructor thus:

- (id) initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
    self = [super init];
    if (self != nil) {
        self.user = [query objectForKey:kParameterUser];
    }
    return self;
}

I tend to create categories on classes for objects I want to be able to open another controller and display themselves.

Doggo answered 17/10, 2010 at 10:17 Comment(2)
in version1.0.5, there is no interface of actionWIthURLPath:applyQuery: in TTURLAction , then how to handle this issue ?At
a little improvement- [query objectForKey:kParameterUser]; will be- [query objectForKey:@"kParameterUser"];Gudrin
F
6

The big problem with directly using TTURLAction is that you can't really use them with TTTableViewItem. The only way to really do it is override -didSelectObject:atIndexPath: and build your custom TTURLAction with your desired object in the query dictionary. But this breaks the nice separation of Model and View Controller, and gets complicated once you have multiple objects to pass.

Instead of this, I've been using a small category which automatically takes the userInfo property of the table item (which I set to whatever I need), and automatically adds it as a URL parameter.

And then you use this to retrieve it in your mapped view controller.

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

}

You can download it as a GitHub Gist here. The code is also below. We're considering merging this into the main branch at some point.


TTTableViewDelegate+URLAdditions.h

@interface TTTableViewDelegate(URLAdditions)

@end

TTTableViewDelegate+URLAdditions.m

#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
Flagelliform answered 24/10, 2010 at 18:9 Comment(0)
O
0

I think the documentation on the official website does describe very clearly the navigation scheme for Three20. Your question is the very common task of any application, and Three20 provides powerful support for that.

Orthodontist answered 16/10, 2010 at 14:32 Comment(1)
Yes, I already read that. But this documentation is very poor : it doesn't explain how to retrieve attributes from the passed object, how to access to the passed object in the target view... That's where I'm stuck. An example would be welcome. Thanks.Residentiary
S
0

I have little fix this awesome code :) as posted version strip callback from TTTableButton.

Correction is:

if( [object isKindOfClass:[TTTableButton class]] ) {
   if (item.delegate && item.selector) {
       [item.delegate performSelector:item.selector withObject:object];
   }
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Sonority answered 1/2, 2011 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.