gpt4 book ai didi

iphone - 更改 AppDelegate 中 var 的值

转载 作者:行者123 更新时间:2023-12-03 19:36:40 26 4
gpt4 key购买 nike

在我的 AppDelegate 中,我有

#import <UIKit/UIKit.h>
#import "CustomerProfile.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) int x;

@end

在B类,我愿意

AppDelegate *appDelegate        =   [[UIApplication  sharedApplication] delegate];
appDelegate.x = 5;

那么在C类,我就这么做了

 AppDelegate *appDelegate        =   [[UIApplication  sharedApplication] delegate];
appDelegate.x = 4;

最终,在 D 类中,我打印出 x 和 x = 5 的结果。 x 应该是4。这让我很困惑。请就这个问题给我建议。谢谢

最佳答案

在您的应用程序委托(delegate)方法中,您的属性 x 设置为强(又名保留),您必须设置为分配,无法保留 int var,因为它不是对象:

@property (assign, nonatomic, readwrite) int x; //then @synthesize in the implementation

其次,您必须导入 appDelegate 的 header (在您的 B、C、D 类中)

#import "yourAppDelegate.h" 

设置您的 appDelegate 实例:

yourAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate]; // or [NSApplication sharedApplication] if your app it is for OS X

然后将您的 x var 设置为所需的值

appDelegate.x = 5 (or whatever)

我在我的一个项目和作品中对此进行了测试。

关于iphone - 更改 AppDelegate 中 var 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8738011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com