2012-07-04 37 views
-1

在我的應用程序,如何從其他視圖更改MutableArray?

有兩種不同的看法ITEMLISTItemSearch

在ItemList文件中我有一個NsMutableArray,名稱爲tblItem。我想從Itemsearch頁面的tblitem中傳遞數據。

我該怎麼做?

+0

如何從ItemSearch導航到ItemList? –

+1

嗯,將NSMutableArray的地址傳遞給另一個視圖? –

+0

[self.navigationController pushViewController:ItemListPage animated:YES]; – Anki

回答

1

您可以使用特性如下:

1,創建於tblItem的ItemList.h一個財產,

@property(nonatomic, retain) NSMutableArray *tblItem; 

然後合成它在ItemList.m,

@synthesize tblItem; 

當您從ItemSearch導航到ItemList時,即當您初始化ItemList時,只需提供tbIItem所需的值,如

ItemListObj.tblItem = theSearchedArray; 
0

這取決於你的需要。你可以使用Singleton類在不同的類之間共享你的變量。定義你想在你的DataClass中共享的所有變量。

在.h文件中(其中RootViewController的是我的數據類,與新類替代名稱).m文件

//make the class singleton:-  
+(RootViewController*)sharedFirstViewController  
{  
@synchronized([RootViewController class]) 
{ 
    if (!_sharedFirstViewController) 
     [[self alloc] init]; 

    return _sharedFirstViewController; 
} 

return nil; 
} 


+(id)alloc 
{ 
@synchronized([RootViewController class]) 
{ 
    NSAssert(_sharedFirstViewController == nil, 
      @"Attempted to allocate a second instance of a singleton."); 
    _sharedFirstViewController = [super alloc]; 
    return _sharedFirstViewController; 
} 
return nil; 
} 

-(id)init { 
self = [super init]; 
if (self != nil) { 
    // initialize stuff here 
} 
return self; 
} 

+(RootViewController*)sharedFirstViewController; 

後,你可以用你的變量在任何其他

類似這樣

[RootViewController sharedFirstViewController].variable 

希望它對你有所幫助:)

1

聲明一個NSMutableArray作爲SecondViewController中的屬性,並在您從FirstViewController推送或呈現SecondViewController時分配數組。

@interface SecondViewController : UIViewController 
{ 
    NSMutableArray *aryFromFirstViewController; 
} 

@property (nonatomic,retain) NSMutableArray *aryFromFirstViewController; 


@end 

在實施,綜合物業

@implementation SecondViewController 

@synthesize aryFromFirstViewController; 

@end 

在FirstViewController的頭導入SecondViewController

#import "SecondViewController.h" 

在實施FirstViewController的,在加上類似下面的代碼您編寫了代碼以呈現或推送SecondViewController

@implementation FirstViewController 


- (void) functionForPushingTheSecondViewController 
{ 
    SecondViewController *objSecondViewController = [[SecondViewController alloc] initWithNIBName: @"SecondViewController" bundle: nil]; 
    objSecondViewController.aryFromFirstViewController = self.myAryToPass; 
    [self.navigationController pushViewController:objSecondViewController animated: YES]; 
    [objSecondViewController release]; 
} 

@end 

請不要忘記發佈aryFromFirstViewControllerdealloc SecondViewController方法,否則它會泄漏,因爲我們保留它。如果我知道這對你有幫助,我感覺很好。請享用。