0

我是 Cocoa 和 XCode 的新手,我想得到你的帮助。我制作了 XIB 窗口的控制器,并启动了我的窗口,我有一个我希望我的控制器需要的参数,你设置了参数,但是当我单击一个按钮并且我需要恢复参数时,该值是 nill 或空的。

例如...

我有

#import <Cocoa/Cocoa.h>
#import "ITKExample1Filter.h"
#import "Helper.h"

@interface VentanaGraficaController1 : NSWindowController {
    ViewerController* _viewerController;
    IBOutlet NSComboBox *combo;
    IBOutlet NSTextField *windowLevelTextField;
    IBOutlet NSTextField *windowWidhTextField;
    NSString *cad_;
}


@property NSString *cad;
@property ViewerController* viewerController;
//Metodos
- (IBAction) converToRGB: (id)sender;
- (IBAction) initAux: (id)sender; 
- (void) initController : (ViewerController*) vc: (int) n;

@end

@implementation VentanaGraficaController1

//Parametros que sintetizaremos para manejar los valores por referencia
@synthesize viewerController = _viewerController;
@synthesize cad = cad_;

// .....

- (IBAction) converToRGB: (id)sender {
    NSAlert *a = [NSAlert alertWithMessageText:cad_   
                                 defaultButton:@"OK"
                               alternateButton:@"Cancel"
                                   otherButton:nil
                     informativeTextWithFormat:@"Cadena por syntetise"];
    [a runModal];
}
// ..... The rest

我使用我的控制器如下:

VentanaGraficaController1 *controller = [[VentanaGraficaController1 alloc] initWithWindowNibName:@"VentanaGraficaController1"];

[controller setViewerController:viewerController];
[controller setViewerController:(viewerController)];
[controller setCad:@"works??"];
controller.viewerController = viewerController;

controller.cad = @"works??";
[controller showWindow:nil];

当我显示 NSSAlert 时,它总是将我显示cad_为空白或空字符串,我尝试了很多方法,但我不知道为什么我失去了价值。

4

2 回答 2

1

看起来你没有告诉你的控制器保留你的属性。

你有

@property NSString *cad;
@property ViewerController* viewerController;

默认为分配值,这意味着它们可能会在您身上消失。

尝试将这些行更改为:

@property (strong) NSString *cad;
@property (strong) ViewerController* viewerController;
于 2012-05-15T06:05:48.803 回答
0

我正在尝试在我的 XIB 控制器中通过引用设置 NSString 的值

@interface VentanaGraficaController1 : NSWindowController {
    ViewerController* _viewerController;
    IBOutlet NSComboBox *combo;
    IBOutlet NSTextField *windowLevelTextField;
    IBOutlet NSTextField *windowWidhTextField;
    NSString *cad_;
}


@property(strong) NSString *cad;

@end

And In my .m file I have the Action of the Button With this Code....

@implementation VentanaGraficaController1

//Parametros que sintetizaremos para manejar los valores por referencia
@synthesize viewerController = _viewerController;
@synthesize cad = cad_;

- (id)initWithWindow:(NSWindow *)window 
{
    self = [super initWithWindow:window];

    if (self) {
        // Initialization code here.

    }

    return self;
}

- (IBAction) converToRGB: (id)sender {
    NSAlert *a = [NSAlert alertWithMessageText: 
                                 cad_
                                 defaultButton:@"OK" alternateButton:@"Cancel"

                                   otherButton:nil informativeTextWithFormat:

                  @"Just a Test"];
    [a runModal];
}

在我的代码中......

    VentanaGraficaController1 *controller = [[VentanaGraficaController1 alloc] initWithWindowNibName:@"VentanaGraficaController1"]; 
   controller.cad = @"My striiiiing"; 
   [controller showWindow:nil]; 
   return 0;

就是这样,我的意思是我有其他对象是我真正需要发送的对象,但是如果我不能发送 NSString 出于同样的原因,我有将近 2 天的时间。

我也使用了setter,但是我得到了相同的结果,当我打印带有NSSAlert的NSString时,它将字符串打印为Empty或Nill

于 2012-05-16T03:40:08.157 回答