Transition metal carbides and nitrides (MXenes), as a large family of emerging two-dimensional materials, have demonstrated extraordinary performance in many fields such as electronics, optics and energy storage. However, their susceptibility to oxidation during preparation and storage in ambient air environment is undesirable for long-term and stable applications. Here, we have demonstrated that the spontaneous oxidation of Ti3C2T
x
can be harnessed ingeniously to prepare Ti3C2T
x
/TiO2
in situ formed heterojunctions. Furthermore, a self-powered ultraviolet photodetector was constructed based on the photoelectrochemical performance of Ti3C2T
x
/TiO2 heterojunctions. Since the highly conductive Ti3C2T
x
can promote the separation and transfer of photogenerated carriers in TiO2, the prepared photodetector exhibits high responsivity (2.06 mA W−1), short rise and decay times (45 and 69 ms) and long-term stability. This work demonstrates the controllable synthesis of Ti3C2T
x
/TiO2 heterojunctions and provides a new promising potential of MXenes for photodetection applications.