2012-08-28 105 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

相關問題