iOS在另一个视图中将NSMutableArray的值传递(保留)到另一个NSMutableArray

时间:2012-08-31 01:26:39

标签: objective-c ios xcode

我正在使用.XIB而没有ARC。我将NSMultableArray的值传递给另一个视图,如果我把[self presentModel ...],它可以工作,但是如果我用一个按钮调用AnotherView,那么AnotherView的NSMultableArray的值为null!

AnotherView.h

@interface AnotherViewController : UIViewController<UITableViewDataSource,      UITableViewDelegate>{
NSMutableArray *otherAnother;
NSMutableArray *arrayOfTheAnotherView;
}
@property (retain, nonatomic) IBOutlet UITableView *tableView;
@property (retain, nonatomic) NSMutableArray *arrayOfTheAnotherView;

AnotherView.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

otherAnother = [[NSMutableArray alloc]init];
otherAnother = [[NSMutableArray alloc]initWithArray:self.arrayOfTheAnotherView];
//    [otherAnother addObjectsFromArray:arrayOfTheAnotherView]; 
NSLog(@"%@", self.arrayOfTheAnotherView);
}

NSLog写的是“null”

CurrentView.h

@interface CurrentViewController : UIViewController {
NSMutableArray * arrayCurrentView;
AnotherViewController *superAnotherView;
}
@property (retain, nonatomic) AnotherViewController *superAnotherView;

CurrentView.m

@synthesize superAnotherView;
NSString *x = [[NSString alloc]initWithFormat:@"%@",[label text]];

arrayCurrentView = [[NSMutableArray alloc]init];
[arrayCurrentView retain];
[arrayCurrentView addObject:x];

self.superAnotherView = [[AnotherViewController alloc]initWithNibName:nil bundle:nil];
self.superAnotherView.arrayOfTheAnotherView = [[NSMutableArray alloc]init];
[self.superAnotherView.arrayOfTheAnotherView retain];
[self.superAnotherView.arrayOfTheAnotherView addObjectsFromArray:arrayCurrentView];

感谢帮助,我不知道如何保留NSMultableArray的价值。

我称之为AnotherView:

UIButton *buttonAnother = [UIButton buttonWithType:UIButtonTypeCustom]; [buttonAnother  setTag:5]; [buttonAnother addTarget:self action:@selector(switchTabBar:) forControlEvents:UIControlEventTouchDown]; 
[tabBarViewController.view addSubview:buttonAnother];
- (IBAction)switchTabBar:(id)sender { switch ([(UIButton *)sender tag]) { case 5:     [self.tabBarController setSelectedIndex:0]; break; }

2 个答案:

答案 0 :(得分:0)

这些不应该是必要的:

self.superAnotherView.arrayOfTheAnotherView = [[NSMutableArray alloc]init]; 
[self.superAnotherView.arrayOfTheAnotherView retain];

初始化类时已初始化属性。

事实上,你的显式保留线根本不是必需的;你没有尽可能地发布它们,只是将保留计数2放在必须释放两次。

我认为这里有一些东西,方法和视图控制器没有按你想要的顺序调用,可能与标签栏有关。

答案 1 :(得分:0)

我知道了!我正在使用MVC APP委托的数组不会丢失其值。 AppDelegate中     NSMutableArray * arrayDelegate; View.m     AppDelegate * appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];     [appDelegate.arrayDelegatePedido addObject:@“title”]; //例如