簡體   English   中英

將導航欄取消按鈕替換為后退按鈕

[英]Replace navigation bar cancel button with back button

我在導航控制器中嵌入了一個表格視圖控制器。 表格單元都是靜態的,選擇其中任何一個單元都會切換到另一個表格視圖。 當發生segue時,導航欄將為新視圖顯示“取消”按鈕,而不是“返回”按鈕。

我可以在代碼中添加一個后退按鈕,例如

- (void)viewDidLoad
{
    self.navigationItem.backBarButtonItem =
        [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                         style:UIBarButtonSystemItemCancel
                                        target:nil
                                        action:nil];
    self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
}

但是,后退按鈕將是一個矩形,而不是默認的后退按鈕形狀,該形狀在左邊緣有一個角度。 如何簡單地將取消按鈕更改為系統后退按鈕?

這是我的從表單元格到下一個表視圖的代碼

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    switch (indexPath.row) {
        case 0:
            [self performSegueWithIdentifier:@"goToSecondTable" sender:self.tableView];
            break;
        /* and perform segue for other rows */
        default:
            break;
    }
}

在prepareForSegue中沒有任何事情要做。

這是連接檢查器顯示的內容

連接檢查器

這是“ Bar Button Item-Back”的連接

條形按鈕項-返回

默認情況下,系統提供的后退按鈕應為左欄按鈕,而無需執行任何操作(使用代碼或IB)。

在“連接”檢查器中刪除與backBarButton的連接。 從IB中的導航欄中刪除后退按鈕。 移除代碼中后杠按鈕的出口。 運行您的應用程序,您應該會看到免費提供的后退按鈕。

如果沒有默認的后退按鈕不起作用,請自定義代碼

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:myBackImage style: UIBarButtonItemStylePlain target:self action:someAction];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];

為什么您要在didselectrow中執行序號,而不是僅按ViewController?

嘗試[self.navigationController pushViewController:YOURVIEWCONTROLLER];

這將確保您有一個后退按鈕。 另外,您必須使用類似的segue,確保對下一個控制器使用推送segue。 看起來您在第二個viewController中創建了自己的后退按鈕。 您無需創建一個,因為將為您創建一個具有上一個viewcontroller標題的視圖。 您可以通過在推送之前在前一個視圖控制器中更改self.title = @“ Back”來確保它返回。

代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    switch (indexPath.row) {
        case 0:
            self.title = @"Back";
            [self.navigationController pushViewController:SECONDVC];
            break;
        /* and perform segue for other rows */
        default:
            break;
    }
}

並且在viewWillAppear中:

 - (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
   self.title = @"Whatever you want";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM