-2

我无法让我的左侧类与视图控制器 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?

4

2 回答 2

0

第一行理论上应该可以工作,但是您没有以调用 viewDidLoad 的方式实例化您的 JACenterViewController - 因此您的 CustomClass 实例没有加载。

要确保调用 viewDidLoad,请使用 nib 实例化您的 JACenterViewController。大概喜欢 JACenterViewController *viewController = [[JACenterViewController alloc] initWithNibName:nil bundle:nil];

这样,您的 viewDidLoad 将被调用,然后您应该实例化您的委托。

于 2013-03-14T11:32:44.587 回答
-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;

}

您喜欢传递和接收消息的任何类都在该类中包含此头文件,并使用工厂方法进行初始化。

于 2013-03-15T09:18:07.330 回答