当我的UIViewController从IBAction为访问我的AppDelegate一个NSArray崩溃程序
-
25-09-2019 - |
题
我有几个UIViewControllers,我试图访问我的AppDelegate里的数组。当我使用一个IBAction为的UIButton并在这方法我访问我的AppDelegate我的程序模默默。在输出或调试无关,它只是停止。如果我运行几次我可以看到,它是无法正确访问阵列。
要探讨这个问题,我创建了一个非常基本的应用程式。
在我的AppDelegate.h我声明和用于阵列组性质
#import <UIKit/UIKit.h>
@class MyViewController;
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyViewController *viewController;
NSArray *images;
}
@property (nonatomic, retain) NSArray *images;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MyViewController *viewController;`
在AppDelegate.m我合成和初始化的NSArray(还确保图像被添加到资源文件夹)。
@synthesize images;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
images = [NSArray arrayWithObjects:
[[NSBundle mainBundle] pathForResource:@"bamboo_nw" ofType:@"jpg"],
.....
nil];
NSLog(@"init images size:%i",[images count]);
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
在我的UIViewController.h我添加类,导入头文件,声明,并且设置属性为我的AppDelegate指针。
#import <UIKit/UIKit.h>
#import "MyAppDelegate.h"
@class MyAppDelegate;
@interface MyViewController : UIViewController {
MyAppDelegate * mainDelegate; IBOutlet中的UIButton myButton的; } @属性(非原子,保留)MyAppDelegate mainDelegate; @属性(非原子,保留)的UIButton * myButton的; - (IBAction为)doSomething的;`
在我的UIViewController.m我合成和分配我的AppDelegate。我建立一个IBAction为将从AppDelegate中日志的NSArray的相同的计数。
#import "MyViewController.h"
#import "MyAppDelegate.h"
@implementation MyViewController
@synthesize mybutton;
- (void)viewDidLoad {
mainDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"vdl images size:%i",[mainDelegate.images count]);
[super viewDidLoad];
}
-(IBAction) doSomething {
的NSLog(@ “DS图像大小:%I”,[mainDelegate.images计数]); }
我打印的NSArray在AppDelegate中的大小,当我创建它,在ViewController中,当我第一分配我的AppDelegate指针,然后作为我IBAction为的结果。
我发现,每次我按下按钮程序模具。在第三次我打的按钮,我看到它跑我IBAction为印刷,但我的数组大小为1,而不是8.我失去的东西吗?另外,为什么我没有拿到堆栈跟踪或任何东西,调试器只是默默的死去?
在此先感谢您的帮助!
调试器控制台输出3个运行:
[Session started at 2010-05-10 06:21:32 -0700.]
2010-05-10 06:21:44.865 My[59695:207] init images size:8
2010-05-10 06:21:47.246 My[59695:207] vdl images size:8
[Session started at 2010-05-10 06:22:15 -0700.]
2010-05-10 06:22:18.920 My[59704:207] init images size:8
2010-05-10 06:22:19.043 My[59704:207] vdl images size:8
[Session started at 2010-05-10 06:22:23 -0700.]
2010-05-10 06:22:25.966 My[59707:207] init images size:8
2010-05-10 06:22:26.017 My[59707:207] vdl images size:8
2010-05-10 06:22:27.814 My[59707:207] ds images size:1
解决方案
您需要使用self.images
作为application:DidFinishLaunchingWithOptions:
你的左值,即:
@synthesize images;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions(NSDictionary *)launchOptions {
self.images = [NSArray arrayWithObjects:
[[NSBundle mainBundle] pathForResource:@"bamboo_nw" ofType:@"jpg"],
.....
nil];
NSLog(@"init images size:%i",[images count]);
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
当使用的ivar images
而不self
,你正在放弃自动保持您设置在创建@property
优度。