2009-12-17 67 views
3

這是我的問題我有一個類X繼承了UITableViewController類和一個繼承X類的類Y,當我嘗試重寫Y類中的方法時,方法在X類被調用...我找不到引用來理解發生了什麼...任何人都可以幫助我?試圖覆蓋Objective-C(iPhone)中的方法的問題

在此先感謝!

代碼!

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 

調用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] 
                ]; 

輸出... :(

2009-12-17 17:30:02.726 mluLawyer [ 2862:20b] mluListBuilder

希望它可以幫助...

+1

沒有辦法幫你無需代碼執行。通常,在Objective-C中,覆蓋效果很好。代碼中必須有錯誤。 – 2009-12-17 18:50:58

+0

從個人經驗來看,我強烈建議不要擴展UITableViewController(或任何類似提供的類)來創建可重用的擴展點。你不知道蘋果原來的實施過程中發生了什麼,它只會在以後導致一袋傷害。相反,將普通行爲歸入一個類別。 – bpapa 2009-12-17 19:34:00

回答

4

我一直在尋找通過您的代碼只是暫時的,但它似乎很明顯(從代碼和輸出),您分配類X的一個實例(mluListBuilder)。

當然,你不能指望有類Y(mluLawyerCaseSituationsList)的方法,當Y從X衍生和對象是類X.

3

所以,你必須:

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

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

要調用-method,但它被調用的X,不是Y'只有這樣才能發生的情況是,如果你有一個X的實例而不是Y(或者如果有人在運行時玩弄了非常愚蠢的玩家 - 不太可能)。

NSLog(@"%@", [self class]);添加到方法實現中,並查看該實例的類是什麼!

1

你不給我們太多的信息在你的問題,但下面是如何應該工作:

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 

別處:

#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]; 

輸出:

method in Class_X was called 
method in Class_Y was called