2012-07-04 36 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方法,否则它会泄漏,因为我们保留它。如果我知道这对你有帮助,我感觉很好。请享用。