1

Its complicated to put in words but lemme give it a try. I have a MenuViewController that has an array with category names, tapping on the category rows in tableview instantiate a different view controller using the Storyboard ID.

Now if i use different classes for each view controller, that would be a lot of redundant code and classes. What i want to do is to use one class for all these view controllers lets call it PrimaryViewController and upon selecting different categories in the MenuViewController, it calls different methods or blocks in the PrimaryViewController.

Here is the method in the PrimaryViewController:

- (void) fetchData:(NSInteger )pageNumber
{        
        channel = [[TheFeedStore sharedStore] fetchWebService:pageNumber withCompletion:^(RSSChannel *obj, NSError *err){

            if (!err) {

                int currentItemCount = [[channel items] count];
                channel = obj;
                int newItemCount = [[channel items] count];

                int itemDelta = newItemCount - currentItemCount;
                if (itemDelta > 0) {
                    NSMutableArray *rows = [NSMutableArray array];

                    for (int i = 0; i < itemDelta; i++) {
                        NSIndexPath *ip = [NSIndexPath indexPathForRow:i inSection:0];
                        [rows addObject:ip];
                    }
                    [[self tableView] insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationBottom];
                }
            }
        }];
}

The above code has the ability to load one category. Notice the first line "channel = [[TheFeedStore sharedStore] fetchWebService", the other categories are named "fetchWebServiceCat2", "fetchWebServiceCat3" and "fetchWebServiceCat4" in another class named TheFeedStore.

What i want is when a different view controller is instantiated from the MenuViewController, it should use PrimaryViewController's fetchData method to call a different category method of TheFeedStore.

Thanks!

4

1 回答 1

2
[store fetchWebService:webService withCompletion:completion];

相当于:

[store performSelector:@selector(fetchWebService:withCompletion:)
            withObject:webService
            withObject:completion];

所以你可以这样做:

SEL sel = nil;

if (...) sel = @selector(fetchWebService:withCompletion:);
if (...) sel = @selector(fetchWebServiceCat2:withCompletion:);
...

[store performSelector:sel withObject:webService withObject:^{}];

甚至这样:

SEL sel = NSSelectorFromString([NSString stringWithFormat:@"fetchWebService%@:withCompletion:", @"Cat2"]);
...
于 2013-05-09T23:33:54.703 回答