2009-12-17 9 views
3

ce problème mon problème j'ai une classe X qui hérite de la classe UITableViewController et une classe Y qui hérite de la classe X, quand j'essaye de surcharger une méthode dans la classe Y la méthode dans la classe X est invoqué ... et je ne trouve pas de références pour comprendre ce qui se passe ... quelqu'un peut-il m'aider?Problèmes en essayant de surcharger les méthodes en Objective-C (iPhone)

Merci d'avance!

Code!

mluListBuilder.h

#import <UIKit/UIKit.h> 

@interface mluListBuilder : UITableViewController { 
    NSString    *sListTitle; 
    NSString    *sEntityName; 
    NSArray     *aEntityProperties; 
    NSMutableArray   *maListRecords; 
    NSManagedObjectContext *mocList; 
    NSFetchRequest   *frListRecords; 
    NSEntityDescription  *edListRecords; 
    NSArray     *aOrderByProperties; 
    NSArray     *aToolBarItems; 
    NSArray     *aToolBarItemsActions; 
} 

@property (nonatomic, retain) NSString     *sListTitle; 
@property (nonatomic, retain) NSString     *sEntityName; 
@property (nonatomic, retain) NSArray     *aEntityProperties; 
@property (nonatomic, retain) NSMutableArray   *maListRecords; 
@property (nonatomic, retain) NSManagedObjectContext *mocList; 
@property (nonatomic, retain) NSFetchRequest   *frListRecords; 
@property (nonatomic, retain) NSEntityDescription  *edListRecords; 
@property (nonatomic, retain) NSArray     *aOrderByProperties; 
@property (nonatomic, retain) NSArray     *aToolBarItems; 
@property (nonatomic, retain) NSArray     *aToolBarItemsActions; 


- (id) initWithStyle:   (UITableViewStyle) style 
    listTitle:     (NSString *)  psListTitle 
    entityName:     (NSString *)  psEntityName 
    entityProperties:   (NSArray *)   paEntityProperties 
    orderListByProperties:  (NSArray *)   paOrderByProperties 
    toolBarItems:    (NSArray *)   paToolBarItems 
    toolBarItemsActions:  (NSArray *)   paToolBarItemsActions; 

- (void)newRecord; 
- (void)deleteRecord; 

@end 

mluListBuilder.m

#import "mluListBuilder.h" 

@implementation mluListBuilder 

@synthesize sListTitle, 
      sEntityName, 
      aEntityProperties, 
      maListRecords, 
      mocList, 
      frListRecords, 
      edListRecords, 
      aOrderByProperties, 
      aToolBarItems, 
      aToolBarItemsActions; 


- (id) initWithStyle:   (UITableViewStyle) style 
    listTitle:     (NSString *)  psListTitle 
    entityName:     (NSString *)  psEntityName 
    entityProperties:   (NSArray *)   paEntityProperties 
    orderListByProperties:  (NSArray *)   paOrderByProperties 
    toolBarItems:    (NSArray *)   paToolBarItems 
    toolBarItemsActions:  (NSArray *)   paToolBarItemsActions 
{ 

    sListTitle    = psListTitle; 
    sEntityName    = psEntityName; 
    aEntityProperties  = paEntityProperties; 
    aOrderByProperties  = paOrderByProperties; 
    aToolBarItems   = paToolBarItems; 
    aToolBarItemsActions = paToolBarItemsActions; 

    if (self = [super initWithStyle:style]) { 
    } 
    return self; 
} 

- (void)viewDidLoad { 
    self.title = NSLocalizedString(sListTitle, nil); 

    if ([aToolBarItems count] > 0) { 
     NSMutableArray *maToolBarItems = [[NSMutableArray alloc] init]; 
     self.navigationController.toolbarHidden = NO; 
     for (int i = 0; i < [aToolBarItems count]; i++) { 
      UIBarButtonItem * bbiToolBarItem = [[UIBarButtonItem alloc] 
               initWithTitle:NSLocalizedString([aToolBarItems objectAtIndex:i], nil) 
               style:UIBarButtonItemStyleBordered 
               target:self 
               action:NSSelectorFromString([aToolBarItemsActions objectAtIndex:i]) 
               ]; 


      [maToolBarItems addObject:bbiToolBarItem]; 
     } 
     self.toolbarItems = maToolBarItems; 
    } else { 
     self.navigationController.toolbarHidden = YES; 
    } 

    if (mocList != nil) { 
     frListRecords = [[NSFetchRequest alloc] init]; 

     NSSortDescriptor *sdListRecords = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 

     [frListRecords setSortDescriptors:[[NSArray alloc] initWithObjects:sdListRecords, nil]]; 

     edListRecords = [NSEntityDescription entityForName:sEntityName inManagedObjectContext:mocList]; 

     [frListRecords setEntity:edListRecords]; 

     NSError *errFetchRequest; 
     maListRecords = [[mocList executeFetchRequest:frListRecords error:&errFetchRequest] mutableCopy]; 
    } 
    [super viewDidLoad]; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    NSError *errFetchRequest; 
    maListRecords = [[mocList executeFetchRequest:frListRecords error:&errFetchRequest] mutableCopy]; 
    [self.tableView reloadData]; 

    if (self.navigationController.toolbarHidden == YES) { 
     if ([aToolBarItems count] > 0) { 
      self.navigationController.toolbarHidden = NO; 
     } 
    } 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


#pragma mark Table view methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [maListRecords count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    for (UIView *vwExisting in cell.contentView.subviews) { 
     [vwExisting removeFromSuperview]; 
    } 

    NSEntityDescription *edCurrentRecord = [maListRecords objectAtIndex:indexPath.row]; 

    UILabel *lblCell = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, 280, 20.0)]; 
    [lblCell setText:edCurrentRecord.name]; 

    [cell.contentView addSubview:lblCell]; 

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Navigation logic may go here. Create and push another view controller. 
    // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil]; 
    // [self.navigationController pushViewController:anotherViewController]; 
    // [anotherViewController release]; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

- (void)newRecord { 
    NSLog(@"%@", [self class]); 
} 

- (void)deleteRecord { 

} 

@end 

mluLawyerCaseSituationsList.h

#import <Foundation/Foundation.h> 
#import "mluListBuilder.h"; 

@interface mluLawyerCaseSituationsList : mluListBuilder { 

} 

- (void)newRecord; 

@end 

mluLawyerCaseSituationsList.m

#import "mluLawyerCaseSituationsList.h" 

@implementation mluLawyerCaseSituationsList 

- (void)newRecord { 
    NSLog(@"%@", [self class]); 
} 

@end 

Appeler le mluLawyerCaseSituationsList

mluLawyerCaseSituationsList *vcCaseSituations = [[mluListBuilder alloc] 
                initWithStyle:UITableViewStylePlain 
                listTitle:@"titCaseSituations" 
                entityName:@"case_situations" 
                entityProperties:[[NSArray alloc] initWithObjects:@"name", nil] 
                orderListByProperties:[[NSArray alloc] initWithObjects:@"name", nil] 
                toolBarItems:[[NSArray alloc] initWithObjects:@"btNew", nil] 
                toolBarItemsActions:[[NSArray alloc] initWithObjects:@"newRecord", nil] 
                ]; 

sortie ... :(

2009-12-17 17: 30: 02,726 mluLawyer [ 2862: 20b] mluListBuilder

Hope it helps ...

+1

Pas moyen de vous aider sans code. Généralement, le dépassement fonctionne bien dans Objective-C. Il doit y avoir quelque chose qui ne va pas dans votre code. –

+0

D'après mon expérience personnelle, je recommande fortement de ne pas étendre UITableViewController (ou l'une des classes fournies de manière similaire) pour créer un point d'extension réutilisable. Vous n'avez aucune idée de ce qui se passe dans la mise en œuvre originale d'Apple et cela ne va mener à un sac de mal plus tard. Au lieu de cela, mettez un comportement commun dans une catégorie. – bpapa

Répondre

4

que je cherchais dans votre code que brièvement, mais il semble évident (à partir du code et de la sortie) que vous allouez une instance de la classe X (mluListBuilder).

Bien sûr, vous ne pouvez pas attendre d'avoir une méthode de la classe Y (mluLawyerCaseSituationsList), effectuée lorsque Y est dérivé de X et l'objet est de classe X.

3

Alors, vous avez:

@interface X : UITableViewController 
- (void) method; 
@end 

@interface Y : X 
- (void) method; 
@end 

Vous appelez -method, mais il est invoqué sur X, Y non? La seule façon qui peut arriver est si vous avez une instance de X au lieu de Y (ou si quelqu'un joue des bêtises très stupides avec l'exécution - peu probable).

Ajoutez NSLog(@"%@", [self class]); aux implémentations de la méthode et voyez la classe de l'instance!

1

Vous ne nous donnez pas beaucoup d'informations dans votre question, mais ce qui suit est la façon dont il devrait travail:

Class_X.h:

@interface Class_X : UITableViewController 
{ 
} 
- (void)someMethod; 
@end 

Class_X.m:

#import "Class_X.h" 

@implementation Class_X 
- (void)someMethod 
{ 
    NSLog(@"method in Class_X was called"); 
} 
@end 

Class_Y.h:

#import "Class_X.h" 

@interface Class_Y : Class_X 
{ 
} 
- (void)someMethod; 
@end 

Class_Y.m:

#import "Class_Y.h" 

@implementation Class_Y 
- (void)someMethod 
{ 
    NSLog(@"method in Class_Y was called"); 
} 
@end 

Ailleurs:

#import "Class_Y.h" 

... 

Class_X * x_instance = [[Class_X alloc] init]; 
Class_Y * y_instance = [[Class_Y alloc] init]; 

[x_instance someMethod]; 
[y_instance someMethod]; 

[Class_Y release]; 
[Class_X release]; 

Sortie:

method in Class_X was called 
method in Class_Y was called