2013-01-07 43 views
0

請幫助,我需要做些什麼來保留我的數組'resultArray'中的對象?我用「保留」,但沒有幫助。當在'doMyAction'中訪問數組時,數組具有對象,然後執行「performSegueWithIdentifier」,它連接到try2FlipsideViewController,我嘗試訪問第一個控制器中定義的「setText」方法,並且該數組現在沒有對象。IOS5如何保留陣列中的對象

其實我的目標是在contoler一些錯誤信息在主控制器確定的翻轉顯示。我也嘗試推動字符串爲「homeController.resultDisplay.text = self.myTextField.text;」但它編譯並沒有任何反應。

任何幫助或建議將不勝感激,謝謝。

try2MainViewController.m

#import "try2MainViewController.h" 
#import "try2Model.h" 
@interface try2MainViewController() 
@property (nonatomic,strong) try2Model * brain; 
@property (retain, nonatomic) NSMutableArray * resultArray; 
@end 

@implementation try2MainViewController 
@synthesize myTextField; 
@synthesize brain = _brain; 
@synthesize resultArray; 

- (IBAction)doMyAction:(id)sender { 

resultArray = [self.brain someMethod]; 

NSString * resultCode = (NSString *)[resultArray objectAtIndex:0]; 


if ([resultCode isEqualToString:@"1"]) { 

    NSNumberFormatter * myFormat = [[NSNumberFormatter alloc]init]; 
    [myFormat setPositiveFormat:@"0.##"]; 
    NSNumber * testNo = (NSNumber *)[resultArray objectAtIndex:1]; 
    NSString * anca = [myFormat stringFromNumber:testNo]; 
    self.myTextField.text = [NSString stringWithFormat: @"<< result %@%%",anca]; 

} 

NSString * r = (NSString *)[resultArray objectAtIndex:1];//*&* test 

[self performSegueWithIdentifier: @"showAlternate" 
          sender: self]; 

} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"showAlternate"]) { 

     try2FlipsideViewController * homeController = (try2FlipsideViewController *)[segue destinationViewController]; 

    homeController.resultDisplay.text = self.myTextField.text; 


    [[segue destinationViewController] setDelegate:self]; 
    } 
} 

- (NSString *) setText { 
    NSString * r = (NSString *)[resultArray objectAtIndex:1];//*&* test 
    return r; 
} 

try2FlipsideViewController.m

#import "try2FlipsideViewController.h" 
#import "try2MainViewController.h" 

@interface try2FlipsideViewController() 
@property (nonatomic,strong) try2MainViewController * getMain; 

@end 
@implementation try2FlipsideViewController 
@synthesize delegate = _delegate; 
@synthesize resultDisplay = _resultDisplay; 
@synthesize getMain = _getMain; 

-(try2MainViewController *)getMain 
{ 
    if (!_getMain) { 
     _getMain = [[try2MainViewController alloc] init]; 
    } 
    return _getMain; 
} 


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

    r = [self.getMain setText]; 
    self.resultDisplay.text = r 
} 
+0

在你的'doMyAction'方法,而不是'resultArray = ...'您是否嘗試過使用'self.resultArray = ...'?因爲否則你繞過可能導致你的問題的setter。 –

+0

謝謝,加入'自我'。到'resultArray'沒有幫助。 –

回答

0

在你try2FlipsideViewController你叫getMain它創建了一個品牌try2MainViewController新的類實例;其中doMyAction尚未被調用,resultArray尚未設置。

也許你應該在NSString屬性添加到try2FlipsideViewController稱爲resultDisplayText並在try2MainViewControllerprepareForSegue方法設置屬性:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"showAlternate"]) { 
     try2FlipsideViewController * homeController = (try2FlipsideViewController *)[segue destinationViewController]; 
     homeController.resultDisplayText = [self setText]; // <-- here it is 
     [[segue destinationViewController] setDelegate:self]; 
    } 
} 

try2FlipsideViewController負載:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.resultDisplay.text = self.resultDisplayText 
} 
+0

哦,之所以說'homeController.resultDisplay.text = self.myTextField.text;'沒有工作,是因爲當你在prepareForSegue方法中調用它,那就是'resultDisplay'的東西(我猜測'的UILabel '?)可能不存在,因爲視圖尚未加載。 –

+0

謝謝你的回答和解釋。你解決了我的問題,我更好地理解它的工作原理。 –