左側のクラスをビュー コントローラーの 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 を呼び出すようにしたいですか?