我无法让我的左侧类与视图控制器ivars方法交互,我认为它可能是做多重继承但我不知道如何重构这个以解决我的问题...
JACenterViewController - >继承CustomClass.h //使用alloc / init创建对象...
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继承的我的Custom类:
#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?
答案 0 :(得分:0)
理论上第一行应该可行,但是你没有以调用viewDidLoad的方式实例化你的JACenterViewController - 因此你的CustomClass实例没有被加载。
要确保调用viewDidLoad,请使用nib实例化JACenterViewController。
可能就像
JACenterViewController *viewController = [[JACenterViewController alloc] initWithNibName:nil bundle:nil];
这样你的viewDidLoad就会被调用,然后你应该让你的委托实例化。
答案 1 :(得分:-2)
单例: 一个总是返回自身相同实例的类。它为类中的所有资源提供了一个全局访问点。
CustomClass.h中的:添加
+(CustomClass*)sharedObject;
CustomClass.m中的:添加
static CustomClass *sharedSingleton = nil;
+(CustomClass*)sharedObject {
@synchronized(self) {
if (!sharedSingleton) {
sharedSingleton = [[CustomClass alloc] init];
} return sharedSingleton;
}
}
+(id)alloc {
@synchronized(self) {
NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton");
sharedSingleton = [super alloc];
return sharedSingleton;
} return nil;
}
您希望传递和接收消息的任何类都包含该类中的头文件,并使用工厂方法进行初始化。