Question

I'm new with Cocoa and XCode, I would like to receive your help. I made my Controller of the XIB Window, and started my window and I have one parameter that I want to my controller need to have, y set the parameter but when I click a Button and I need to recover the parameter the value is nill or empty.

For example...

I have

#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

I use my controller as follow:

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

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

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

When I display the NSSAlert it always show me cad_ as a blank or empty string, I have tried a lot of ways, but I dont know why I lose the value.

Was it helpful?

Solution

Looks like you're not telling your controller to retain your properties.

You have

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

Which defaults to assigning values, which means they could disappear on you.

Try changing those lines to:

@property (strong) NSString *cad;
@property (strong) ViewerController* viewerController;

OTHER TIPS

I'm trying to set the value of a NSString by Reference, in my Controller Of the XIB I have

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

And In my Code...

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

Just That, I mean i have other object that is the one that I really need to send, but If I can not send a NSString It is by the same reason, I have almost 2 days with this.

I have used the setter too, but I got the same result, when I print the NSString whith NSSAlert It printed the String as a Empty or Nill

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top