2012-08-28 107 views
0

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

我在运行代码时无法访问布尔值并出现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; 

我想设置布尔变量(pantsButton)中的AppDelegate里面“YES”,如果被点击的按钮,使if语句这样

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

“changeClothes”是我的“ChangeClothesViewController”。我知道这违背了苹果的标准命名约定,但我还没有修复它。但我知道这件事,并会在未来做出改变 – Jay

+0

你有一个裤子压力属性和一个裤子压制的动作方法,这不能是好的:)重命名其中一个,然后再试一次 – Asciiom

+0

哈哈,我甚至没有意识到,直到现在。修复它,但仍然得到SIGABRT错误 – Jay

回答

2

当你分配给whichButton但这不是它是什么(即你正在铸造应用程序的委托来(changeClothes *),我假设你的应用程序委托是不是苏b类changeClothes)。

这条线......

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

...是说,把应用程序委托,假装这是一个changeClothes对象。问题在于你对编译器说谎。 :-)后来,当应用程序试图使用你告诉它是一个changeClothes对象的东西...

if (whichButton.pantsPressedBool == YES) { 

...它发现你给的东西它不会作用对象应该采取行动(即不知道任何关于pantsPressedBool)。

+0

我认为这的确的确如此:) – Asciiom

+0

你能解释一下更详细一点吗?我不知道你的意思 – Jay

+0

已更新。我希望细节帮助。基本上,除非你真的确定它们是相同的东西,否则不要将一种对象投射到另一种类型的引用上。 –

1

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

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

+0

我使用passColor从另一个视图中获取颜色(一旦用户点击按钮,这个视图就会被调用),一旦颜色被选中,我把它放在NSMutable数组内,并在for循环中去除任何重复项等等。 – Jay

+0

好的关于passColor。看看你正在分配AppDelegate的'changeClothes * whichButton'。首先,它可能不是一个changeClothes对象(参见Philip Mills的答案)。其次:它没有'_pantsPressedBool'属性,因为你告诉我你在'ChangeClothesViewController'中做了这个。 – Rengers

+0

changeClothes是我的ChangeClothesViewController。我只是还没有确定名字。对困惑感到抱歉。我不知道我是否理解菲利普斯的答案。也许你可以更好地解释它? – Jay

相关问题