2012-08-31 36 views
0

我正在使用.XIB並且沒有ARC。我將NSMultableArray的值傳遞給另一個視圖,如果我把[self presentModel ...],它可以工作,但如果我用一個按鈕調用AnotherView的AnotherView的NSMultableArray的值爲null!iOS將NSMutableArray的值傳遞(保留)到另一個視圖中的另一個NSMutableArray

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的寫 「空」

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; } 
+0

嘗試數據存儲:http://stackoverflow.com/questions/5448476/objective-c-singleton-objects-and-global-variables或http://www.galloway.me.uk/教程/單類/ – kezi

回答

0

這些不應該是必要的:當類初始化

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

屬性已經被初始化。

事實上,你明確的保留行不應該是必要的;你不會按照我所能看到的那樣釋放它們,只是將保留計數設置爲2以便它必須被釋放兩次。

我認爲這裏發生的事情是,方法和視圖控制器沒有按照你想要的順序調用,可能與標籤欄有關。

0

我dicoverd!我正在使用MVC APP代理的數組不會失去它的價值。 AppDelegate NSMutableArray * arrayDelegate; View.m AppDelegate * appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; [appDelegate.arrayDelegatePedido addObject:@「title」]; //例如

相關問題