简体   繁体   English

目标C-在代码中创建的对象在切换视图控制器后消失

[英]Objective C - Objects created in code disappear after switching view controllers

I have a simple app with two views in the storyboard, with two segues connecting them together. 我有一个简单的应用,在情节提要中有两个视图,两个segue将它们连接在一起。 in the viewDidLoad of each controller I programmatically create one button, and the action for that button is to switch to the other view. 在每个控制器的viewDidLoad中,我以编程方式创建一个按钮,该按钮的操作是切换到另一视图。 The main code for one of the view controllers is shown here The code for the second one is identical except for the view controller and button names. 这里显示了其中一个视图控制器的主要代码。除了视图控制器和按钮名称之外,第二个视图的代码相同。

UIButton*           button;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    if (!button) {
        button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(0, 0, 50, 20);
        button.center = CGPointMake(200, 200);
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button setTitle:@"View 2" forState:UIControlStateNormal];
        [button setBackgroundColor: [UIColor blueColor]];
        [button setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
        button.tag = 7;
        [button addTarget:self action:@selector(view2Action:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    } else {
        NSLog(@"View 1 already created. Button Tag: %d", button.tag);
    }
}

- (void)view2Action:(id)sender {
    [self performSegueWithIdentifier:@"Segue2" sender:self];
}

@end

The first view appears correctly creates the button and shows it. 出现的第一个视图正确创建了按钮并显示它。 Clicking the button takes me to the second view, and the corresponding button there is also created and shown. 单击该按钮可将我带到第二个视图,并且还将创建并显示相应的按钮。 And when I click this button, I do go back to the first view. 当我单击此按钮时,确实返回到第一视图。 But, the problem is now the button in the first view is not visible anymore. 但是,问题是现在第一个视图中的按钮不再可见。 The code "if (!button)" recognizes the existence of the button, and prints the message "View 1 already created. Button Tag: 7". 代码“ if(!button)”将识别按钮的存在,并显示消息“ View 1 has created。Button Tag:7”。 It seems that all the button properties are there except its graphic. 似乎所有按钮属性都在那里,除了它的图形。

One point, if instead of creating button in code I place the button provided in the storyboard, this problem does not occur. 一点,如果不是在代码中创建按钮,而是将我提供的按钮放置在情节提要中,则不会发生此问题。 What do I need to make the button stay displayed? 我需要什么来使按钮保持显示状态?

Any help will be appreciated. 任何帮助将不胜感激。

Behzad 贝扎德

Your UIButton* button; 您的UIButton* button; variable declaration is in the wrong place. 变量声明在错误的位置。 It's not syntactically illegal, but it's in the global scope of the file and of anything that imports that file. 从语法上讲,这不是非法的,但是它在文件的全局范围内以及任何导入该文件的范围内。 It's not technically an instance variable, as it is not within the scope of the class (neither in its @interface nor in its @implementation ). 从技术上讲,它不是实例变量,因为它不在类的范围内(既不在其@interface也不在其@implementation )。 It should be declared as either an instance variable or a property. 应该将其声明为实例变量或属性。

Without seeing more of your code, I can't entirely explain the behaviour you're seeing. 没有看到更多的代码,我无法完全解释您看到的行为。 As a global variable it should still remain available. 作为全局变量,它应该仍然可用。 However, perhaps your two UIViewController classes are both accessing the same global variable, and overwriting the button properties, and the second one has stolen it and added it to it's view (removing it from the view of the first view controller). 但是,也许您的两个UIViewController类都正在访问相同的全局变量,并且覆盖了按钮属性,而第二个已窃取并将其添加到视图中(将其从第一个视图控制器的视图中删除)。 You could test this by logging it's memory address and see if it is the same in both instances, or by testing its superview property and comparing that with the view of both your view controllers. 您可以通过记录它的内存地址并在两个实例中查看其是否相同来进行测试,或者通过测试其superview属性并将其与两个视图控制器的视图进行比较来进行测试。

To declare it as an instance variable or as a property, it should be within either the @interface / @end section or the @implementation / @end section. 要将其声明为实例变量或属性,应位于@interface / @end节或@implementation / @end节中。 If declaring it as a property, then it should then be preceded by @property (...) . 如果将其声明为属性,则应在其前面加上@property (...) Eg: 例如:

@implementation ViewController

@property (nonatomic, strong) UIButton *button;

- (void)viewDidLoad {
    [super viewDidLoad];
    ...
    ...

Note that this is normally done in the @interface , but it is permissible in the @implementation . 注意,这通常是在@interface完成的,但在@implementation是允许的。

(This answer is based largely on the comments from both @Gellert_Lee and yourself.) (此答案主要基于@Gellert_Lee和您自己的评论。)

Problem resolved after multiple tries at different things. 在多次尝试不同的事物后问题得以解决。 And Thanks to help from Gellert Lee, and Son of the Beach. 还要感谢Gellert Lee和海滩之子的帮助。 If you need to know how, please read through the comments in the answer above. 如果您需要了解方法,请仔细阅读以上答案中的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM