我已经尝试让这项工作工作有一段时间了,但没有机会,每次我从一个 View 更改为另一个 View 时, View 的变量和内容都会重置为“默认”,例如,如果在 View 中我更改了标签的文本将字段从默认的“标签”更改为“Hello”,然后更改 View ,一旦我回到同一 View ,文本将再次变为“标签”。
到目前为止,我解决这个问题的唯一方法是设置一个静态字符串,然后将 Label.text 更改为 viewDidLoad 中的字符串。我只知道这不是这样做的方法。我有一种预感,这与我如何从一个 View 转换到另一个 View 有关(分配和启动等)
我目前的过渡方式:
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 再次设置所有值,我只是不认为这是一种好的编程风格。
最佳答案
您对问题所在的怀疑是正确的。尽管您调用了方法 returnToHome:
,但您并没有真正返回任何地方,而是将 ViewController
的新副本堆叠在您已有的内容之上。
要真正返回,presentViewController:
的反面是 dismissViewControllerAnimated:
。当按下按钮返回时,尝试在 MainGameDisplay
类中使用它。
关于objective-c - View 更改后 View 属性重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13368254/