gpt4 book ai didi

iphone-sdk-3.0 - 当我的UIViewController从IBAction访问AppDelegate中的NSArray时,程序崩溃

转载 作者:行者123 更新时间:2023-12-03 17:43:47 24 4
gpt4 key购买 nike

我有几个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(还确保将图像添加到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计数]);
}

创建时,我在AppDelegate中打印NSArray的大小;当我第一次分配AppDelegate指针时,然后在IBAction的结果中,在ViewController中打印NSArray的大小。

我发现每次我按一下按钮,程序就会死亡。第三次单击按钮时,我看到它运行了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;
}

当使用不带 images的ivar self时,您将放弃创建 @property时设置的自动保留的好处。

关于iphone-sdk-3.0 - 当我的UIViewController从IBAction访问AppDelegate中的NSArray时,程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2803154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com