2013-03-14 71 views
-2

我無法讓左側類與視圖控制器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方法,因爲它是繼承?

+0

要調用繼承類中的方法,請調用該方法。還有另外一個問題嗎?有沒有錯誤信息? – 2013-03-14 11:20:46

+0

什麼是預期和實際行爲?我對預期產生了一個微弱的線索,並想知道實際行爲是什麼。 – 2013-03-14 11:20:53

+0

根本沒有編譯器錯誤,我想從我的JALeft類中調用helloDelegate。如果你看看我的JALeft類,那麼看到我無法得到任何響應總是返回0 – 2013-03-14 11:26:12

回答

-2

Singleton: 一個總是返回自身相同實例的類。它爲班內所有資源提供全球接入點。

在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; 

}

你想傳遞和接收消息包括在這個頭文件中的任何類類並使用工廠方法進行初始化。

+0

我認爲他想要一個委託而不是一個單身 - 真的非常不同。 – tobyc 2013-03-15 10:07:56

+0

@half_brick - 啊,但單身人士是每個問題的答案。 – 2013-03-16 13:47:52

0

理論上第一行應該工作,但是您沒有以調用viewDidLoad的方式實例化您的JACenterViewController - 因此您的CustomClass實例未加載。

爲確保調用viewDidLoad,使用nib實例化JACenterViewController。 可能就像 JACenterViewController *viewController = [[JACenterViewController alloc] initWithNibName:nil bundle:nil];

這樣你的viewDidLoad將被調用,然後你應該讓你的委託實例化。

+0

我目前正在使用故事板,所以我翻譯爲此代碼: NSString * deviceMode = [[NSBundle mainBundle]。infoDictionary objectForKey:@「UIMainStoryboardFile」]; UIStoryboard * storyboard = [UIStoryboard storyboardWithName:deviceMode bundle:nil]; ViewController * viewcontroller = [storyboard instantiateViewControllerWithIdentifier:@「viewController」]; 但仍然不能觸及繼承的對象方法,即如果我在視圖控制器上調用一個方法,它的工作原理但是如果我從視圖控制器調用方法helloDelegate我沒有從我的左班級得到任何響應 – 2013-03-14 12:35:12