我想隐藏一个对象在我的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