从另一个类设置 BOOL 属性

发布于 2024-12-02 03:28:23 字数 672 浏览 0 评论 0原文

我可以知道如何将 BOOL 变量从一个类共享到另一个类吗?

这就是我正在尝试做的事情。我正在 SecondVC 处设置 FirstVC 的 BOOL 变量。当我尝试更改 BOOL 值时应用程序崩溃时,我在这里做错了什么。没有错误日志。

FirstVC.h

@interface FirstVC : UIViewController <UITextFieldDelegate>{
    BOOL pickUpAddressIsSet;

}

@property (nonatomic) BOOL pickUpAddressIsSet;
@end

FirstVC.m

@synthesize pickUpAddressIsSet;

secondaryVC.m

NSArray *viewControllers = [self.navigationController viewControllers];
FirstVC *firstVC = (FirstVC *)[viewControllers objectAtIndex:viewControllers.count - 2];
firstVC.pickUpAddressIsSet = YES; // tried TRUE & 1

May I know how do I share a BOOL variable from one class to another class?

Here's what I am trying to do. I am setting the BOOL variable of FirstVC at SecondVC. What am I doing wrong here as the app crashes when I tried to change the BOOL value. With no error log.

FirstVC.h

@interface FirstVC : UIViewController <UITextFieldDelegate>{
    BOOL pickUpAddressIsSet;

}

@property (nonatomic) BOOL pickUpAddressIsSet;
@end

FirstVC.m

@synthesize pickUpAddressIsSet;

secondVC.m

NSArray *viewControllers = [self.navigationController viewControllers];
FirstVC *firstVC = (FirstVC *)[viewControllers objectAtIndex:viewControllers.count - 2];
firstVC.pickUpAddressIsSet = YES; // tried TRUE & 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

七月上 2024-12-09 03:28:23

试试这个:

@property(nonatomic, assign, getter= isPickUpAddressIsSet) BOOL pickUpAddressIsSet;

它对我有用

Try this:

@property(nonatomic, assign, getter= isPickUpAddressIsSet) BOOL pickUpAddressIsSet;

It works for me

大海や 2024-12-09 03:28:23

看起来 firstVC 实例实际上并不是 FirstVC 类型(类),这可以解释该错误。

尝试以下操作:

NSArray *viewControllers = [self.navigationController viewControllers];
id firstVC = [viewControllers objectAtIndex:viewControllers.count - 2];
NSLog(@"%@", [firstVC class]);

您在 NSLog 输出中看到什么类型的类?

It seems like the firstVC instance isn't actually of the type (class) FirstVC, that would explain the error.

Try the following:

NSArray *viewControllers = [self.navigationController viewControllers];
id firstVC = [viewControllers objectAtIndex:viewControllers.count - 2];
NSLog(@"%@", [firstVC class]);

What kind of class do you see in the NSLog output?

永不分离 2024-12-09 03:28:23
firstVC.pickUpAddressIsSet: = YES;

应该是

firstVC.pickUpAddressIsSet = YES;

冒号不应该在那里。

firstVC.pickUpAddressIsSet: = YES;

Should be

firstVC.pickUpAddressIsSet = YES;

The colon should not be there.

伴随着你 2024-12-09 03:28:23

我发现这是因为我试图在代码的其他部分将 BOOL 变量记录为字符串。

I found out that it's because I am trying to NSlog the BOOL variable as a string at other parts of the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文