【发布时间】:2012-04-24 15:38:20
【问题描述】:
为什么继承必须从一个 NSObject 开始,然后在另一个类中使用继承?
想知道为什么我有一个来自 UIViewController 的子类“FatherClass”。 所以我想创建一个继承FatherClass(ChildClass:FatherClass)。
我不能这样做,我得到一个错误日志。 我在这里和 Google 搜索的所有示例都以 NSObject 作为父亲开头。
所以我的问题必须如此?还是我在某个地方错了。
这是错误日志
#0 0x33d6c6f8 in CFStringGetCharacters ()
CoreFoundation`CFStringGetCharacters:
0x33d6c6f8: push.w {r8, r10}
谢谢
使用代码在此处编辑
FatherClass.h
@class ChildClass;
@interface FatherClass : UIViewController <UITabBarControllerDelegate, UINavigationBarDelegate, UIPopoverControllerDelegate, MKMapViewDelegate, MKAnnotation>
{
//Some variables
}
@property (nonatomic, strong) ChildClass *sidebar_table_controller;
-(void) show_modal: (id) sender; // Thats the Method i want to call
@end
FatherClass.m
#import "FatherClass.h"
#import "ChildClass.h"
@interface FatherClass ()
@end
@implementation FatherClass
@synthesize sidebar_table_controller;
// All inits here... DidiLoad, DidUnload, etc..
-(void) show_modal: (id) sender // Thats the Method!!!
{
// All initiate ChildClass an then.. present modal
[self presentModalViewController:self.sidebar_table_controller animated:YES];
NSLog(@"Clicked ?...%@", sender);
}
@end
现在的孩子
ChildClass.h
@interface ChildClass : FatherClass <UINavigationControllerDelegate, UITableViewDataSource, UITableViewDelegate>
@end
ChildClass.m
#import "ChildClass.h"
@interface ChildClass ()
@end
@implementation ChildClass
// All inits here... DidiLoad, DidUnload, etc..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here i want to call Father Method
[FatherClass show_modal:indexPath];
}
@end
当我将 ChildClass : UIViewController 更改为 ChildClass : FatherClass 我的应用程序崩溃。
【问题讨论】:
-
从 UIViewController 继承没有错。展示如何创建“FatherClass”和“ChildClass”的代码。创建这些类时,您是否从编译器收到任何错误或警告?
-
奇怪的是,这是使用 BreakPoint 而没有 BreakPoint 时唯一的错误。但我会编辑我的问题并输入代码给你看。
-
如果没有与您用于创建 UIViewController 子类的代码相关的编译器错误或警告,则可能需要堆栈回溯。 CFStringGetCharacters() 中的一条 x86 指令确实不足以说明发生了什么。
-
代码已修改,请查看是否更好,谢谢
-
貌似show_modal是一个实例方法,但是你却试图调用FatherClass的一个类方法。
标签: objective-c inheritance subclass nsobject