我無法讓左側類與視圖控制器ivars方法進行交互,我認爲它可能會執行多重繼承,但我沒有得到任何線索如何重構這個解決我的問題......已從另一個類繼承的Objective-C調用方法
JACenterViewController - >繼承CustomClass.h //使用創建對象分配/初始化...
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
而我最後我的左邊類(BTW:從故事板該類負載):
#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方法,因爲它是繼承?
要調用繼承類中的方法,請調用該方法。還有另外一個問題嗎?有沒有錯誤信息? – 2013-03-14 11:20:46
什麼是預期和實際行爲?我對預期產生了一個微弱的線索,並想知道實際行爲是什麼。 – 2013-03-14 11:20:53
根本沒有編譯器錯誤,我想從我的JALeft類中調用helloDelegate。如果你看看我的JALeft類,那麼看到我無法得到任何響應總是返回0 – 2013-03-14 11:26:12