老铁们,大家好,相信还有很多朋友对于toolbar是什么意思?用法、例句和Cocoa开发之NSToolbar(工具栏)的相关问题不太懂,没关系,今天就由我来为大家分享分享toolbar是什么意思?用法、例句以及Cocoa开发之NSToolbar(工具栏)的问题,文章篇幅可能偏长,希望可以帮助到大家,下面一起来看看吧!
版权??2020水煮沉浮所有,未经授权不得转载。
本教程使用Xcode12,Swift5.3实现。
创建好toolbar之后要设置其delegate,在delegate中实现以下方法就可以完成自定义toolbar了
//默认的按钮列表\nfunctoolbarDefaultItemIdentifiers(_toolbar:NSToolbar)->[NSToolbarItem.Identifier]\n//允许使用的按钮列表\nfunctoolbarAllowedItemIdentifiers(_toolbar:NSToolbar)->[NSToolbarItem.Identifier]\n//创建toolbar按钮\nfunctoolbar(_toolbar:NSToolbar,itemForItemIdentifieritemIdentifier:NSToolbarItem.Identifier,willBeInsertedIntoToolbarflag:Bool)->NSToolbarItem?\n//可选验证按钮是否激活\nfuncvalidateToolbarItem(_item:NSToolbarItem)->Bool
做好这些后最基础的自定义工具栏就完成啦。
小技巧,扩展`NSToolbarItem.Identifier`就可以在delegate实现中大幅减少匹配id的代码量了(相对于使用string对比)
extensionNSToolbarItem.Identifier{\nstaticletconnect=NSToolbarItem.Identifier("ConnectToolbarItemIdentifier")\nstaticletdisconnect=NSToolbarItem.Identifier("DisconnectToolbarItemIdentifier")\nstaticletnewDirectory=NSToolbarItem.Identifier("NewDirectoryToolbarItemIdentifier")\nstaticletupload=NSToolbarItem.Identifier("UploadToolbarItemIdentifier")\nstaticletdownload=NSToolbarItem.Identifier("DownloadToolbarItemIdentifier")\nstaticletdelete=NSToolbarItem.Identifier("DeleteToolbarItemIdentifier")\nstaticletaddToQueue=NSToolbarItem.Identifier("AddToQueueToolbarItemIdentifier")\nstaticletrefresh=NSToolbarItem.Identifier("RefreshToolbarItemIdentifier")\n}
toolbar基础元素配置一下可以让代码结构更加简洁清晰
\tvartoolbarItemMeta:[NSToolbarItem.Identifier:(label:String,tooltip:String,iconName:String,action:Selector)]{\nget{\nreturn[\n.connect:("Connect","Connecttosftpserver","connect",#selector(connect)),\n.disconnect:("Disconnect","Disconnectfromserver","disconnect",#selector(disconnect)),\n.newDirectory:("NewDirectory","Createnewdirectoryonserver","new_directory",#selector(newDirectory)),\n.download:("Download","Downloadselectedfilesfromserver","download",#selector(download)),\n.upload:("Upload","Uploadfiles","upload",#selector(upload)),\n.delete:("Delete","Deleteselectedfilesfromserver","delete",#selector(delete)),\n.refresh:("Refresh","Refreshcurrentfilelistinthedirecory","refresh",#selector(refresh)),\n]\n}\n}
GosFTP开发过程中遇到的问题
toolbar时隐时现,自定义选项时自己的按钮却都在,该怎么办呢?
暂未明确,可能是系统bug,toolbar的delegate未触发相应函数。
移除storyboard中的toolbar,采用代码动态添加方式,在`Window`的`viewDidLoad`创建一个toolbar并把`delegate`设置为当前`WindowController`的`contentViewController`,然后设置window的toolbar
funcaddToolbar(){\nguardletvc=self.window?.contentViewControlleras?ConnectionViewControllerelse{\nprint("failedtogetcontentviewcontroller")\nreturn\n}\nlettoolbar=NSToolbar(identifier:"ConnectionWindowToolbar")\ntoolbar.allowsUserCustomization=true\ntoolbar.autosavesConfiguration=true\ntoolbar.delegate=vc\nself.window?.toolbar=toolbar\n}
GosFTP项目中使用的代码片段
好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!