Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.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
Ios 将UI元素连接到阵列中的插槽_Ios_Iphone_Cocoa Touch - Fatal编程技术网

Ios 将UI元素连接到阵列中的插槽

Ios 将UI元素连接到阵列中的插槽,ios,iphone,cocoa-touch,Ios,Iphone,Cocoa Touch,我从周一到周日有7个交换机,需要将所有7个交换机连接到我的控制器。将它们单独连接起来感觉非常尴尬: @property (strong, nonatomic)IBOutlet UISwitch *switch1; @property (strong, nonatomic)IBOutlet UISwitch *switch2; @property (strong, nonatomic)IBOutlet UISwitch *switch3; ... @property (strong, nonato

我从周一到周日有7个交换机,需要将所有7个交换机连接到我的控制器。将它们单独连接起来感觉非常尴尬:

@property (strong, nonatomic)IBOutlet UISwitch *switch1;
@property (strong, nonatomic)IBOutlet UISwitch *switch2;
@property (strong, nonatomic)IBOutlet UISwitch *switch3;
...
@property (strong, nonatomic)IBOutlet UISwitch *switch7;
// It gets worse when you have even more switches

相反,是否可能有一个容纳7个交换机的
NSArray*switchArr
,我们将交换机1连接到switchArr[1],将交换机2连接到switchArr[2],依此类推

右键单击IB中的单个开关,并从“新引用插座集合”拖动到.h文件,以创建新的插座集合。你应该去看看房子

@property (strong, nonatomic) IBOutletCollection(UISwitch) NSArray *switches;

创造。重复每隔一次拖动开关到该属性。

您可以这样做

@property (strong, nonatomic)IBOutletCollection(UISwitch) NSArray * switches;

按照希望开关在阵列中的顺序为每个开关设置标记,可以从10开始标记以避免与其他视图冲突,然后在viewDidLoad中,可以将视图存储到可变阵列中,如下所示:

NSMutableArray *switches = [[NSMutableArray alloc] init];
NSInteger initialTag = 10; //set your initial tag
for(NSInteger i = initialTag; i < initialTag + 7; i++) {
    [switches addObject:[self.view viewWithTag:i]];
}
NSMutableArray*开关=[[NSMutableArray alloc]init];
NSInteger initialTag=10//设置初始标签
对于(NSInteger i=initialTag;i
订单是否保留?例如,如果我按
1,2,3,4
的顺序连接,是否一定要将
1
连接到数组中的插槽
0
?不是100%确定,但您可以始终为每个开关使用标记。什么意思是为每个开关使用标记?在属性检查器中,您可以为每个开关设置属性“标记”,从0到6。然后,当您通过调用switch.tag来迭代switches数组以确定要访问哪个交换机时,可以访问该命令。还可以查看UIView上的viewWithTag方法。但是请注意,0是默认值,因此它可能会返回一些随机子视图:)我想添加以下内容:为每个开关设置一个标记,并在viewDidLoad中根据标记对数组进行排序