如何在现有视图控制器的基础上部分加载新的视图控制器?

时间:2016-01-28 01:33:17

标签: ios objective-c swift viewcontroller scene

目标是实现与在iOS上的Mail应用程序中创建新电子邮件完全相同的效果。

单击Mail应用程序右下角的“撰写”按钮时,当前视图控制器在后台略微淡化,并且新视图控制器部分加载在其上。仍然可以在屏幕顶部感知旧视图控制器。当点击右上角的“+”按钮时,Fantastical应用程序也会这样做。

1 个答案:

答案 0 :(得分:0)

您可以将其作为子视图控制器添加到当前视图控制器中。

import java.util.Arrays;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;


public class TableViewTest extends Application
{

    public static void main(String[] args)
    {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        // basic ui setup
        AnchorPane parent = new AnchorPane();
        Scene scene = new Scene(parent);
        primaryStage.setScene(scene);



        //create a basic tableView
        TableView<String> table = new TableView<String>();
        table.setEditable(true);
        TableColumn<String, String> column = new TableColumn<String, String>();
        column.setCellFactory(new CellFactory());
        column.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue()));

        column.setOnEditCommit(e -> {
            int row = e.getTablePosition().getRow() ;
            table.getItems().set(row, e.getNewValue());
        });

        for (int i = 0 ; i < 50; i++) {
            table.getItems().add(new String("Test"));
        }

        table.getColumns().add(column);
        parent.getChildren().add(table);

        primaryStage.show();
    }

    public static class CellFactory implements Callback<TableColumn<String, String>, TableCell<String, String>> {

        private int editingIndex = -1 ;

        @Override
        public TableCell<String, String> call(TableColumn<String, String> param) {
            return new TableCell<String, String>() {

                private TextField textField = new TextField() ;

                {
                    textField.setOnAction(e -> commitEdit(textField.getText()));
                    textField.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
                        if (e.getCode() == KeyCode.ESCAPE) {
                            cancelEdit();
                        }
                    });
                    setGraphic(textField);
                    setContentDisplay(ContentDisplay.TEXT_ONLY);
                }

                @Override
                protected void updateItem(String item, boolean empty)
                {
                    super.updateItem(item, empty);

                    if (getIndex() != -1 && getIndex() == editingIndex) {
                        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                    } else {
                        setText(empty ? null : item);
                        setContentDisplay(ContentDisplay.TEXT_ONLY);
                    }

                    // checks cells index and set its color.
                    if(this.getIndex() >= 0 && getIndex() < 12)
                        this.setStyle("-fx-background-color: rgba(253, 255, 150, 0.4);");
                    else this.setStyle("");
                }

                @Override
                public void startEdit() {
                    editingIndex = getIndex();
                    super.startEdit();
                    textField.setText(getItem());
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                }

                @Override
                public void commitEdit(String newValue) {
                    editingIndex = -1 ;
                    super.commitEdit(newValue);
                    setContentDisplay(ContentDisplay.TEXT_ONLY);
                }

                @Override
                public void cancelEdit() {
                    setText(getTableView().getItems().get(editingIndex));
                    editingIndex = -1 ;
                    super.cancelEdit();
                    setContentDisplay(ContentDisplay.TEXT_ONLY);
                }

            };
        }

    }

}