-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 が確実に呼び出されるようにするには、JACenterViewController を nib でインスタンス化します。おそらく好き 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 に答える