请帮助,我需要做些什么来保留数组'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
}
答案 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