我的问题是从控制器(这恰好是我的rootViewController)获取信息到另一个视图。在试图通过应用程序委托访问它时,我无法使其工作。我已经找到了如何做到这一点,但是这又引起了另一个让模态视图控制器中的视图实际显示数据的问题。下面我已经发布了appDelegate信息和NSMutable Dictionary解决方案代码,以供那些可能需要帮助的人员使用。AppDelegate从不同的类访问,从RootController.m
我已经尝试了一个多星期来解决我自己的问题。我的问题最终是如何访问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
and in viewController.m file;
#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
我希望这可以帮助别人得到这个直,因为它确实花了我很多时间,我希望你可以从我的教训得到位。
真诚, 柯克
这是什么问题?你把它编辑出来了吗? – 2009-12-05 04:10:57