0

I'm going crazy! I just don't get it. When I start a second window a method is called within the second window controller. The method is doing a lot of calculations and should put some results in labels via outlets. The labels remain empty. I don't know how to make it work.

my AppDelegate.m:

#import "AppDelegate.h"
#import "ToDoItem.h"
#import "ResultWindowController.h"

@implementation AppDelegate

- (IBAction)pushRun:(id)sender {

    if (rwc)
    {
        [rwc close];
    }
    rwc = [[ResultWindowController alloc] init];
    [rwc calculateResults];//add observer
    [rwc setShouldCascadeWindows:NO]; //window re-opens at the same position
    [rwc showWindow:self];
}
@end

my ResultWindowController.h:

#import <Cocoa/Cocoa.h>

@interface ResultWindowController : NSWindowController
{

}
@property (weak) IBOutlet NSTextField *outputResultAverageValue;
@property (weak) IBOutlet NSTextField *outputResultToleranceValue;

-(void)calculateResults;

@end

ResultWindowController.m:

-(void)awakeFromNib
{
    NSString *initial =@"-";
    [_outputResultAverageValue setStringValue:initial];
    [_outputResultToleranceValue setStringValue:initial];
}

- (void)calculateResults
{
double resultAverageValue = 0, resultToleranceValue = 0;

 //calculations
 for-loop{

   resultAverageValue = (maxresult + minresult)/2;
   resultToleranceValue = (maxresult - minresult)/2;
 }

NSLog(@"resultaverage is:%f", resultAverageValue);
[_outputResultAverageValue setDoubleValue:resultAverageValue];
[_outputResultToleranceValue setDoubleValue:resultToleranceValue];
}

NSLog gives me the value I want to display in my Label. I also can initialize my labels using the awakeFromNib method. Do I have a design failure. Do I need to make sure that the labels are set after the calculateResults methode is done?

Thanks in advance!!!

JFS
  • 2,992
  • 3
  • 37
  • 48
  • What is the data type of `_outputResultAverageValue`? Where's the code where you try to set the label's text? – rmaddy Jan 23 '13 at 23:10
  • Hi rmaddy, I added the code above. Does it need to be a text always? `_outputResultAverageValue` is a NSTextfield outlet connected to the label on the nib. – JFS Jan 23 '13 at 23:35

3 Answers3

0
  1. Can you please specify why exactly are you using weak?

  2. Try using:

    [self._outputResultAverageValue setDoubleValue:resultAverageValue];
    [self._outputResultToleranceValue setDoubleValue:resultToleranceValue];
    

    Also, have you connected the outlets?

  3. The idea of ViewControllers is to request the calculation or logic from a model and then submit that to the view. So, I wouldn't really call this to be a design flow but it's better to follow the idea of Model View Controllers in iOS.

p0lAris
  • 4,750
  • 8
  • 45
  • 80
  • I used weak for Outlets and it usually works fine. I did try to use `self.` as you suggested but it still doesn't work. The outlets are connected in the IB. – JFS Jan 23 '13 at 22:49
  • Would like to see how you have initialized your labels. – p0lAris Jan 23 '13 at 22:53
  • @omniDETH You can't call `[self._outputResultAverageValue setDoubleValue:resultAverageValue];`. There is no property named `_outputResultAverageValue`. You can do `self.outputResultAverageValue` or you can do `self->_outputResultAverageValue` which is the same as just `_outputResultAverageValue`. – rmaddy Jan 23 '13 at 23:12
  • Right. I already did adapt the code to `[self.outputResultToleranceValue setDoubleValue: resultToleranceValue];` But why does it still not work. I wonder if there is a conflict in the timing. Do I need to add something to make sure that the data get send after the `calculateResult` method is done? – JFS Jan 23 '13 at 23:29
  • Can you please check if your -awakeFromNib is firing up? I doubt it will. But simply add a break point to the first statement in the -awakeFromNib method and let me know if that works. – p0lAris Jan 24 '13 at 07:35
  • Another thing — You will need to refer them as self.'' as you haven't declared them as variables; you can but it's of no use really. I actually have a small project that I created on iOS to do the exact same thing you are doing. And it seems to work. If you can upload a link to the mac project, I might be able to look into it. – p0lAris Jan 24 '13 at 07:40
  • Hello omniDETH, thanks for still sticking with it. How can I upload my code? As long as I see it the àwakeFromNib` is firing. The labels get initialized. – JFS Jan 24 '13 at 19:58
  • Upload the project to dropbox and share? I am really amazed why your code doesn't work? The only think I still suspect are the outlets not properly connecting to this variables. Seeing the project & breaking on different points might help. Up the code and I will see. – p0lAris Jan 24 '13 at 20:06
  • [link](https://www.dropbox.com/sh/r8s8ifydvoc392l/XRC5QqCqDb) :please don't get upset about the name I'm just too desperate sometimes... And don't hesitate to give more hints about my structure! I' still learning! – JFS Jan 24 '13 at 20:54
  • I am afraid I cannot find those lines of code where you set the double value. Check your code and let me know where they are. – p0lAris Jan 24 '13 at 23:39
0

I hope this will work, but do:

.h

@property (nonatomic, strong) IBOutlet UILabel *averageValueLabel;

.m

-(void)calculateResults {
     //your stuff

     averageValueLabel.text = [NSString stringWithFormat:@"%g", resultAverageLabel];
}
Nate Lee
  • 2,842
  • 1
  • 24
  • 30
  • Hello nlee918. I'm working on an osx app not iOS but I did try your suggestion: `_outputResultAverageValue.stringValue = [NSString stringWithFormat: @"%g",resultAverageValue];`. I can't find `NSLabel` in the osx version but used `NSString`. Unfortunately it still doesn't work. But it is compiling without errors. So, it is still an interesting approach. – JFS Jan 23 '13 at 23:17
  • @JFS Oh, sorry, I did not know that you were using OSX to program. Unfortunately I am not familiar with OSX programming even though they use the same language. – Nate Lee Jan 24 '13 at 00:04
  • Absolutely no problem. The adapted code is not buggy and still interesting. Thanks! – JFS Jan 24 '13 at 00:08
  • 1
    @nlee918 All the Foundation stuff is almost the same in iOS and OSX, e.g. things like NSString, NSNumber, NSDictionary, NSData, etc. Most iOS code will compile on OSX and the other way round, with one big exception: UI. The UI frameworks are completely different. iOS uses UIKit and OS X uses AppKit (Note: Some people say Cocoa, but that is wrong, Cocoa is a meta framework that consists of Foundation and AppKit). So basically every class whose name starts with UI does not exist on OS X (though many classes have very similar equivalents: UIButton <=> NSButton for example) – Mecki Jan 24 '13 at 00:10
  • @Mecki Oh thats what the difference is? Thanks for posting this +1 – Nate Lee Jan 24 '13 at 00:20
0

I finally found my problem. I bound the outlets with the ResultWindowController object in the xib. I changed the binding to the File's Owner and it works now.

JFS
  • 2,992
  • 3
  • 37
  • 48