Mon problème consiste à obtenir les informations d'un contrôleur (qui se trouve être mon rootViewController) à une autre vue. En essayant d'y accéder via le délégué de l'application, je n'ai pas réussi à le faire fonctionner. J'ai trouvé comment faire ceci, mais cela a créé un autre problème d'obtenir la vue à l'intérieur d'un contrôleur de vue modale pour réellement afficher les données. Ci-dessous, j'ai posté à la fois les informations appDelegate, et un code de solution NSMutable Dictionary pour ceux qui pourraient avoir besoin d'aide aussi.AppDelegate Accès à partir de différentes classes, à partir de RootController.m
J'avais essayé pendant plus d'une semaine pour résoudre ce problème par moi-même. Mon problème a fini par être comment accéder à l'appDelegate, qui était la raison pour laquelle j'avais un problème avec le NSDictionary. Donc, à la fin, le problème n'était pas le NSDictionary, même si j'étais allé plus loin, cela aurait été un problème, j'en suis sûr. Tout d'abord, je tiens à remercier TechZen pour m'avoir aidé à voir que j'étais sur la programmation, et me diriger dans la bonne direction.
Voici ce que j'ai appris.
Affectez votre variable dans appDelegate.
AppDelegate.h
@interface AppDelegate : NSObject < UIApplicationDelegate, UINavigationControllerDelegate >
{
UIWindow *window;
UINavigationController *navController;
// Array to store the Makers Objects
NSMutableArray *makers;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navController;
@property (retain, nonatomic) NSMutableArray *makers;
@end
AppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
makers = [[NSMutableArray alloc] init] ;
}
Dans la ViewController.m assigner la variable à la appDelegate. Je l'ai fait dans une fonction tableView: didSelectRowAtIndexPath. Maintenant, dans votre vue Controller, vous voulez charger la variable à partir de appDelegate, configurez-la comme ceci.
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Maker.h"
@class AppDelegate;
@interface DetailsViewController : UIViewController
{
AppDelegate *dappDelegate;
DetailsViewController *detailsView;
IBOutlet UITextView *makerDescription;
}
@property (retain, nonatomic) AppDelegate *dappDelegate;
@property (nonatomic, retain) DetailsViewController *detailsView;
@property (nonatomic, retain) IBOutlet UITextView *makerDescription;
@end
et dans le fichier viewController.m;
#import "DetailsViewController.h"
#import "AppDelegate.h"
@synthesize dappDelegate;
- (void)viewWillAppear:(BOOL)animated // or ViewDidLoad not sure which is better.
dappDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSString *newLocalVariable = [dappDelegate.makers description];
NSLog(@"newLocalVariable: %@", [dappDelegate.makers description]);
// This is for verifying you have it loaded. 'description' is defined in the Maker NSObject, see below for those files, and above for where it was assigned originally
... et de l'assigner à ce que vous voulez maintenant!
J'espère que cela aide tout le monde. Vous pouvez à ce moment déposer le NSArray à partir de là dans un NSDictionary, mais l'accès est maintenant avec des clés et des valeurs, donc un peu plus compliqué à ce stade pour accéder, mais bien sûr des avantages. Je n'arrive tout simplement pas encore à le comprendre et j'ai abandonné cette méthode pour utiliser un NSArray pour l'instant.
Vous trouverez ci-dessous un exemple de fichier Makers h et m à afficher également.
Maker.h
@interface Maker : NSObject
{
NSString *name;
NSString *address;
NSString *city;
NSString *postalcode;
NSString *country;
NSString *phonenumber;
NSString *email;
NSString *description;
NSString *services;
NSString *website;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *postalcode;
@property (nonatomic, copy) NSString *country;
@property (nonatomic, copy) NSString *phonenumber;
@property (nonatomic, copy) NSString *email;
@property (nonatomic, copy) NSString *description;
@property (nonatomic, copy) NSString *services;
@property (nonatomic, copy) NSString *website;
- (id)initWithName:(NSString *)n address:(NSString *)a city:(NSString *)c postalcode:(NSString *)z country:(NSString *)o phonenumber:(NSString *)p email:(NSString *)e description:(NSString *)d services:(NSString *)s website:(NSString *)w;
@end
et son fichier Maker.m;
#import "ViolinMaker.h"
@implementation Maker
@synthesize name, address, city, postalcode, country, phonenumber, email, description, services, website;
- (id)initWithName:(NSString *)n address:(NSString *)a city:(NSString *)c postalcode:(NSString *)z country:(NSString *)o phonenumber:(NSString *)p email:(NSString *)e description:(NSString *)d services:(NSString *)s website:(NSString *)w;
{
self.name = n;
self.address = a;
self.city = c;
self.postalcode = z;
self.country = o;
self.phonenumber = p;
self.email = e;
self.description = d;
self.services = s;
self.website = w;
return self;
}
@end
J'espère que cela aide les autres à obtenir ce droit, car il m'a vraiment coûté beaucoup de temps et je l'espère, vous pouvez obtenir un peu de ce que j'appris.
Sincèrement, Kirk
Quelle est la question? L'avez-vous édité? –