A general procedure for solving the time-dependent Schrödinger equation