John Sidney McCain III, the maverick Republican who twice sought the presidency and served in the Senate for more than three decades after surviving brutal captivity during the Vietnam War, died Saturday after battling brain cancer. He was 81.

“Senator John Sidney McCain III died at 4:28pm on August 25, 2018. With the Senator when he passed were his wife Cindy and their family. At his death, he had served the United States of America faithfully for sixty years,” the family said in a statement.

A public figure for nearly 40 years, Sen. McCain left an indelible mark on the nation during a long and sometimes polarizing political career marked by an aggressive military posture as the United States fought wars in Iraq and Afghanistan.

In the final few years of his life, the senator clashed sharply with President Trump, who said early in the 2016 presidential campaign that McCain, widely celebrated for his tenacity as a prisoner of war, was not a war hero.

McCain first arrived on the national stage as a POW after his plane was shot down over Hanoi in 1967. A few months earlier, McCain, then a Navy pilot, had survived a deadly fire on the USS Forrestal, an aircraft carrier.

In 1982, he was elected to the U.S. House of Representatives. Four years later, he won his seat in the Senate. In the upper chamber of Congress, McCain established himself as a leading voice on national security and foreign policy, particularly from his chairmanship of the powerful Armed Services Committee.

He also cultivated a reputation as an independent willing to work with Democrats on immigration and campaign finance. McCain was a fierce critic of Russia and a strong proponent of an aggressive U.S. role against the Islamic State extremist group.

While he had the respect of many GOP leaders, Trump criticized him personally in 2015, shocking political observers. Then a candidate for president, Trump said McCain was not a war hero because he was captured by the North Vietnamese. “I like people that weren’t captured,” Trump said at the time.

The two men also were at odds over policy. Last fall, McCain offered a sharp, if veiled critique of Trump, saying in a speech that “some half-baked, spurious nationalism” should be considered “as unpatriotic as an attachment to any other tired dogma of the past that Americans consigned to the ash heap of history.”

Earlier this year, McCain came out against Trump’s nominee for director of the Central Intelligence Agency. McCain said Gina Haspel had not adequately explained her involvement, during the George W. Bush administration, in a controversial “enhanced” interrogation program that he and other critics said amounted to torture. Haspel was confirmed by the Senate nevertheless.

McCain spoke from experience. He was held for more than five years in a Hanoi prison, where he was tortured and often deprived of sleep and food.

The son and grandson of Navy admirals, he was offered early release by his captors but refused to go home before the other POWs.

His opposition to Haspel drew a crass response from Kelly Sadler, a White House communications official, who dismissed the senator’s rejection of the nominee, saying, “It doesn’t matter; he’s dying, anyway,” according to The Washington Post and other media outlets.

In the summer of 2017, Sen. McCain was diagnosed with a tumor called a glioblastoma, which is an aggressive type of brain cancer.

He returned to the Senate after his diagnosis and cast a pivotal vote against a Republican bill to undo the Affordable Care Act. During a post-midnight roll call on the Senate floor, he turned his thumb down and effectively thwarted one of the GOP’s signature promises of recent years.

After leaving Washington in December, he never returned to the Senate. As he underwent treatment in Arizona, he kept a low profile, issuing written statements on major news developments but offering the public few glimpses of his condition.

McCain collaborated with a longtime adviser, Mark Salter, on a memoir, “The Restless Wave,” that was released in May.

Among other things, the book captured McCain’s difficult relationship with Trump. The president, he wrote, “has declined to distinguish the actions of our government from the crimes of despotic ones.”

He also remarked that,” The appearance of toughness, or a reality show facsimile of toughness, seems to matter more than any of our values.”

McCain ran for president in 2000, losing the Republican nomination to the eventual 43rd president, George W. Bush. Eight years later, he ran again, this time winning his party’s nomination but losing the general election to Barack Obama.

McCain struggled against strong head winds in a banner year for Democrats. Obama and his party scored major victories as voters vented their anger and anxiety over the Iraq War and a struggling economy.

The Arizona senator selected then-Alaska Gov. Sarah Palin for the party’s vice-presidential nominee, a controversial choice that would become one of the defining decisions of his career.

Two years after running for president, he sought reelection to the Senate. The conservative tea party movement was on the rise in 2010, and McCain tacked to the right to survive a contested primary.

In one controversial campaign commercial, Sen. McCain talked about the need to “complete the danged fence” to address concerns about undocumented immigrants crossing the nation’s southern border.

In 2013, McCain moved back to the middle on immigration. He joined the bipartisan “Gang of Eight” senators who proposed a sweeping rewrite of immigration laws that included a path to citizenship for undocumented immigrants.

The bill passed the Republican-controlled Senate but went nowhere in the Republican-controlled House.

The senator is survived by his mother, Roberta; his wife, Cindy; two sons and a daughter from a first marriage, Douglas, Andrew and Sidney; four children from his second marriage, Meghan McCain, Jimmy McCain Jack McCain and Bridget McCain; a brother, Joseph P. McCain of Washington; a sister, Jean Alexandra McCain Morgan of Annapolis; and five grandchildren.

McCain’s death opens up a Senate seat that party leaders expect to remain in Republican hands for two more years, as the GOP clings to a narrow majority in the upper chamber of Congress.

Under state law, Republican Gov. Doug Ducey will appoint a successor. Among the list of potential appointees are McCain’s wife, Cindy; Ducey’s chief of staff, Kirk Adams; state Treasurer Eileen Klein; former congressman John Shadegg; and former U.S. senator Jon Kyl. State law says the appointee must be from the same party as the person vacating the seat.

Because McCain died after the deadline to file for this year’s election, the new senator will not have to face voters until the 2020 election.