当我的 UIViewController 从 IBAction 访问 AppDelegate 中的 NSArray 时,程序会崩溃
我有几个 UIViewController,我正在尝试访问 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(还确保将图像添加到 Resources 文件夹中)。
@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; } @property(非原子,保留)MyAppDelegate mainDelegate; @property(非原子,保留)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 count]); 当我创建 AppDelegate 时,我会
在 AppDelegate 中打印 NSArray 的大小;当我第一次分配 AppDelegate 指针时,我会在 ViewController 中打印 NSArray 的大小,然后作为 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
I have a couple UIViewControllers that I am trying to access an array inside my AppDelegate. When I use an IBAction UIButton and in that method I access my AppDelegate my program dies silently. Nothing in output or the debugger, it just stops. If I run it several times I can see that it is failing to access the array properly.
To investigate this problem I created a very basic app.
In my AppDelegate.h I declared and set properties for the array
#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;`
In the AppDelegate.m I synthesised and initialized the NSArray (Also made sure the images were added to the Resources folder).
@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;
}
In my UIViewController.h I added class, imported header file, declared, and set properties for my AppDelegate pointer.
#import <UIKit/UIKit.h>
#import "MyAppDelegate.h"
@class MyAppDelegate;
@interface MyViewController : UIViewController {
MyAppDelegate *mainDelegate;
IBOutlet UIButton mybutton;
}
@property (nonatomic, retain) MyAppDelegate mainDelegate;
@property (nonatomic, retain) UIButton *mybutton;
-(IBAction) doSomething;`
In my UIViewController.m I synthesize and assign my AppDelegate. I set up an IBAction that will log the same count of the NSArray from the AppDelegate.
#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 images size:%i",[mainDelegate.images count]);
}
I print the size of the NSArray in the AppDelegate when I create it, in the ViewController when I first assign my AppDelegate pointer, and then as a result of my IBAction.
I find that everytime I hit the button the program dies. On the third time I hit the button, I saw that it ran my IBAction but printed my array size as 1 instead of 8. Am I missing something? Also, why don't I get stack traces or anything, the debugger just dies silently?
Thanks in advance for any help!
Debugger Console output for 3 runs:
[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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在
application:DidFinishLaunchingWithOptions:
中使用self.images
作为左值,即:当您使用 ivar
images< /code> 没有
self
,您就放弃了创建@property
时设置的自动保留的优点。You need to use
self.images
as your lvalue inapplication:DidFinishLaunchingWithOptions:
, to wit:When you use the ivar
images
without theself
, you are giving up the goodness of the automatic retention that you set up when you created the@property
.