theNoobProgrammer
theNoobProgrammer

Reputation: 932

Calling a function in viewcontroller from appdelegate

I want to call a function in a viewController from my appDelegate but with the following code, it doesn't get called. What am I doing wrong?

AppDelegate.h:

#import <UIKit/UIKit.h>
#import "DetailsToTransfer.h"

@class AccountDetailTransferViewController;

@interface AccountDetailTransferAppDelegate : NSObject <UIApplicationDelegate> {

    DetailsToTransfer *objDetailsToTransfer;
}

@property (nonatomic, retain) DetailsToTransfer *objDetailsToTransfer;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet AccountDetailTransferViewController *viewController;

-(void)sendToTransferScreen:(NSArray *)detailsArray;

@end

AppDelegate.m:

....
-(void)sendToTransferScreen:(NSArray *)detailsArray {


    [objDetailsToTransfer setLabels:detailsArray];

    objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:@"DetailsToTransfer" bundle:nil];

    [self.window addSubview:objDetailsToTransfer.view];

}

DetailsToTransfer.h:

#import <UIKit/UIKit.h>

@interface DetailsToTransfer : UIViewController {
    NSArray *accountDetailsArray;
    UILabel *nameLabel;
    UILabel *accountLabel;
    IBOutlet UIButton *btnTransfer;
    IBOutlet UIButton *btnBack;
}

@property (nonatomic, retain) NSArray *accountDetailsArray;
@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UILabel *accountLabel;

-(IBAction)sendDetails;
-(IBAction)goBack;
-(void)setLabels:(NSArray *)array;

@end

DetailsToTransfer.m

....
-(void)setLabels:(NSArray *)array {

    NSLog(@"Setting Labels");

    self.nameLabel.text = [array objectAtIndex:0];
    self.accountLabel.text = [array objectAtIndex:1];
}
....

I would like to add that all the properties have been synthesized and that I'm calling the method in the appDelegate properly (i checked using NSLogs)

Upvotes: 0

Views: 1087

Answers (2)

ms83
ms83

Reputation: 1814

Try changing your method to this:

-(void)sendToTransferScreen:(NSArray *)detailsArray {

     if (!objDetailsToTransfer) {

         objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:@"DetailsToTransfer" bundle:nil];

         [objDetailsToTransfer setLabels:detailsArray];

         [self.window addSubview:objDetailsToTransfer.view];

    }
}

The problem might be that you are trying to set the values on a object that hasn't been created yet. I also provided an if statement to prevent the object from being created multiple times.

Upvotes: 0

Christopher Moore
Christopher Moore

Reputation: 149

In AppDelegate.m:

Looks as if you are calling a method on your object before the object has been created. Until you have alloc / init your object, there will be no labels to set text.

Upvotes: 1

Related Questions