简体   繁体   English

NSMutableArray与UIView iPhone

[英]NSMutableArray Vs UIView iphone

I have an NSMutableArray in my MainViewController classes.. I added 10 Objects to NSMutableArray.When i Click subview button in my MainViewController classes, I push SubviewController (UIViewController)classes.... In subviewcontroller classes i want to use MainViewController Values of NSMutableArray. 我的MainViewController类中有一个NSMutableArray。我向NSMutableArray中添加了10个对象。当我在MainViewController类中单击subview按钮时,我按了SubviewController(UIViewController)类。...在subviewcontroller类中,我想使用NSMutableArray的MainViewController值。 And also i Want to add or remove objects from that NSMutableArray... 我也想从该NSMutableArray添加或删除对象...

My aim is I want to use the NSMutableArray Values to all UIViewController classes to my project with an same name of the variable. 我的目标是我要使用变量名称相同的NSMutableArray值对我项目的所有UIViewController类使用。

If I understand you right - you want to access the NSMutableArray in MainViewController from all your (subview) controllers? 如果我理解正确,那么您想从所有(子视图)控制器访问MainViewController中的NSMutableArray吗?

You can store the handle to your MainViewController in all you subviews and access the array using that handle. 您可以将句柄存储到MainViewController中的所有子视图中,并使用该句柄访问数组。

I think better idea would be move the data to some globally accessible instance (eg to ApplicationDelegate class or wrap it into singleton class) 我认为更好的主意是将数据移至某些可全局访问的实例(例如,移至ApplicationDelegate类或将其包装为singleton类)

What you want to do is add a Cocoa property in your main view controller that references the NSMutableArray instance which you instantiate and populate with elements. 您要做的是在主视图控制器中添加一个Cocoa属性 ,该属性引用您实例化并使用元素填充的NSMutableArray实例。

In the header: 在标题中:

@interface MainViewController : UIViewController {
  NSMutableArray *myMutableArray;
}

@property (nonatomic, retain) NSMutableArray *myMutableArray;

@end

In the implementation, add the @synthesize directive and remember to release the array in -dealloc : 在实现中,添加@synthesize指令,并记住在-dealloc release该数组:

@implementation MainViewController

@synthesize myMutableArray;

...

- (void) dealloc {
  [myMutableArray release];
  [super dealloc];
}

@end

You also want to add this property to view controllers that are subordinate to the main view controller, in the exact same way. 您还希望以完全相同的方式将此属性添加到从属于主视图控制器的视图控制器。

In your main view controller, when you are ready to push the subordinate view controller, you set the subordinate view controller's property accordingly: 在主视图控制器中,当您准备按下从属视图控制器时,可以相应地设置从属视图控制器的属性:

- (void) pushSubordinateViewController {
  SubordinateViewController *subVC = [[SubordinateViewController alloc] initWithNibName:@"SubordinateViewController" bundle:nil];
  subVC.myMutableArray = self.myMutableArray; // this sets the sub view controller's mutable array property
  [self.navigationController pushViewController:subVC animated:YES];
  [subVC release];
}

Likewise in your subordinate view controller, it will need to set its subordinate's mutable array property accordingly, when it pushes its own view controller. 同样,在你的下属视图控制器, 需要相应地设置其下属的可变数组属性,当它推出了它自己的看法控制器。

By setting references in this way, each view controller is pointed at the same mutable array, containing the desired elements. 通过以这种方式设置引用,每个视图控制器都指向包含所需元素的同一可变数组。

To use the array, just call self.myMutableArray , eg [self.myMutableArray addObject:object] . 要使用该数组,只需调用self.myMutableArray ,例如[self.myMutableArray addObject:object]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM