5

これは私が自分でやろうとしている最初のアプリで、いくつか質問があります。4 つのタブが必要で、「HomeView」という名前の最初のタブで、JSON データを解析しています (これまでのところ完了しています)。

しかし、私が望むのは、他のタブに表示されるように解析されているデータの一部です (再度解析する必要はありません)。

したがって、HomeView の私のコードの一部は次のとおりです。

#import "HomeView.h"

@interface HomeView ()
@end

@implementation HomeView


//other code

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//data fetched
parsed_date=[res objectForKey:@"date"];
 NSLog(@"Date:%@",parsed_date);
[UIAppDelegate.myArray  addObject:parsed_date];
        }

「parsed_date」が正しく出力されていることがわかります。

したがって、この parsed_date が OtherView に表示されるようにします。

これは私のコードですが、印刷できません。

OtherView.m

#import "OtherView.h"
#import "HomeView.h"
#import "AppDelegate.h"

@interface OtherView ()

@end

@implementation OtherView
@synthesize tracks_date;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //preview value of other class
   tracks_date = [UIAppDelegate.myArray objectAtIndex:0];
NSLog(@"Dated previed in OtherView: %@", tracks_date);
}

および (null) が出力されています。

アプリの delegate.h のコードを追加

#import <UIKit/UIKit.h>

#define UIAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) NSArray *myArray;

@end

それで、解決策を提案してもらえますか?

4

4 に答える 4

11

Add the property to your Application Delegate instead.

When assigning the property do something like:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

delegate.myProperty = @"My Value";

then, in your different tabs, you can retrieve this property in the same manner:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *valueInTab = delegate.myProperty; 
于 2012-08-27T15:59:00.513 に答える
2

最後のコード セグメントで HomeView を作成すると、新しいオブジェクト、つまりクラスの新しいインスタンスが作成されます。そのメソッドがクラスのそのインスタンスで実行されない限り、connectionDidFinishLoading からのデータは含まれません。

基本的に、「シングルトン」のラインに沿ったAppDelegateまたは静的ストレージのいずれかで、必要なことを行うにはある種の永続化メカニズムを使用する必要があります。

于 2012-08-27T16:01:18.687 に答える
1

これは、自分自身のインスタンスを作成するために発生しHomeViewます。それは単に何にも関係がありません。
最初の例は、ペン先から作成および初期化されているため機能します。

最善の方法はIBOutlet、InterfaceBuilder で両方の「ビュー」を使用してから接続することだと思います。

@interface OtherView ()
    IBOutlet HomeView *homeView;
@end

@implementation OtherView
@synthesize tracks_date;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"Dated previed in OtherView: %@", homeView.parsed_date);
}

- (void)dealloc:
{
    [homeView release];
}

ここを見てください、それはそれをもっと詳しく示します

InterfaceBuilder では、オブジェクトを管理し、それらを (IBOutlets や IBAction などを介して) 一緒に接続できます。

このビデオは、このコンセプトがどのように機能するかを示す良いデモンストレーションだと思います。

于 2012-08-27T16:03:47.087 に答える
1

While this may not be the best method of doing this, it is easy and effective.

Save your data in your app delegate and retrieve it from there. You can create a shortcut to your app delegate shared application. Then just access the values there.

AppDelegate.h

#define UIAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

@property (nonatomic, strong) NSArray *myArray;

TabView1.m

#import "AppDelegate.h"

SomeObject *myObject = [UIAppDelegate.myArray objectAtIndex:0];

Like I said, it might not be the best way to organize your data for your application, this method does work for small amounts of data needing to be shared at the application level. Hope this helps.

于 2012-08-27T15:59:47.620 に答える