我有一个用作开关的 UIButton。当用户点击它时,它的状态变为“选中”并调用动作“一”。当用户再次点击时,UIButton 状态更改为“未选中”并且该操作不再可用。
有没有办法通过点击一个完全不同的 UIButton 来将 UIButton 设置为“选定”并将其更改为“选定”并调用相同的操作?
干杯
根据我从问题中了解到的情况,我想给你我的建议
if (!<your_button>.selected)
{
<your_button>.selected = YES;
// do your stuff here
}
else
{
<your_button>.selected = NO;
// do your stuff here, in your case action should be no longer available so that,
<your_button>.userInteractionEnabled = NO;
}
享受编程!
简单的事情,当您点击 uibutton 为该按钮执行操作并在该按钮的操作中,您可以将按钮状态设置为选中,然后在下一行代码中您可以调用该方法(操作)
- (IBAction)btn1Touched:(id)sender {
[_btn2 setSelected:YES];
[self btn2Touched:self];
}
- (IBAction)btn2Touched:(id)sender {
NSLog(@"btn Action called");
}
UIbutton has state called selected
and it is managed via property selected
so to make a button to selected state
[buttonInstance setSelected:YES];
To make it unselected use
[buttonInstance setSelected:NO];
请为该按钮操作编写一个函数,例如,
-(void)Buttonaction
{
do your action here
also set your button has selected state.
buttonname.setselected=yes;
}
在您按下按钮时调用此函数。
试试这样,'
-(IBAction)nextbutton:(id)sender{
UIButton *btn=(UIButton *)[self.view viewWithTag:tagValue];//pass tag value of that particuler button
btn.selected=YES;
//do whatevr you want.
}
做这个:
-(IBAction)switchButtonClicked:(UIButton*)sender
{
if(sender.isSelected)
{
.....
}
else
{
...
}
[sender setSelected:!sender.isSelected];//This will work as Switch.
}
根据需要设置
Selected(Switch ON)
和default(Switch OFF)
属性(图像和标题)UIButton
fromXIB
,或来自 Code 。只需一个按钮,您就可以使用此代码完成功能