我的问题是从控制器(恰好是我的 rootViewController)获取信息到另一个视图。在尝试通过应用程序委托访问它时,我无法让它工作。我已经找到了如何做到这一点,但这反过来又产生了另一个问题,即在模态视图控制器中获取视图以实际显示数据。下面我发布了 appDelegate 信息和 NSMutable Dictionary 解决方案代码,供那些可能也需要帮助的人使用。
我已经尝试了一个多星期来自己解决这个问题。我的问题最终是如何访问 appDelegate,这就是我遇到 NSDictionary 问题的原因。所以最终问题不是 NSDictionary,尽管我敢肯定,如果我走得更远,那将是一个问题。
首先,我要感谢 TechZen 帮助我看到我已经过度编程,并为我指明了正确的方向。
这是我学到的。
在 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] ;
}
在 ViewController.m 中将变量分配给 appDelegate。我在 tableView 函数 didSelectRowAtIndexPath 中执行了此操作。
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// The line below loads it into the variable, the one above takes it and makes it available for other view Controllers.
Maker *maker = (Maker *)[appDelegate.makers objectAtIndex:indexPath.row];
// the combination of the above and below also loads the information into another array in this view controller, defined in an NSObject Controller called Maker (.h and .m files)
maker = [self.Makers objectAtIndex:indexPath.row];
现在在您的视图控制器中,您想从 appDelegate 加载变量,像这样设置它。
#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
在 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
.....并将其分配给您现在想要的任何东西!
我希望这对每个人都有帮助。此时您可以将 NSArray 从那里放到 NSDictionary 中,但是现在使用键和值进行访问,因此此时访问有点复杂,但当然有优势。我还不能完全把它弄下来,现在已经放弃了这种方法,只使用一个 NSArray 。
下面是 Makers h 和 m 文件的示例,您也可以查看。
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
及其 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
我希望这可以帮助其他人弄清楚这一点,因为这确实花费了我很多时间,我希望你能从我学到的东西中有所收获。
真诚的,柯克