如果我有多个UISwitch,我怎么知道哪些是开启的?

时间:2021-12-10 03:39:12

Hope that all are fine.

希望一切都好。

I have 22 UISwitch in a table view and I have created all these switch objects in one function and set them in the table view.

我在表视图中有22个UISwitch,我在一个函数中创建了所有这些开关对象,并在表视图中设置它们。

How can I know which switch is on or off? I want to do this through only one action method but I don't know how to do that.

我怎么知道哪个开关打开或关闭?我想通过一种动作方法来做到这一点,但我不知道该怎么做。

If anyone has faced this problem, please give me ideas...

如果有人遇到这个问题,请给我一些想法......

Thanks, Haresh.

3 个解决方案

#1


The best way to do something like this is to set up each table cell with your table view controller as delegate along with a tag indicating which switch that cell is presenting - then have each cell detect the switch change, and in turn tell the delegate that the switch with the indicated ID has been altered. You can keep the switch states in an NSDictionary stored by cell switch tag.

执行此类操作的最佳方法是使用表视图控制器设置每个表格单元格作为委托以及指示单元格所呈现的切换的标记 - 然后让每个单元格检测到切换更改,然后告诉代理人具有指示ID的开关已被更改。您可以将开关状态保存在由单元格开关标记存储的NSDictionary中。

This all of course implies a custom UITableViewCell class so you can store a tag and a delegate link back to your UITableViewController (note, do NOT retain this link back to your table view controller)

这当然意味着一个自定义的UITableViewCell类,因此您可以将标记和委托链接存储回UITableViewController(注意,不要将此链接保留回您的表视图控制器)

EDIT This answer provides a different approach you may prefer:

编辑此答案提供了您可能更喜欢的不同方法:

Getting the row of a NSButtonCell

获取NSButtonCell的行

#2


tableView:didSelectRowAtIndexPath: will only fire for a selection of the row itself within your table view, which doesn't sound like what you're trying to do.

tableView:didSelectRowAtIndexPath:只会触发表视图中行本身的选择,这听起来不像你想要做的那样。

In the method where you create your UISlider instances, you could store those instances as keys in an NSMutableDictionary (using setObject:forKey), with NSNumbers as the values for use in a later switch statement. For each UISlider, make sure that the change in their value is being recorded by setting the target to be a method in the controller using something similar to the following:

在创建UISlider实例的方法中,可以将这些实例作为键存储在NSMutableDictionary中(使用setObject:forKey),NSNumbers作为在以后的switch语句中使用的值。对于每个UISlider,通过使用类似于以下内容的方法将目标设置为控制器中的方法,确保记录其值的更改:

[slider addTarget:self action:@selector(sliderValueDidChange:forEvent:) forControlEvents:UIControlEventAllTouchEvents];

and set up a method in your controller similar to this:

并在控制器中设置一个方法,类似于:

- (void)sliderValueDidChange:(id)sender forEvent:(UIEvent *)event;

within which you can use the sender, the NSDictionary of UISlider instances, and a switch statement on the NSNumber's intValue to determine which slider's value has changed and what the new value is.

在其中,您可以使用发件人,UISlider实例的NSDictionary以及NSNumber的intValue上的switch语句来确定哪个滑块的值已更改以及新值是什么。

Instead of an on/off switch, may I also suggest the use of a checkmark as an accessory view? Then, you could use tableView:didSelectRowAtIndexPath: to track user selection of the row and display a checkmark in the accessory view if the element has become selected. This might be more in line with Apple's Human Interface Guidelines.

我可以建议使用复选标记作为附件视图而不是开/关开关吗?然后,您可以使用tableView:didSelectRowAtIndexPath:跟踪用户对行的选择,如果元素已被选中,则在附件视图中显示复选标记。这可能更符合Apple的人机界面指南。

#3


checkmark on accessory view?????? found one example 'Accessory' but they have set one button and set the image of checkmark button and uncheckmark button..you mean to say same like that

附件视图上的复选标记??????找到一个例子'附件',但他们设置了一个按钮并设置了复选标记按钮的图像并取消选中按钮..你的意思是说同样的那样

#1


The best way to do something like this is to set up each table cell with your table view controller as delegate along with a tag indicating which switch that cell is presenting - then have each cell detect the switch change, and in turn tell the delegate that the switch with the indicated ID has been altered. You can keep the switch states in an NSDictionary stored by cell switch tag.

执行此类操作的最佳方法是使用表视图控制器设置每个表格单元格作为委托以及指示单元格所呈现的切换的标记 - 然后让每个单元格检测到切换更改,然后告诉代理人具有指示ID的开关已被更改。您可以将开关状态保存在由单元格开关标记存储的NSDictionary中。

This all of course implies a custom UITableViewCell class so you can store a tag and a delegate link back to your UITableViewController (note, do NOT retain this link back to your table view controller)

这当然意味着一个自定义的UITableViewCell类,因此您可以将标记和委托链接存储回UITableViewController(注意,不要将此链接保留回您的表视图控制器)

EDIT This answer provides a different approach you may prefer:

编辑此答案提供了您可能更喜欢的不同方法:

Getting the row of a NSButtonCell

获取NSButtonCell的行

#2


tableView:didSelectRowAtIndexPath: will only fire for a selection of the row itself within your table view, which doesn't sound like what you're trying to do.

tableView:didSelectRowAtIndexPath:只会触发表视图中行本身的选择,这听起来不像你想要做的那样。

In the method where you create your UISlider instances, you could store those instances as keys in an NSMutableDictionary (using setObject:forKey), with NSNumbers as the values for use in a later switch statement. For each UISlider, make sure that the change in their value is being recorded by setting the target to be a method in the controller using something similar to the following:

在创建UISlider实例的方法中,可以将这些实例作为键存储在NSMutableDictionary中(使用setObject:forKey),NSNumbers作为在以后的switch语句中使用的值。对于每个UISlider,通过使用类似于以下内容的方法将目标设置为控制器中的方法,确保记录其值的更改:

[slider addTarget:self action:@selector(sliderValueDidChange:forEvent:) forControlEvents:UIControlEventAllTouchEvents];

and set up a method in your controller similar to this:

并在控制器中设置一个方法,类似于:

- (void)sliderValueDidChange:(id)sender forEvent:(UIEvent *)event;

within which you can use the sender, the NSDictionary of UISlider instances, and a switch statement on the NSNumber's intValue to determine which slider's value has changed and what the new value is.

在其中,您可以使用发件人,UISlider实例的NSDictionary以及NSNumber的intValue上的switch语句来确定哪个滑块的值已更改以及新值是什么。

Instead of an on/off switch, may I also suggest the use of a checkmark as an accessory view? Then, you could use tableView:didSelectRowAtIndexPath: to track user selection of the row and display a checkmark in the accessory view if the element has become selected. This might be more in line with Apple's Human Interface Guidelines.

我可以建议使用复选标记作为附件视图而不是开/关开关吗?然后,您可以使用tableView:didSelectRowAtIndexPath:跟踪用户对行的选择,如果元素已被选中,则在附件视图中显示复选标记。这可能更符合Apple的人机界面指南。

#3


checkmark on accessory view?????? found one example 'Accessory' but they have set one button and set the image of checkmark button and uncheckmark button..you mean to say same like that

附件视图上的复选标记??????找到一个例子'附件',但他们设置了一个按钮并设置了复选标记按钮的图像并取消选中按钮..你的意思是说同样的那样