【问题标题】:how to pass in BOOL value from one view to another in objective c如何在目标c中将BOOL值从一个视图传递到另一个视图
【发布时间】:2012-08-28 14:34:31
【问题描述】:

我目前正在开发一个应用程序,如果用户单击一个按钮,它会将 buttonPressed 设置为“YES”并转到一个新视图。在这个新视图中,您可以选择颜色,一旦选择了颜色,它将返回上一个视图,并在单击的按钮的背景上显示其选择的颜色。

我在运行代码时无法访问布尔值并收到 SIGBART 错误。 我在这里做错了什么? 在我的 ChangeClothesViewController 中

@property (assign, readwrite) BOOL pantsPressedBool;
@synthesize pantsPressedBool = _pantsPressedBool;

- (IBAction)pantsPressed:(UIButton *)sender {
ChooseColorsViewController *color = [[ChooseColorsViewController alloc] initWithNibName:@"ChooseColorsViewController" bundle:nil];
[self.navigationController pushViewController:color animated:YES];
//AppDelegate *passColors = (AppDelegate *)[[UIApplication sharedApplication]delegate];
//this code above does nothing and is not even needed. I was testing something out and forgot to take it out

_pantsPressedBool = YES;
}

在我的 RectangleView 内部(这是我在按钮后面制作一个矩形来为按钮制作背景的地方)

- (void)drawRect:(CGRect)rect
{
//custom delegate
AppDelegate *passColors = (AppDelegate *)[[UIApplication sharedApplication]delegate];
changeClothes *whichButton = (changeClothes *)[[UIApplication sharedApplication]delegate];

for (int i=0; i < [passColors.getColors count]; i++) {
    if ([[passColors.getColors objectAtIndex:i] isEqualToString: @"Black"]) {
        NSLog(@"what button: %c", whichButton.pantsPressedBool); //HERE
        if (whichButton.pantsPressedBool == YES ) { //AND HERE is where I get the SIGABRT error
            // Drawing code
            CGContextRef context = UIGraphicsGetCurrentContext();
            [[UIColor blackColor] set];
            //CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 0.5); //CGContextRef, Red, Green, Blue, Alpha
            rect = CGRectMake(20, 20, 40, 80); //x, y, width, height
            CGContextFillRect(context, rect); //CGContextRef, CGRect
        }

    }
}
} 

一如既往,任何帮助都将不胜感激。

提前致谢。

[编辑]

所以我正在尝试不同的东西。

- (IBAction)pantsPressed:(UIButton *)sender {
ChooseColorsViewController *color = [[ChooseColorsViewController alloc] initWithNibName:@"ChooseColorsViewController" bundle:nil];
[self.navigationController pushViewController:color animated:YES];
AppDelegate *chosenButton = (AppDelegate *)[[UIApplication sharedApplication]delegate];
chosenButton.pantsButton = YES; 
}

所以在我的 AppDelegate 中,我有

@property (assign, readwrite) BOOL pantsButton;
@synthesize pantsButton = _pantsButton;

如果单击该按钮,我正在尝试将 AppDelegate 中的 BOOL 变量(pantsButton)设置为“YES”,并设置 if 语句

if ([chosenButton.pantsButton == YES]) {
    do something
 }

【问题讨论】:

  • “changeClothes”是我的“ChangeClothesViewController”。我知道这违反了 Apple 的标准命名约定,但我还没有修复它。但我确实知道这一点,并将在未来做出改变
  • 你有一个pantsPressed 属性和一个pantsPressed 操作方法,这不太好:) 重命名其中一个,然后重试
  • 大声笑哦,直到现在我才意识到这一点。修复它但仍然收到 SIGABRT 错误
  • 你能提供更多的错误细节吗?调试器说什么?
  • 这是我的输出框中的内容。 AppDelegatepantPressedBool]:无法识别的选择器发送到实例 0x6e4d170

标签: iphone objective-c uiviewcontroller boolean


【解决方案1】:

当您分配给whichButton 时,您正在将应用程序委托转换为(changeClothes *),但事实并非如此(也就是说,我假设您的应用程序委托不是changeClothes 的子类)。

这一行...

changeClothes *whichButton = (changeClothes *)[[UIApplication sharedApplication]delegate];

...是说,获取应用程序委托并假装它是changeClothes 对象。问题是你在对编译器撒谎。 :-) 稍后,当应用程序尝试使用您告诉它的东西时,它是 changeClothes 对象...

if (whichButton.pantsPressedBool == YES ) {

...它发现你给它的东西并没有像changeClothes 对象应该的那样运行(即对pantsPressedBool 一无所知)。

【讨论】:

  • 你能详细解释一下吗?我不确定你的意思
  • 已更新。我希望细节有所帮助。基本上,除非您真的确定它们是同一事物,否则不要将一种对象转换为另一种类型的引用。
  • 哦,好吧,我现在理解得更好了。我只是想 changeClothes *whichButton = (changeClothes *)[[UIApplication sharedApplication]delegate];是使自定义委托在不同“视图”之间来回传递对象/变量值的标准方法。你能给我一些关于如何解决我遇到的这个问题的建议吗?
  • 我是否需要在 AppDelegate 中创建更多 BOOL 并将 BOOL 值(是/否)存储在其中并在我的 drawRec 中获取该 BOOL?
  • 这是一种选择:像对待颜色名称一样对待它。或者,您可以将对ChangeClothesViewController 的引用传递给需要知道pantsPressedBool 值的任何其他类。实际上有很多方法取决于整个应用程序中数据共享的复杂程度。 (不同的主题:我刚刚意识到您正在使用 '==' 进行字符串比较......它应该是isEqualToString:。)
【解决方案2】:

您正在使用 AppDelegate passColorswhichButton 做一些奇怪的事情。在您的 drawRect 中,您指的是 AppDelegate 的 pantsPressedBool 属性。但是,您的属性在 ChangeClothesViewController 中声明。

您需要在 drawRectpantsPressed 以及您的函数中引用正确的对象。在这种情况下,它应该是您的 ChangeClothesViewController 实例。

【讨论】:

  • 我正在使用 passColor 从另一个视图中获取颜色(一旦用户单击按钮,就会调用此视图),一旦选择了颜色,我将其放入 NSMutable 数组和 for 循环中摆脱任何重复等。
  • 通过颜色确定。查看您分配 AppDelegate 的 changeClothes *whichButton。首先,它可能不是一个 changeClothes 对象(参见 Philip Mills 的回答)。其次:它没有_pantsPressedBool 属性,因为你告诉我你是在ChangeClothesViewController 中制作的。
  • changeClothes 是我的 ChangeClothesViewController。我只是还没有改名。对困惑感到抱歉。我不确定我是否理解菲利普斯的回答。也许你能解释得更好一点?
  • 是的,我知道它应该是您的 ChangeClothesViewController。但是,您将 App Delegate 分配给它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 2021-09-04
相关资源
最近更新 更多