Build stud walls from dry lumber. Allowing lumber exposed to moisture to dry thoroughly before constructing stud walls will reduce nail pops. Wait as long as feasible after stud walls are set in place before hanging drywall, permitting the lumber to further shrink as much as possible.
Attach drywall with specially designed drywall screws and a power screwdriver. Engineered to hold drywall firmly in place, drywall screws are much more effective in securing drywall to studs than nails.
Replace popped nails with drywall screws. Place a piece of scrap plywood, or other thin board, on the wall with the edge of the wood touching the side of the nail. Place the head of a claw hammer on the plywood, so as not to damage the drywall, and pry the nail from the wall with the hammer.
Press the drywall tightly against the wall with one hand and install a drywall screw directly above or below the nail hole with the power screwdriver. Drive the screw into the wall until the head of the screw is set just below the drywall surface.
Smooth joint compound over the nail hole and screw head with a putty knife. Allow the joint compound to dry and sand the patched areas with 320-grit sandpaper before repainting.