2012-04-06 6 views
1

私はテーブルビューとシングルトンクラスを探していますが、ここで私は何も解決策を見つけることができませんでした。シングルトンクラスで配列を使用してビューコントローラ間でデータを渡すIOS?

ユーザーが行を選択したときにviewcontrollerにテーブルビューがあります。選択したデータをシングルトンクラスの配列に送信し、別のviewcontrollerのスクリーンに出力します。 、私は、次の試してみましたシングルトンクラスのArrayにデータを渡すべきか、どのように

#import <Foundation/Foundation.h> 


@interface DataController : NSObject { 
    NSArray* standLoc; 
} 

@property (readonly)NSArray* standLoc; // stand location 

+(DataController*)sharedInstance; 

@end 


#import "DataController.h" 

@implementation DataController 

@synthesize standLoc; 

+(DataController*)sharedInstance 
{ 
    static DataController* sharedInstance = nil; 
    if (!sharedInstance) 
    { 
     sharedInstance = [[DataController alloc]init]; 
    } 
    return sharedInstance; 
} 

@end 

私が選択した見ることができますデバッグで
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath { 

    StartHuntViewController *startHuntController = [[StartHuntViewController alloc] initWithNibName:@"StartHuntView" bundle:nil]; 

    DataController* sharedSingleton = [DataController sharedInstance]; 
    sharedSingleton = [stands objectAtIndex:indexPath.row]; 

    startHuntController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentModalViewController:startHuntController animated:YES];; 


    [startHuntController release]; 
    startHuntController =nil; 
} 

:ここ

は私のシングルトンクラスのコードですitemはsharedSingletonにありますが、どのようにNSArray * standLocに渡すことができますか?

EDIT


私は私のコードを編集した、それが

私のシングルトンの.mと.hの今微FOW複数contorllerビューを作品SO:

#import <Foundation/Foundation.h> 


@interface DataController : NSObject { 
    NSString* standLoc; 
} 

@property (nonatomic,retain)NSString* standLoc; // stand location 

+(DataController*)sharedInstance; 
-(void) setData: (NSString *) data; 

@end 
#import "DataController.h" 


@implementation DataController 


static DataController* sharedInstance = nil; 

@synthesize standLoc; 

+(DataController*)sharedInstance 
{ 

    @synchronized (self) { //this ensure this methods will not be called at the same time.. 
     if(sharedInstance == nil){ 
      [[self alloc] init]; 
     } 
    } 
    return sharedInstance; 
} 
+(id) allocWithZone:(NSZone *)zone{ 
    @synchronized (self){ 
     if (sharedInstance == nil) { 
      sharedInstance = [super allocWithZone:zone]; 
      return sharedInstance; 
     } 
    } 
    return nil; 
} 
-(id)copyWithZone:(NSZone *)zone{ // incase if we want to copy our singleton instance 
    return self; 
} 
//to protect singleton from deallocation, we need to override some functions of memory allocation 
-(id) retain { 
    return self; 
} 

-(id) autorelease{ 
    return self; 
} 
-(NSUInteger) retainCount{ 
    return NSUIntegerMax; 
} 

-(id) init{ // lets set the default data in it 
    @synchronized (self){ 
     [super init]; 
     standLoc = [[NSString alloc] initWithString:@"Stand Loc"];//for performance, as we expect 5 digits from server, it's size was set to another 5 digits.. 
     return self; 
    } 
} 
-(void) setData: (NSString *) data{ // this is the function to set static data which is the member of the class, reaching data will be allowed with this method 
    @synchronized (self){ 
     if (standLoc != data) { 
      [standLoc release]; 
      standLoc = [data retain]; 
     } 
    } 
} 
-(NSString *) standLoc{ 
    @synchronized(self){ 
     return standLoc; 
    } 
} 

へデータをシングルトンに渡す:

DataController* sharedSingleton = [DataController sharedInstance]; 
    NSString* transfer = [stands objectAtIndex:indexPath.row]; 
    [sharedSingleton setData:transfer]; 

答えて

0
  • は読み取り専用standLocをしないでください:あなたのテーブル内のセルをタップすると、テーブルのビューコントローラが新たなディテールコントローラをプッシュできるように、あなたはこのように、ナビゲーションコントローラを使用しているように聞こえます他のクラスから更新したい場合
  • standLocを「スタンド」から1つのオブジェクトに渡す場合、なぜそれを配列にしたいのですか?
  • あなたは(上記の2点をセトリングした後に)したい構文の種類は次のとおりです。

    DataController * sharedSingleton = [DataController sharedInstance];

    sharedSingleton.standLoc = //シングルトン内にあるべきデータ要素が何であっても。

0

なぜシングルトンが必要ですか?あるビューコントローラーから次のビューコントローラーに直接データを渡すだけで、コードの管理が簡単になり、簡単になります。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:nil bundle:nil]; 
    detailViewController.data = [tableData objectAtIndex:[indexPath row]]; 
    [self.navigationController pushViewController:detailController animated:YES]; 
} 
+0

問題は私は複数のviewcontrollersを持っているだけではなく、いずれかのビューからメインビューにデータを渡す場合別のviewcontrollerに移動して再びメインに戻ると、データが失われているのが分かるので、メインviewcontrollerにデータを保持するためのソリューションが必要だったので、シングルトンパターンが答えだと思った –

0

なぜ@ synchronizedを使用していますか?

(これはiOSではほとんど使用されていません) とにかく、(非アトミック)を使用していて、同期していると、ちょっと奇妙なようです。

それではましょうアップルのコンパイラより良いその仕事をする:

プロパティは、デフォルトではデフォルトでアトミックです

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html)、Objective-Cのプロパティ は原子です:

@interface XYZObject :NSObject @property NSObject * implicitAtomicObject; //原子のデフォルト値@property(atomic)NSObject * explicitAtomicObject; // @endこれは、合成されたアクセサによって、 のゲッターメソッドによって常に完全に取得された値、または セッターメソッドによって完全に設定された値であることを保証することを意味します。

最後のノート:アップルはそうでは不可分と、ロックを使用して、言うように、データへの深いアクセスに対して保証するものではありません別のスレッドを形成:

あなたは配列を持っていて、それ(その参照を保護します。 。)をatomic/synchronizedにすると、そのオブジェクトへのアクセスは保護され、オブジェクトは含まれません。 (多くの注意を払って手でロックする必要があります...競合状態が発生する可能性があります...)

関連する問題