查看视图更改后重置的属性

时间:2012-11-13 20:30:23

标签: objective-c ios xcode

我一直试图让这项工作暂时没有机会,每次我从视图更改为视图时,视图的变量和内容都会重置为“默认”,例如,如果在视图中我更改了标签的文本字段从默认的“标签”到“你好”,然后更改视图,一旦我回到同一个视图,文本将再次成为“标签”。

到目前为止我唯一能解决这个问题的方法是设置一个静态字符串,然后在viewDidLoad中将Label.text更改为string。我只知道这不是这样做的方法。我有一种预感,它与我从视图到视图的转换,(分配和启动等)有关。

目前的转型方式:

FirstView.h:
@interface FirstView : Engine
@property(nonatomic, readwrite) MainGameDisplay *secondView;

FirstView.m:
@implementation FirstView

- (IBAction)StartGame:(id)sender 
{ 
    if (! self.secondView)
    self.secondView = [[MainGameDisplay alloc] initWithNibName:nil bundle:nil];
    [self presentViewController: self.secondView animated:YES completion:NULL];
}

和MainGameDisplay:

MainGameDisplay.h:
@class ViewController;

@interface MainGameDisplay : Engine
@property (strong) ViewController *firstPage;

MainGameDisplay.m:
@implementation MainGameDisplay

- (IBAction)returnToHome:(id)sender {
    if (!self.firstPage)
        self.firstPage = [[ViewController alloc] initWithNibName:nil bundle:nil];
        [self presentViewController: self.firstPage animated:YES completion:NULL];
}

我想要的是不必通过viewDidLoad再次设置所有值,我只是觉得它不是一个好的编程风格。

1 个答案:

答案 0 :(得分:1)

你对于出了什么问题的怀疑是正确的。虽然你打电话给你的方法returnToHome:,但你并没有真正回到任何地方,而是将ViewController的新副本堆叠在已有的方法之上。

要实际返回,presentViewController:的反面是dismissViewControllerAnimated:。当按下按钮返回时,尝试在MainGameDisplay课程中使用它。