尝试重写 Objective-C (iPhone) 中的方法时出现问题

发布于 2024-08-16 03:23:36 字数 8864 浏览 2 评论 0原文

这是我的问题,我有一个继承 UITableViewController 类的类 X 和一个继承 X 类的类 Y,当我尝试重写 Y 类中的方法时,会调用 X 类中的方法...并且我找不到参考资料以了解发生了什么...任何人都可以帮助我吗?

提前致谢!

代码!

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

希望有帮助...

this my problem i have a class X that inherits UITableViewController class and a class Y that inherits the X class, when i try to override a method in the Y class the method in the X class is invoked... and i can't find references to understand what's happening... can anyone help me?

Thanks in advance!

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

Calling the 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]
                                                     ];

Output... :(

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

Hope it helps...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埖埖迣鎅 2024-08-23 03:23:36

我只是简单地浏览了您的代码,但很明显(从代码和输出来看)您分配了类 X (mluListBuilder) 的实例。

当然,您不能指望拥有 Y 类的方法 (mluLawyerCaseSituationsList),该方法在 Y 派生自 X 并且对象属于 X 类时执行。

I’ve been looking through your code only briefly, but it seems obvious (from code and from the output) that you allocate an instance of class X (mluListBuilder).

Of course, you cannot expect to have a method of class Y (mluLawyerCaseSituationsList), performed when Y is derived from X and the object is of class X.

誰認得朕 2024-08-23 03:23:36

所以,您有:

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

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

您正在调用 -method,但它是在 X 而不是 Y 上调用的?唯一可能发生的情况是,如果您拥有 X 而不是 Y 的实例(或者如果有人在运行时玩非常愚蠢的游戏——不太可能)。

NSLog(@"%@", [self class]); 添加到方法实现中,看看实例的类到底是什么!

So, you have:

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

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

You are calling -method, but it is being invoked on X, not Y? Only way that can happen is if you have an instance of X instead of Y (or if someone is playing very silly buggers with the runtime -- unlikely).

Add NSLog(@"%@", [self class]); to the method implementations and see what the class of the instance really is!

仅此而已 2024-08-23 03:23:36

您在问题中没有向我们提供太多信息,但以下是它应该如何工作:

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

You don't give us much information in your question, but the following is how it should work:

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

Elsewhere:

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

Output:

method in Class_X was called
method in Class_Y was called
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文