Mac OS X Cocoa multiview application navigation

后端 未结 3 1954
一生所求
一生所求 2020-12-28 23:50

I\'ve already spent 2 full days trying to figure out how to use NSViewControllers in order to create a multiview application.

Here is what I do.

I have 2 Vie

3条回答
  •  梦谈多话
    2020-12-29 00:34

    here is the solution to my question then.

    Here's the code for the AppDelegate.h:

      //  AppDelegate.h
    
    
     #import 
     #import "FirstViewController.h"
     #import "SecondViewController.h"
    
     //We need to declare the AppDelegate class as being the delegate for both 
     //FirstViewController and SecondViewController
    
     @interface AppDelegate : NSObject 
    
     @property (strong, nonatomic) NSWindow *window;
     @property (strong) FirstViewController *firstViewController;
     @property (strong) SecondViewController *secondViewController;
    
     -(void) goToSecondView;
     -(void) goToFirstView;
    
     @end
    

    Now, here's the AppDelegate.m:

     //  AppDelegate.m
    
     #import "AppDelegate.h"
    
    
     @implementation AppDelegate
    
     @synthesize window = _window;
     @synthesize firstViewController;
     @synthesize secondViewController;
    
     -(void) awakeFromNib {
    
     [self goToFirstView];
     self.firstViewController.delegate = self;
    
     }
    
    
     -(void) goToSecondView {
    
            if (self.secondViewController ==nil) {
                self.secondViewController =[[SecondViewController alloc] 
                initWithNibName:@"SecondViewController" bundle:nil];
            }
    
            self.window.contentView = [self.secondViewController view];
      }
    
     -(void) goToFirstView {
    
     if (self.firstViewController ==nil) {
         self.firstViewController =[[FirstViewController alloc] 
         initWithNibName:@"FirstViewController" bundle:nil];
       }
    
        self.window.contentView = [self.firstViewController view];
    
     }
    
     @end
    

    Next we need to set delegates in the FirstViewController and the SecondViewController

    //  FirstViewController.h
    
    #import 
    #import "SecondViewController.h"
    
    //We declare the delegation protocole:
    
    @protocol FirstViewControllerDelegate 
    
    -(void)goToSecondView;
    
    @end
    
    @interface FirstViewController : NSViewController
    
    - (IBAction)firstViewControllerButtonClicked:(id)sender;
    
    @property (nonatomic, strong) id  delegate;
    
    @end
    

    And here is the FirstViewController.m:

     //  FirstViewController.m
    
     #import "FirstViewController.h"
    
     @implementation FirstViewController
     @synthesize delegate;
    
     - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
       self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
       if (self) {
    
        self.delegate = [NSApp delegate];
    
        }
    
       return self;
     }
    
     - (IBAction)firstViewControllerButtonClicked:(id)sender {
    
       NSLog(@"button from first View Controller clicked");
    
       if ([self.delegate respondsToSelector:@selector(goToSecondView)]) {
        [self.delegate goToSecondView];
       }
     }
    
     @end
    

    Now, same thing for the SecondViewController:

     //  SecondViewController.h
    
     #import 
    
     @protocol SecondViewControllerDelegate 
    
     -(void)goToFirstView;
    
     @end
    
     @interface SecondViewController : NSViewController
    
     @property (nonatomic, strong) id  delegate;
    
     - (IBAction)goToFirstViewControllerButtonClicked:(id)sender;
    
     @end
    

    And here's the SecondViewController.m:

      //  SecondViewController.m
    
      #import "SecondViewController.h"
    
      @interface SecondViewController ()
    
      @end
    
      @implementation SecondViewController
    
      - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
    
          self.delegate = [NSApp delegate];
       }
    
        return self;
      }
    
      - (IBAction)goToFirstViewControllerButtonClicked:(id)sender {
    
        NSLog(@"button from Second View Controller clicked");
    
        if ([self.delegate respondsToSelector:@selector(goToFirstView)]) {
        [self.delegate goToFirstView];
      }
    
     }
     @end
    

    Well, I guess this code may be improved and if you have any suggestion, feel free to let me know. Hope it will help others.

提交回复
热议问题