我的問題是從控制器(這恰好是我的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