我目前正在开发一个应用程序,如果用户单击一个按钮,它会将 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
}