我想隱藏一個對象在我的viewController中,從自定義類中執行代碼,但對象爲零。隱藏另一個類的對象
FirstViewController.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
IBOutlet UILabel *testLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *testLabel;
- (void) hideLabel;
FirstViewController.m 我合成testLabel,我有一個函數來隱藏它。如果我從viewDidAppear調用函數,它可以工作,但我想從我的其他類調用它。當從其他類中調用,testLabel是零
#import "FirstViewController.h"
#import "OtherClass.h"
@implementation FirstViewController
@synthesize testLabel;
- (void) hideLabel {
self.testLabel.hidden=YES;
NSLog(@"nil %d",(testLabel==nil)); //here I get nil 1 when called from OtherClass
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
OtherClass *otherClass = [[OtherClass alloc] init];
[otherClass hideThem];
//[self hideLabel]; //this works, it gets hidden
}
OtherClass.h
@class FirstViewController;
#import <Foundation/Foundation.h>
@interface OtherClass : NSObject {
FirstViewController *firstViewController;
}
@property (nonatomic, retain) FirstViewController *firstViewController;
-(void)hideThem;
@end
OtherClass.m 調用FirstViewController的hideLabel功能。在我原來的項目,(這是一個明顯的例子,但原來的項目是在工作中)我在這裏下載一些數據,我想隱藏我的加載標籤和指示燈,當下載完成
#import "OtherClass.h"
#import "FirstViewController.h"
@implementation OtherClass
@synthesize firstViewController;
-(void)hideThem {
firstViewController = [[FirstViewController alloc] init];
//[firstViewController.testLabel setHidden:YES]; //it doesn't work either
[firstViewController hideLabel];
}
任何想法?
謝謝您的回答,但viewDidLoad中被前執行。我通過在viewDidLoad中放入NSLog(@「View Did Load」)來測試它,並在得到「nil 1」之前得到它。 我應該重新加載FirstViewController嗎?我怎麼能這樣做? – CostasKal
嘗試將'alloc init'改爲'alloc initWithNibName:bundle:' – Alladinian
嘗試initWithNibName:@「FirstViewController」bundle:nil。仍然不起作用:( – CostasKal