私の問題は、コントローラー(たまたま私のrootViewController)から別のビューに情報を取得することです。アプリデリゲートを介してアクセスしようとしても、機能させることができませんでした。これを行う方法を見つけましたが、このインターンにより、モーダルビューコントローラー内のビューを実際にデータを表示するという別の問題が発生しました。以下に、appDelegate 情報と、支援が必要な場合に備えて NSMutable Dictionary ソリューション コードの両方を投稿しました。
私はこの問題を自分で解決するために1週間以上試みました. 私の問題は、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 を使用するだけです。
以下は、Maker の 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
私は本当に多くの時間を費やしたので、これが他の人がこれを理解するのに役立つことを願っています.
カーク