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调用viewController中的函数,但是使用以下代码,它不会被调用。我究竟做错了什么?
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)
我想补充一点,所有属性都已合成,我正在appDelegate中正确调用该方法(我使用NSLogs检查)
2 个解决方案
#1
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.
问题可能是您尝试在尚未创建的对象上设置值。我还提供了一个if语句来防止多次创建对象。
#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.
看起来好像在创建对象之前在对象上调用方法。在你有alloc / init对象之前,没有标签来设置文本。
#1
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.
问题可能是您尝试在尚未创建的对象上设置值。我还提供了一个if语句来防止多次创建对象。
#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.
看起来好像在创建对象之前在对象上调用方法。在你有alloc / init对象之前,没有标签来设置文本。