0

请帮助,我需要做些什么来保留我的数组'resultArray'中的对象?我使用了“保留”,但没有帮助。在“doMyAction”中访问该数组时有对象,然后我执行“performSegueWithIdentifier”,它连接到 try2FlipsideViewController,在其中我尝试访问第一个控制器中定义的“setText”方法,并且该数组现在没有对象。

实际上我的目标是在翻转控制器中显示一些在主控制器中确定的错误消息。我还尝试将字符串推送为“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
}
4

1 回答 1

0

try2FlipsideViewController你的调用getMain中,它创建了一个全新的try2MainViewController类实例;一个doMyAction没有被调用resultArray也没有被设置。

也许您应该向被调用的方法中添加一个NSString属性,并在设置属性的方法中:try2FlipsideViewControllerresultDisplayTextprepareForSeguetry2MainViewController

- (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
}
于 2013-01-07T13:35:32.877 回答