私のUIViewControllerはIBActionから私AppDelegate内にNSArrayにアクセスすると、それはプログラムをクラッシュ

StackOverflow https://stackoverflow.com/questions/2803154

質問

私はAppDelegate内の配列にアクセスしようとしているというカップルのUIViewControllersを持っています。私は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 I合成し、アサイン私AppDelegate。私はAppDelegateからにNSArrayの同じ数を記録しますIBActionを設定します。

#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カウント])。     }

私、私はそれを作成するときに私が最初に私のIBActionの結果として、その後の私AppDelegateポインタを割り当て、ときのViewControllerに、AppDelegate内に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を作成したときに設定することを自動保持の良さを放棄されます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top