0
votes

the way to call a method has driven me really crazy! and unfortunately I couldn't find it in the questions already asked. I know how to alloc and init but the thing is in my program I need to call the method inside its own class here is a sample implementation, of course I know I could do this in dozen simpler ways but in the program I develop I have a much complex case that I need to do in this way

in my NIB file I have a label, and two buttons. the label and one of the buttons (called "set to today") are linked in appDelegate the other button (called "set to ten days later") is linked to classTwo here are the codes for all the files although the code doesn't produce any error, but triggering the second button has no effect what is the proper way to call that method.

AppDelegate.h:

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    IBOutlet NSTextField *label;
    NSDate *toDay;
}
-(IBAction)setToToday:(id)sender;
-(void)updateLabelText;
-(void)setToTenDays;

@property (assign) IBOutlet NSWindow *window;
@end

AppDelegate.m:

#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
-(IBAction)setToToday:(id)sender
{
    toDay=[NSDate date];
    [self updateLabelText]; 
}
-(void)updateLabelText
{
    [label setStringValue:[NSString stringWithFormat:@"%@",toDay]];
}
-(void)setToTenDays
{
    int secondsPerDay=60*60*24;
    toDay = [NSDate dateWithTimeInterval:10*secondsPerDay sinceDate:toDay];
}
@end

ClassTwo.h

@interface ClassTwo : NSObject
-(IBAction)setToTenDaysLater:(id)sender;
@end

ClassTwo.m

#import "ClassTwo.h"
#import "AppDelegate.h"
@implementation ClassTwo
-(IBAction)setToTenDaysLater:(id)sender
{
    AppDelegate *classOne=[[AppDelegate alloc] init];
    [classOne setToTenDays];
    [classOne updateLabelText];
}
@end

Why I can't get the label set to new date? both of the objects are in NIB file and actions and outlets are set Thank you in advance for you kind helps.

P.S. in my real experience , for example I am using a trackpad event in CustomWindow Class to change the label.

2

2 Answers

3
votes

Because in class two, you're creating a new instance of AppDelegate, not calling the already-created one. You're only supposed to have one NSApplicationDelegate anyway.

You need to set up a reference to ClassOne from ClassTwo in the header file and connect them in the NIB, or set it programmatically, if you're creating ClassTwo programmatically.

Edit: I've added property accessors, make sure to read up on declared properties.

@class AppDelegate;

@interface ClassTwo : NSObject {
   AppDelegate *delegate;
}

@property (assign, nonatomic) IBOutlet AppDelegate *delegate;

- (IBAction)setToTenDaysLater:(id)sender;

@end

ClassTwo.m

#import "ClassTwo.h"
#import "AppDelegate.h"

@implementation ClassTwo

@synthesize delegate;

-(IBAction)setToTenDaysLater:(id)sender {

    [delegate setToTenDays];
    [delegate updateLabelText];
}
@end

Then in ClassOne (AppDelegate) You could just set the delegate property like so:

classTwo.delegate = self;

Where classTwo is an IBOutlet to an archived instance in your XIB, or you could create it in code as well.

0
votes
DisplayView *year = [[DisplayView alloc] initWithNibName:DisplayView bundle:nil];
[self presentModalViewController:year animated:YES];