我无法让我的左侧类与视图控制器 ivars 方法进行交互,我认为它可能是进行多重继承,但我不知道如何重构它来解决我的问题......</p>
JACenterViewController -> 继承 CustomClass.h // 使用 alloc/init 创建对象...</p>
JALeft -> 继承 JACenterViewController // 我想说 [viewController myCustomClass] helloDelegate];
视图控制器类:
#import "CustomClass.h"
@interface JACenterViewController : UIViewController <CustomClassDelegate>
@property (nonatomic, retain) CustomClass *myCustomClass;
-(void)pushMe;
@end
#import "JACenterViewController.h"
@implementation JACenterViewController
@synthesize myCustomClass;
// delegrate method
-(void)sayHello:(CustomClass *)customClass {
[customClass helloDelegate];
}
// class method
- (void)viewDidLoad {
[super viewDidLoad];
self.myCustomClass = [[CustomClass alloc] init];
[self.myCustomClass setDelegate:self];
[[self view] addSubview:[self.myCustomClass createButton]];
}
-(void)pushMe {
NSLog(@"push me %@", [self class]);
[myCustomClass helloDelegate];
}
@end
我的 VC 继承的自定义类:
#import <Foundation/Foundation.h>
@class CustomClass;
@protocol CustomClassDelegate
-(void)sayHello:(CustomClass *)customClass;
@end
@interface CustomClass : NSObject
@property (nonatomic, assign) id delegate;
@property UIButton *button;
-(void)helloDelegate;
-(UIButton*)createButton;
@end
#import "CustomClass.h"
@implementation CustomClass
@synthesize delegate, button;
-(id)init {
self = [super init];
return self;
}
-(void)helloDelegate {
NSLog(@"Hello Delegate working!!: %@", [self class]);
[self.button setTitle:@"Title" forState:UIControlStateNormal];
}
-(UIButton*)createButton {
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.button setFrame:CGRectMake(0, 0, 100, 100)];
return self.button;
}
@end
最后是我的左侧课程(顺便说一句:这个课程从故事板加载):
#import "JACenterViewController.h"
@interface JALeft : UIViewController
@end
#import "JALeft.h"
@implementation JALeft
- (void)viewDidLoad {
JACenterViewController *viewController = [[JACenterViewController alloc] init];
NSLog(@"Respones %d", [[viewController myCustomClass] respondsToSelector:@selector(helloDelegate)]);
NSLog(@"Respones %d", [self respondsToSelector:@selector(helloDelegate)]);
NSLog(@"Respones %d", [super respondsToSelector:@selector(helloDelegate)]);
/* NOT WORKING: Method should call customClass methods that has been inherited from ViewController */
[[viewController myCustomClass] helloDelegate];
}
@end
我希望我的 JALeft 类在继承时从视图控制器类调用方法 helloDelegate?