Regulating the heterostructure of electrode materials has a great influence on electron migration and subsequent electrochemical reactions, which play a critical role in the sensing process. Herein, a facile method has been demonstrated to synthesize TiN/graphitic carbon (TiN/gC) nanocomposites by the in situ growth of TiN nanoparticles on C nanosheets. The formation of TiN/gC heterojunctions was evidenced by X-ray diffraction, Raman microscopy, and transmission electron microscopy. This heterostructure can effectively facilitate electron transfer and increase the electrochemically active area, which improves the electrocatalytic performance. Interestingly, the TiN/gC-modified glassy carbon electrode (TiN-gC/GCE) exhibited excellent electrocatalytic activity toward ofloxacin (OFLX) redox. With the change in the OFLX concentration, the electrochemical response of the sensor also changes. By using a differential pulse voltammetry (DPV) technique, the designed sensor showed a wide detection range: the DPV response was linearly dependent on the concentrations in the range of 0.051.0 and 1.0100 μM. Moreover, TiN-gC/GCE demonstrated outstanding analytical performances in the quantitative OFLX assay, such as a high sensitivity of 2876 µAmM−1cm−2, low detection limit of 0.016 μM, good selectivity and stability. The proposed method was successfully applied for OFLX detection in eye drops and environmental water with satisfactory results.