简体   繁体   中英

Appdelegate Variable in storyboard

I used to declare variale in appdelegate and make it as sharable in all the classes (If the variable is global ) .

appDelegate = (StoryAppDelegate*)[[UIApplication sharedApplication]delegate];

this was the code am using normally to access appdelegate variable. Now am trying a story board application and its not working for me . while declaring appdelegate it shows an error "Unknown type name StoryAppDelegate".

StoryAppDelegate*ss; 

this is the code am using .

Any help appreciated .

Just don't use the app delegate. That isn't what it's for.

Instead, create a specific class to own the responsibility + knowledge, make it a singleton and have all classes that require it get it via it's 'sharedController' (or whatever you call it) class method.

Or use a 'constants' file with a static variable or something (just not the app delegate).

Storyboard is used only for design, not changes to the code.

For that, you'd use:

AppDelegate *app;

in the header file of the view's controller.

And in the implementation file,

  app=(AppDelegate *)[[UIApplication sharedApplication]delegate];

Then you can just use app.yourVariable

It seems the case of circular dependency.

use @class StoryAppDelegate;

instead of

#import "StoryAppDelegate.h"

in your header file.

I would recommend using a singleton instance of your global variable as they have bailed me out of your exact situation multiple times. Here is an example I'm currently using to implement a singleton. This methodology is also ARC-safe as well

mySingleton.h

#import <Foundation/Foundation.h>

@interface mySingleton : NSObject {

}
+ (NSMutableDictionary *) myMutableDict;

@end

mySingleton.m

#import "mySingleton.h"

@implementation mySingleton

+ (NSMutableDictionary *)myMutableDict
{
    static NSMutableDictionary *singletonInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singletonInstance = [[NSMutableDictionary alloc]init];

    });
    return singletonInstance;
}

@end

As long as you include mySingleton.h in all of your view controllers you can access the data via [mySingleton myMutableDict] . For example: [[mySingleton myMutableDict] setObject:myObject forKey:myKey]; This will of course work with any object type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM