2013-06-06 25 views
0

我想從我的viewcontoller訪問另一個類,但訪問NSObject類不工作:的iOS從我的viewController

viewcontroller.h 

#import <UIKit/UIKit.h> 
@class firstClass; //nsobject class 


@interface ViewController : UIViewController 
{ 
    firstClass *firstclass; 

} 

@property (retain,nonatomic) LEMZfirstClass *firstclass; 

--- 
firstClass.h: 

#import "LEMZViewController.h" 


@interface firstClass : NSObject 
{ 
    ViewController *viewController; 
} 

@property (retain,nonatomic) ViewController *viewController; 


-(void)doSomenthing; 


firstClass.m: 

@synthesize viewController; 


-(void)doSomenthing 
{ 
    viewController.firstclass=self; 
    [email protected]"This is my Label"; 
} 



viewcontroller.m: 

@synthesize firstclass; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [firstclass doSomenthing]; 

} 

它編譯沒有錯誤,但標籤永遠不會更新,併爲此事第一類是永遠不會調用所有的。我做錯了什麼?我會非常感謝你的幫助。

回答

0

幾件事我注意到:

  1. 一般來說,你將有視圖控制器類手柄更新了自己的UI元素,而不是另一個類。
  2. 你的outPutLabel變量在哪裏?它是通過在InterfaceBuilder中連接的代碼或IBOutlet創建的嗎?
  3. 在你可以調用firstclass的東西之前,你必須先創建它。像這樣:

    firstclass = [[firstClass alloc] init]; [firstclass doSomenthing];

viewController.firstclass=self;行將是多餘的。

0

你firstClass.h

#import <Foundation/Foundation.h> 

@interface firstClass : NSObject 
+(NSString *)doSomenthing; //Instance Class 
@end 

firstClass.m

#import "firstClass.h" 

@implementation firstClass 
+(NSString *)doSomenthing 
{ 

    return @"This is my Label"; 
} 
@end 

ViewController.h

#import <UIKit/UIKit.h> 
#import "firstClass.h" 

@interface ViewController : UIViewController 

@end 

ViewController.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 



    [firstClass doSomenthing]; 

    outPutLabel.text=[firstClass doSomenthing];; 

    // Do any additional setup after loading the view, typically from a nib. 
} 

注意:這裏我使用實例類。在你使用這段代碼之前,你必須學習Instance類。

相關問題