UIButton编程在iOS中不起作用
当我点击按钮时,什么也没发生UIButton编程在iOS中不起作用,ios,objective-c,xcode,Ios,Objective C,Xcode,当我点击按钮时,什么也没发生 checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)]; [checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal]; [checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox-chec
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:@selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
您必须创建如下所示的按钮
UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom]; // this is important
checkbox.frame = CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:@selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
您必须首先使用button属性创建按钮,然后给出其框架
如果仍不工作,则self.background addSubview
应为self.view addSubview
什么是
self.background
?检查您的background
属性的userInteractionEnabled
属性
但我明白了为什么在rest正常的情况下您对按钮没有反馈-您使用
UIControlStateSelected
设置按钮接触时的图像,但您必须使用uicontrolstatelogated
您的代码完全正常。您需要检查以下内容
- 检查self.background是否正确放置在您的视图继承人权限中,我使用self.view而不是下面的self.background尝试了您的代码,效果非常好
- 从您给出的错误消息中
"
警告:尝试在其视图不在窗口层次结构中的
上显示
!
ui按钮的方法是使用类型为:
的按钮,而不是initWithFrame:
返回值
新创建的按钮
讨论
此方法是一个方便的构造函数,用于创建具有特定配置的按钮对象。如果子类UIButton,则此方法不会返回子类的实例。如果要创建特定子类的实例,则必须直接alloc/init按钮
创建UIButtonTypeCustom类型的自定义按钮时,按钮的框架最初设置为(0,0,0,0)。在将按钮添加到界面之前,应将框架更新为更合适的值
唯一一次应该将alloc/init
与UIButton
一起使用的时候是当您对UIButton
进行了子类化时,但是您的init方法应该实现按钮,类型为:
并传入uibuttonypecustom
因此,改变:
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
到
对于以编程方式创建的任何按钮,其在view Controller 2015-03-12 16:02:07.228 Sportslion Ios应用程序[71668:2157973]上不起作用警告:尝试在其视图不在窗口层次结构中的对象上显示!您可以发布更多的代码吗,如操作autologin
?什么是self.background?如果其UIImageView可能无法工作此按钮操作的预期效果他们的代码不是很好,因为他们需要使用[UIButtonWithType:]
而不是[[UIButton alloc]initWithFrame:]
因为这不会起任何作用。所以您的答案是incorrect@Popeye,你能证明你的评论是正确的吗?我测试了这段代码,它对我来说运行良好。阅读苹果关于UIButton
和创建按钮的文档,你就会明白为什么你应该使用buttonwhithtype
而不是initWithFrame:
@NikitaIvaniushchenko仅仅因为你的答案不正确,你不能接受,这不是开始侮辱其他用户的理由,这在这里是不受欢迎的。提醒版主注意在这个问题中没有提到只有该方法必须用于按钮创建。此外,他们使用预定义类型的按钮-UIButtonTypeRondRect,所以initWithFrame不适合。评论不是为了进行深入讨论;这段对话是。没错,但我仍然看到最初的问题没有得到解决-所以仍然不知道原因是什么。每个人都在对其原因进行假设。我想要按钮背面图像背景是图像对象按钮是可见的,但如果我按一下按钮什么事也没发生
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
// Create a button of type custom
checkbox = [UIButton buttoWithType:UIButtonTypeCustom];
// Now set the frame for your button
[checkbox setFrame:CGRectMake(73, 324, 15, 15)];
// Continue with the rest of your button code
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:@selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];