0

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)

3
  • Are you trying to call setLabels method ? Commented Oct 7, 2011 at 18:54
  • Have you try to call setLabels after creating the object..you're calling a method on an non-allocated object. Commented Oct 7, 2011 at 19:46
  • ..you should also create the object in this way if you want to retain it: self.objDetailToTransfer=..etc Commented Oct 7, 2011 at 19:53

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

well now it gets to the function in the viewController and its getting the right data being passed in, but the labels aren't being set because when the view loads, theres nothing in the labels... I also NSLogged the values and they return (null)... What am I doing wrong?
When you call setLabels, your program will run through the method but the labels and self are still nil until you create the object. You will see the logging but that's about it. When you alloc and init your object, self will now be an object of DetailsToTransfer class and the labels will be created from the xib.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.