Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 更改appDelegate变量时应用程序崩溃_Iphone_Objective C_Cocoa Touch - Fatal编程技术网

Iphone 更改appDelegate变量时应用程序崩溃

Iphone 更改appDelegate变量时应用程序崩溃,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,更新AppDelegate varibale时,我的应用程序崩溃。我是以错误的方式声明对象,还是UIColor没有资格这样通过 在AppDelegate.h中,我声明UIColor对象: UIColor *myColor; @property(nonatomic,retain) UIColor *myColor; In my viewController i update the myColor when a button pressed. -(IBAction) pressed:(id)

更新AppDelegate varibale时,我的应用程序崩溃。我是以错误的方式声明对象,还是UIColor没有资格这样通过

在AppDelegate.h中,我声明UIColor对象:

UIColor *myColor;

@property(nonatomic,retain) UIColor *myColor;

In my viewController i update the myColor when a button pressed.

-(IBAction) pressed:(id)sender{
    YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.myColor = [sender backgroundColor];
}

但在我多次按下按钮更新myColor后,应用程序崩溃了。有专家能指出哪一部分出了问题吗?

您在这一行得到警告了吗?[发送者背景色]

如果是,则不使用[sender backgroundColor]

使用


你是合成myColor的财产吗?坠机发生在哪里?当它发生时,检查调试器中的堆栈跟踪。或者像这样重写方法的原型:-iAction pressed:UIButton*senderButton;这样,Interface Builder将不允许您连接按钮以外的其他内容,也不需要强制转换,这只是隐藏警告,但不会阻止问题的发生。谢谢Kingo。但我追踪了错误的来源。这是由于在我尝试更新该值时,应用程序的某个地方使用了appDelegate.myColor,从而导致应用程序崩溃。
UIButton *but=(UIButton *)sender;
appDelegate.myColor=[but backgroundColor];