尝试覆盖Objective-C(iPhone)中的方法时出现问题 [英] Problems trying to override methods in Objective-C (iPhone)

查看:72
本文介绍了尝试覆盖Objective-C(iPhone)中的方法时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题,我有一个继承UITableViewController类的类X和一个继承X类的类Y,当我尝试覆盖Y类中的方法时,将调用X类中的方法...并且我可以找不到参考来了解正在发生的事情...有人可以帮助我吗?

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?

提前谢谢!

代码!

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

希望有帮助...

推荐答案

我只是简要地浏览了一下您的代码,但是从代码和输出看,您显然分配了类X的实例(mluListBuilder).

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).

当然,您不能期望拥有Y类(mluLawyerCaseSituationsList)的方法,该方法是在Y从X派生而对象是X类时执行的.

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.

这篇关于尝试覆盖Objective-C(iPhone)中的方法时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆