【问题标题】:Calling instance method to access IBOutlet instance from another class调用实例方法从另一个类访问 IBOutlet 实例
【发布时间】:2012-03-23 16:46:41
【问题描述】:

我一直在搜索很多论坛,但它仍然让我发疯。 我不明白如何从试图更改值的类“B”访问 IBOutlet 实例,例如类“A”的实例类型是 IBOutlet NSTextLabel *progressStatus,字符串值“Capturing”想要更改为“正在识别”。只能从 classA 自身的实例方法调用,不能从其他类调用。

@interface classA : NSView
{
      NSTextField *progressStatus;
}
@property (assign) IBOutlet NSTextField *progressStatus;
-(void)recognizeStatus;
@end    

#import "classA.h"

@implementation classA
@synthesize progressStatus;

-(void)awakeFromNib
{
      [self recognizeStatus];     //successfully change the value inside progressStatus
}

-(void)recognizeStatus
{
      [progressStatus setStringValue:@"Recognizing"];
      NSLog(@"Progress Status : %@",progressStatus.stringValue);
}

- (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection*)connection
{
      //code to convert videoFrame to IplImage type named frameImage

      BOOL faceDetected = [classB faceDetection:frameImage];
}
@end

#import "classA.h"

@interface classB : NSObject
{

}
+(BOOL)faceDetection:(IplImage*)source;
@end

#import "classA.h"
#import "classB.h"

@implementation classB
+(BOOL)faceDetection:(IplImage*)source
{
       classA *status = [[classA alloc] init];    
       [status recognizeStatus];                     //no changes with the value inside progressStatus

       //return bool type
}
@end

【问题讨论】:

  • //this is not worked! 无法帮助我们了解您的问题。你预期会发生什么,你实际看到了什么?崩溃、编译器错误、UI 没有按照您的预期改变?
  • 它在 UI 中没有改变,即使在 B 类中一次没有调用方法识别状态。我希望成功调用识别状态方法,所以 A 类中的 NSTextField 将它的文本字段从“捕获" 到 "认识"。

标签: objective-c xcode macos cocoa iboutlet


【解决方案1】:

classB 的类方法faceDetection:中,您初始化了一个派生自NSViewclassA 实例,但您没有将该实例添加到视图层次结构中。此外,在初始化 classA 的实例时,您不会从 nib 加载视图,因此 classA status 的实例可能有 nil progressStatus

我怀疑您可能正在尝试访问已初始化并添加到其他地方的视图层次结构的classA 实例;但初始化另一个实例不会为您提供对原始实例的引用。

【讨论】:

  • 确实是的,我不知道如何将实例添加到视图层次结构并将其引用到原始实例。我从另一个帖子中看到使用委托,但我不知道如何使用它。
  • 这取决于classB 是否持有对classA 现有实例的引用。如果没有,那么你需要重新设计两个类之间的关系。委托模式仍然需要两个类之间的引用关系,尽管很弱。或者,您可以使用NSNotificationCenter 发布来自classB 的通知; classA 可以注册这些通知。
  • 好的,我会尝试使用通知中心。你有这个样本吗?
猜你喜欢
  • 2015-06-10
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
  • 2016-01-15
  • 2012-12-18
  • 1970-01-01
  • 2014-11-05
相关资源
最近更新 更多