IOS5如何保留数组中的对象

时间:2013-01-07 12:56:11

标签: ios5 uiviewcontroller

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

实际上我的目标是在Flip 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
}

1 个答案:

答案 0 :(得分:0)

try2FlipsideViewController中,您致电getMain,创建try2MainViewController班级的全新实例;一个尚未调用doMyAction并且尚未设置resultArray的文件。

也许您应该向名为NSString的{​​{1}}添加try2FlipsideViewController属性,并在resultDisplayText中的prepareForSegue方法中设置属性:

try2MainViewController

加载- (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