English 中文(简体)
从其他类中隐藏对象
原标题:hide an object from another class
  • 时间:2012-05-26 06:55:08
  •  标签:
  • xcode

我试图隐藏一个物体 在我眼中的主计长, 代码执行 从定制类, 但该物体是零。

< pronger > FirstView 主计长.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController {
    IBOutlet UILabel *testLabel;
}

@property (nonatomic, retain) IBOutlet UILabel *testLabel;

- (void) hideLabel;

FirstViewController.m I synthesize testLabel and I have a function to hide it. If I call the function from viewDidAppear it works, but I want to call it from my other class. When called from the other class, testLabel is nil

#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
}

< 坚固 > 其他千字节.h

@class FirstViewController;

#import <Foundation/Foundation.h>

@interface OtherClass : NSObject {
    FirstViewController *firstViewController;
}

@property (nonatomic, retain) FirstViewController *firstViewController;

-(void)hideThem;

@end

OtherClass.m calls the hideLabel function in FirstViewController. In my original project, (this is an example obviously, but the original project is at work) I download some data here and I want to hide my loading label and indicator when download is done

#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];
}

有什么想法吗?

最佳回答

您的 UILabel 是零, 因为您刚刚初始化了您的控制器, 但是没有装入它视图。 您的控制器的 IBoutlets 被自动从xib 或故事板上立即激活, 当你第一次请求访问约束视图时, 所以为了访问它们, 您必须首先以某种方式装入它的观点 。

<强度 > EDIT (在业务方案评论之后):

Since your FirstViewController is already initialized and your OtherClass is instantiated by that controller, you could just hold a reference to it and not try to initialize a new one. So try something like this:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    OtherClass *otherClass = [[OtherClass alloc] init];
    otherClass.firstViewController = self;
    [otherClass hideThem];
}

在您的 < strong > 其它星系. m < / strong > 中 :

-(void)hideThem {
    [self.firstViewController hideLabel];
}
问题回答

暂无回答




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签