NSTabViewController添加NSToolbarItems

时间:2017-11-08 12:15:44

标签: swift macos xcode9 nstabview nstabviewcontroller

我想使用NSTabViewController通过工具栏样式切换6个不同的Tab。

所有选项卡都有共同点,它们显示了Customer实体的不同方面。

现在我想将附加的NSToolbarItems添加到NSTabViewController的工具栏中?但我还没有找到一种方法来访问工具栏。 我还想在ToolbarItem之间添加Space。

有办法吗?

或者如何在不使用NSTabViewController的情况下将我的ViewController从Storyboard添加到NSTabView?

此致

奥利弗

与此同时,我尝试了另一种方法,我认为这种做法更有希望但导致另一种奇怪的行为:

我已经创建了一个新的NSViewController并在其中放置了一个NSTabView。为了加载我已经存在的ViewControllers,我使用了这个

override func viewDidLoad() {
    super.viewDidLoad()
    let customerController = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("CustomerVCID")) as! CustomerViewController

    let servicesController = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("ServicesVCID")) as! ServicesController

    customerController.customer = self.customer
    servicesController.customer = self.customer

    self.tabView.tabViewItems[0].view = customerController.view
    self.tabView.tabViewItems[1].view = servicesController.view

}

确实有效,但现在我所有有动作的NSButton都会导致我的应用程序崩溃。

1 个答案:

答案 0 :(得分:1)

每个窗口只有一个工具栏。因此,您的def insert_figure_image_to_slides(user_login_dict, figure_url, page_id): presentation_id = user_login_dict["presentation_id"] jsonFile = user_login_dict["api_login_json"] scope = 'https://www.googleapis.com/auth/presentations'#, 'https://www.googleapis.com/auth/drive.file' seed(randint(page_id,101)) id=randint(1, 100000) image_id = "Figure" + str(id) try: working_folder = os.getcwd() os.chdir("..") os.chdir(user_login_dict["certificates_folder"]) credentials = service_account.ServiceAccountCredentials.from_json_keyfile_name(jsonFile, scopes=scope) os.chdir(working_folder) service = build('slides', 'v1', http=credentials.authorize(httplib2.Http()), cache_discovery=False) slides = service.presentations().get(presentationId=presentation_id, fields='slides').execute().get('slides') page_ids = [] for slide in slides: page_ids.append(slide['objectId']) emu4M = { 'magnitude': 4000000, 'unit': 'EMU' } requests=[] requests.append({ 'createImage': { 'objectId': image_id, 'elementProperties': { 'pageObjectId': page_ids[page_id] }, 'url': figure_url } }) body = { 'requests': requests } response = service.presentations().batchUpdate(presentationId=presentation_id, body=body).execute() create_image_response = response.get('replies')[0].get('createImage') print('Created image with ID: {0}'.format( create_image_response.get('objectId'))) except: os.chdir(working_folder) print("Image insertion into presentation failed") def export_figures_to_drive(user_login_dict, figure_name, figure_no): drive_folder_id = user_login_dict["drive_folder_id"] file_name = figure_name + ".png" scopes = ( 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/presentations', ) os.chdir("Output_files") output_dir = os.getcwd() os.chdir("..") try: working_folder = os.getcwd() os.chdir("..") os.chdir(user_login_dict["certificates_folder"]) g_login = GoogleAuth(user_login_dict["drive_login_yaml"]) g_login.LocalWebserverAuth() store = Storage(user_login_dict["drive_login_json"]) flow = client.flow_from_clientsecrets(user_login_dict["drive_login_json"], scopes) os.chdir(working_folder) drive = GoogleDrive(g_login) file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList() for file in file_list: if file['title'] == user_login_dict["result_file_name"]: folder_id = file.get('id') file_list = drive.ListFile({'q': "'%s' in parents and trashed=false" % folder_id}).GetList() for file1 in file_list: if file1['title'] == file_name: file1.Delete() os.chdir(output_dir) file_on_drive = drive.CreateFile({'parents': [{'id': drive_folder_id}]}) file_on_drive.SetContentFile(file_name) file_on_drive.Upload() os.chdir(working_folder) print('Graph Export to drive complete - %s' % (file_name)) metadata = file_on_drive.attr["metadata"] fig_url4 = metadata["alternateLink"] fig_url5 = metadata['selfLink'] fig_url6 = metadata['webContentLink'] fig_url7 = metadata['downloadUrl'] except: os.chdir(working_folder) print("Error uploading file to drive") creds = tools.run_flow(flow, store) #opens browser identificaiton page HTTP = creds.authorize(httplib2.Http()) DRIVE = discovery.build('drive', 'v3', http=HTTP) rsp = DRIVE.files().list(q="name='%s'" % file_name).execute().get('files')[0] fig_url1 = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png' fig_url2 = "https://drive.google.com/uc?export=download&id=" + rsp["id"] fig_url3 = '%s&access_token=%s' % (DRIVE.files().get_media(fileId=rsp['id']).uri, creds.access_token) fig_url8="https://drive.google.com/file/d/1AmCmoc8Xrv706yJeWCmCuQw0HQkkYHpQ/view?usp=sharing" insert_figure_image_to_slides(user_login_dict=user_login_dict, figure_url=fig_url1, page_id=figure_no) 分享了它。

  1. 选择NSTabViewController的工具栏模式
  2. 覆盖NSTabViewController并添加您的物品

示例:

NSWindowController

您始终可以通过以下路径视图->窗口->工具栏来访问工具栏

您唯一的问题是每个 override func windowDidLoad() { super.windowDidLoad() window?.toolbar?.insertItem(withItemIdentifier: .print, at: 0) } 有一个delegate。这意味着您必须在NSToolbar委托中创建自定义NSToolbarItem

NSTabViewController

请记住,您必须叫超级。这是因为基础方法想要创建与视图控制器的绑定。 如果您需要工具栏中的可操作按钮,只需添加它们而无需调用super。