我想从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];
}
....
我想补充一点,所有属性都已合成,我正在appDelegate中正确调用该方法(我使用NSLogs检查)
答案 0 :(得分:1)
在AppDelegate.m中:
看起来好像在创建对象之前在对象上调用方法。在你拥有alloc / init对象之前,没有标签可以设置文本。
答案 1 :(得分:0)
尝试将您的方法更改为:
-(void)sendToTransferScreen:(NSArray *)detailsArray {
if (!objDetailsToTransfer) {
objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:@"DetailsToTransfer" bundle:nil];
[objDetailsToTransfer setLabels:detailsArray];
[self.window addSubview:objDetailsToTransfer.view];
}
}
问题可能是您尝试在尚未创建的对象上设置值。我还提供了一个if语句来防止多次创建对象。