So did he have a spare communicator? And why didn't I notice this before?
Either a) he made a public show of giving it to Kirk so that the Yonadans wouldn't frisk him for the spare, just in case, because McCoy wants to be ready for anything, or b) Kirk/Spock slipped it to him when he wasn't looking. When he learns that there's a chance to save the asteroid ship, he reaches into his pocket without thinking and sure enough, found he had one after all. (Good old Jim/Spock).