{"id":430,"date":"2013-09-16T09:30:31","date_gmt":"2013-09-16T08:30:31","guid":{"rendered":"http:\/\/www.renaudpradenc.com\/?p=430"},"modified":"2013-09-30T11:46:53","modified_gmt":"2013-09-30T10:46:53","slug":"une-fenetre-qui-change-de-view-controller","status":"publish","type":"post","link":"https:\/\/www.renaudpradenc.com\/?p=430","title":{"rendered":"Une fen\u00eatre qui change de view controller"},"content":{"rendered":"<p>Ce billet int\u00e9ressera les programmeurs d\u00e9butants sur le Mac. Je vais ici vous montrer comment passer d&#8217;un panneau \u00e0 un autre, chacun d\u00e9fini dans son propre xib et g\u00e9r\u00e9 par une sous-classe de NSViewController.<\/p>\n<p><a href=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-10.09.24.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-434\" alt=\"Capture d\u2019\u00e9cran 2013-09-16 \u00e0 10.09.24\" src=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-10.09.24.png\" width=\"634\" height=\"490\" srcset=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-10.09.24.png 634w, https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-10.09.24-300x231.png 300w, https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-10.09.24-624x482.png 624w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/a><\/p>\n<h2>Cr\u00e9ation du projet et mise en place de la toolbar<\/h2>\n<p>J&#8217;utilise Xcode 5. Commencez par cr\u00e9er un projet d&#8217;application pour Mac.<\/p>\n<p>J&#8217;ajoute deux images de 32 x 32 pixels, qui serviront d&#8217;ic\u00f4nes pour la toolbar:<\/p>\n<p><a href=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Square.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-435\" alt=\"Square\" src=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Square.png\" width=\"32\" height=\"32\" \/><\/a> <a href=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Circle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-436\" alt=\"Circle\" src=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Circle.png\" width=\"32\" height=\"32\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Xcode a cr\u00e9\u00e9 un premier MainMenu.xib pour votre appli, comprenant une barre des menus et surtout une fen\u00eatre.<\/p>\n<p>Nous allons ajouter une NSToolbar \u00e0 cette fen\u00eatre. Pour cela, il suffit de glisser une toolbar depuis la rubrique Object Library (dans le coin inf\u00e9rieur droit de la fen\u00eatre de Xcode) vers la fen\u00eatre. La toolbar comporte d\u00e9j\u00e0 des ic\u00f4nes. Configurez-la pour qu&#8217;elle pr\u00e9sente deux ic\u00f4nes en son centre:<\/p>\n<p><a href=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.33.27.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-437 alignnone\" alt=\"Capture d\u2019\u00e9cran 2013-09-16 \u00e0 09.33.27\" src=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.33.27.png\" width=\"567\" height=\"508\" srcset=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.33.27.png 567w, https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.33.27-300x268.png 300w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><\/a><\/p>\n<p>Pour cela, supprimez tous les NSToolbarItems pr\u00e9sents, \u00a0glissez de nouveaux items, puis configurez-les.<\/p>\n<p>Maintenant, tirez des actions allant de chaque ic\u00f4ne vers l&#8217;AppDelegate.<\/p>\n<pre class=\"lang:default decode:true\">@interface CEAppDelegate : NSObject &lt;NSApplicationDelegate&gt;\r\n\r\n@property (assign) IBOutlet NSWindow *window;\r\n- (IBAction)item0Pushed:(NSToolbarItem *)sender;\r\n- (IBAction)item1Pushed:(NSToolbarItem *)sender;\r\n\r\n@end<\/pre>\n<h2>\u00a0Cr\u00e9ation des panneaux<\/h2>\n<p>Chaque panneau sera g\u00e9r\u00e9 par un view controller et sera contenu dans un xib.<\/p>\n<p>Commencez par cr\u00e9er une sous-classe de NSViewController (que j&#8217;appelle CECustom0ViewController):<\/p>\n<p><a href=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.42.16.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-438 alignnone\" alt=\"Capture d\u2019\u00e9cran 2013-09-16 \u00e0 09.42.16\" src=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.42.16.png\" width=\"520\" height=\"129\" srcset=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.42.16.png 520w, https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.42.16-300x74.png 300w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/a><\/p>\n<p>\u00c9ditez\u00a0CECustom0ViewController.xib. Ajoutez simplement une NSBox et fixez son titre pour savoir de quel xib il s&#8217;agit:<\/p>\n<p><a href=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.43.50.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-433 alignnone\" alt=\"Capture d\u2019\u00e9cran 2013-09-16 \u00e0 09.43.50\" src=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.43.50.png\" width=\"568\" height=\"374\" srcset=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.43.50.png 568w, https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/Capture-d\u2019\u00e9cran-2013-09-16-\u00e0-09.43.50-300x197.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/a><\/p>\n<p>Cr\u00e9ez une deuxi\u00e8me sous-classe de NSViewController sur le m\u00eame principe (CECustom1ViewController).<\/p>\n<h2>Passage d&#8217;un view controller \u00e0 l&#8217;autre<\/h2>\n<p>Pour g\u00e9rer le changement de view controller, je cr\u00e9\u00e9 une classe CEViewControllerSwitcher, qui h\u00e9rite de NSObject:<\/p>\n<p>CEViewControllerSwitcher.h<\/p>\n<pre class=\"lang:default decode:true\">#import &lt;Foundation\/Foundation.h&gt;\r\n@interface CEViewControllerSwitcher : NSObject\r\n\r\n- (id) initWithParentView:(NSView *)parentView viewControllers:(NSArray *)viewControllers;\r\n@property (nonatomic, assign) NSUInteger viewControllerIndex;\r\n\r\n@end<\/pre>\n<p>CEViewControllerSwitcher.m<\/p>\n<pre class=\"lang:default decode:true\">#import \"CEViewControllerSwitcher.h\"\r\n\r\n@interface CEViewControllerSwitcher ()\r\n\r\n@property (strong) NSView *parentView;\r\n@property (strong) NSArray *viewControllers;\r\n@property (weak) NSViewController *currentViewController;\r\n\r\n@end\r\n\r\n@implementation CEViewControllerSwitcher\r\n\r\n- (id) initWithParentView:(NSView *)parentView viewControllers:(NSArray *)viewControllers\r\n{\r\n\tself = [super init];\r\n\tif (self) {\r\n\t\t_parentView = parentView;\r\n\t\t_viewControllers = viewControllers;\r\n\t\tself.viewControllerIndex = 0;  \/\/ Afficher le premier VC au d\u00e9part\r\n\t}\r\n\treturn self;\r\n}\r\n\r\n- (void) setViewControllerIndex:(NSUInteger)viewControllerIndex\r\n{\r\n\t_viewControllerIndex = viewControllerIndex; \r\n\t\/\/ Retirer le VC pr\u00e9c\u00e9dent\r\n\tif(self.currentViewController)\r\n\t{\r\n\t\t[self.currentViewController.view removeFromSuperview];\r\n\t} \r\n\r\n\t\/\/ Ajouter le nouveau VC\r\n\tself.currentViewController = [self.viewControllers objectAtIndex:viewControllerIndex];\r\n\t[self.parentView addSubview:self.currentViewController.view];\r\n}\r\n\r\n@end<\/pre>\n<h2>Liaison avec l&#8217;AppDelegate<\/h2>\n<p>CEAppDelegate.m<\/p>\n<pre class=\"lang:default decode:true crayon-selected\">#import \"CEAppDelegate.h\"\r\n#import \"CEViewControllerSwitcher.h\"\r\n#import \"CECustom0ViewController.h\"\r\n#import \"CECustom1ViewController.h\"\r\n\r\n@interface CEAppDelegate ()\r\n\r\n@property (strong, nonatomic) CEViewControllerSwitcher *viewControllerSwitcher;\r\n\r\n@end\r\n\r\n@implementation CEAppDelegate\r\n\r\n- (void)applicationDidFinishLaunching:(NSNotification *)aNotification\r\n{\r\n\tNSView *contentView = self.window.contentView;\r\n\tCECustom0ViewController *viewController0 = [[CECustom0ViewController alloc] initWithNibName:@\"CECustom0ViewController\" bundle:nil];\r\n\tCECustom1ViewController *viewController1 = [[CECustom1ViewController alloc] initWithNibName:@\"CECustom1ViewController\" bundle:nil];\r\n\tself.viewControllerSwitcher = [[CEViewControllerSwitcher alloc] initWithParentView:contentView viewControllers:[NSArray arrayWithObjects:viewController0, viewController1, nil]];\r\n}\r\n\r\n- (IBAction)item0Pushed:(NSToolbarItem *)sender {\r\n\tself.viewControllerSwitcher.viewControllerIndex = 0;\r\n}\r\n\r\n- (IBAction)item1Pushed:(NSToolbarItem *)sender {\r\n\tself.viewControllerSwitcher.viewControllerIndex = 1;\r\n}\r\n\r\n@end<\/pre>\n<h2>C&#8217;est termin\u00e9 !<\/h2>\n<p>Voil\u00e0, \u00e7a fonctionne, on passe bien d&#8217;un View Controller \u00e0 l&#8217;autre. Il s&#8217;agissait de la technique de base, je vous laisse r\u00e9gler l&#8217;autolayout des vues pour qu&#8217;elles remplissent bien la contentView de la fen\u00eatre comme on le souhaite.<\/p>\n<p>Le projet Xcode complet:\u00a0<a href=\"https:\/\/www.renaudpradenc.com\/wp-content\/uploads\/2013\/09\/ChangeVues.zip\">ChangeVues<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ce billet int\u00e9ressera les programmeurs d\u00e9butants sur le Mac. Je vais ici vous montrer comment passer d&#8217;un panneau \u00e0 un autre, chacun d\u00e9fini dans son propre xib et g\u00e9r\u00e9 par une sous-classe de NSViewController. Cr\u00e9ation du projet et mise en place de la toolbar J&#8217;utilise Xcode 5. Commencez par cr\u00e9er un projet d&#8217;application pour Mac. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[90],"tags":[94,93,95,96],"class_list":["post-430","post","type-post","status-publish","format-standard","hentry","category-francais","tag-cocoa","tag-mac","tag-nsviewcontroller","tag-nswindow"],"_links":{"self":[{"href":"https:\/\/www.renaudpradenc.com\/index.php?rest_route=\/wp\/v2\/posts\/430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.renaudpradenc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.renaudpradenc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.renaudpradenc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.renaudpradenc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=430"}],"version-history":[{"count":8,"href":"https:\/\/www.renaudpradenc.com\/index.php?rest_route=\/wp\/v2\/posts\/430\/revisions"}],"predecessor-version":[{"id":465,"href":"https:\/\/www.renaudpradenc.com\/index.php?rest_route=\/wp\/v2\/posts\/430\/revisions\/465"}],"wp:attachment":[{"href":"https:\/\/www.renaudpradenc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.renaudpradenc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.renaudpradenc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}