-1

たまに呼び出されるメソッドがあります。ボタンを作成し、フレームを与えて、self.view に追加します。問題は、これらのボタンを重ねたくないということです。そのため、カウンター (整数) を使用して、ボタンの数を数えています。カウンターが +-1 の誤差を生じ、2 つのボタンが重なって表示されることがあります。

これはコードです:

私の.hで

int CountNumberOfBluetoothDevices;

.m で

- (void) browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info{

NSLog(@"%@", peerID.description);

CountNumberOfBluetoothDevices = CountNumberOfBluetoothDevices + 1;

NSArray *keys = [BluetoothDeviceDictionary allKeys];
for (NSUInteger k = keys.count; k > 0; k--) {
    MCPeerID *key = keys[k - 1];
    UIButton *btn = BluetoothDeviceDictionary[key];
    if ([key.displayName isEqualToString:peerID.displayName]) {
        [btn removeFromSuperview];
        NSLog(@"REMOVE DOUBLE");
        CountNumberOfBluetoothDevices--;
        [BluetoothDeviceDictionary removeObjectForKey:key];
    }
}

if (CountNumberOfBluetoothDevices == 1) {
BluetoothDeviceButton = [[UIButton alloc] initWithFrame:CGRectMake(130, -200, 55,55)];
}else if (CountNumberOfBluetoothDevices == 2){
    BluetoothDeviceButton = [[UIButton alloc] initWithFrame:CGRectMake(240, -200, 55,55)];
}else if (CountNumberOfBluetoothDevices == 3){
    BluetoothDeviceButton = [[UIButton alloc] initWithFrame:CGRectMake(130, -125, 55,55)];
}else if (CountNumberOfBluetoothDevices == 4){
    BluetoothDeviceButton = [[UIButton alloc] initWithFrame:CGRectMake(240, -125, 55,55)];
}

これを検出するより良い方法はありますか?

4

1 に答える 1