nick shmick
nick shmick

Reputation: 925

how to add a navigation controller to a view that is not the main view?

im a beginner and trying to figure out how to work with nib properly.

I have a HomeViewController.h:

#import <UIKit/UIKit.h>
#import "StackTableViewController.h"

@interface HomeViewController : UIViewController

@property (strong, nonatomic) StackTableViewController *stackViewController;

- (IBAction)goToStack:(id)sender;


@end

HomeViewController.m:

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (id)init {

    self = [super initWithNibName:@"HomeViewController" bundle:nil];
    if (self) {
        //
        _stackViewController = [[StackTableViewController alloc]init];
    }

    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)goToStack:(id)sender {

    //[self.navigationController showViewController:_stackViewController sender:self];
    [self presentViewController:_stackViewController animated:YES completion:nil];
}

As you can see I'm modelling from the HomeViewController to StackTableViewController...

Now it works ok, but I want that StackTableViewController will be embedded in NavigationController...that I can put a cancel button in the top.

This is my StackTableViewController.m:

#import "StackTableViewController.h"

@interface StackTableViewController ()

@property (strong, nonatomic) UINavigationController *navBar;

@end

@implementation StackTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0;
}

What should I add to the viewDidLoad method that will embed the navBar in the tableview?

tnx

Upvotes: 0

Views: 225

Answers (1)

nburk
nburk

Reputation: 22751

There is not need to declare StackTableViewController as a property of HomeViewController, just modify goToStack like so:

- (IBAction)goToStack:(id)sender {
     StackTableViewController *stackViewController = [[StackTableViewController alloc] init]; // shouldnt this get loaded from a NIB though???
    [self presentViewController:stackViewController animated:YES completion:nil];
}

About youer issue with UINavigationController, depending on your setup the UINavigationController is the base for a certain navigation stack within your app, so, if you're just building a simple app without a tab bar or another more complex interface, your UINavigationController might be the rootViewController of your application's main UIWindow (a property of your AppDelegate).

So, what you will have to do to get this setup to work is in application:didFinishLaunchinWithOptions of your AppDelegate, set the application's root window:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    HomeViewController *homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]; // I assume you have a NIB file called HomeViewController
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:homeViewController];
    self.window.rootViewController  = navigationController;
    return YES;
} 

This will have the effect that the HomeViewController that you will see on application startup, is embedded within an instance of UINavigationController, which again is the rootViewController of your whole application.

Then again, you can modify goToStack to use this instance of UINavigationController instead of showing stackViewController modally:

- (IBAction)goToStack:(id)sender {
     StackTableViewController *stackViewController = [[StackTableViewController alloc] init]; // shouldnt this get loaded from a NIB though???
    [self.navigationController pushViewController:stackViewController animated:YES];
}

You can use self.navigationController here because homeViewController is embedded in a UINavigaitonController, so iOS will set this property for you.

Hope that helps! :)

Update: If you don't want to have your HomeViewController embedded within the UINavigationController, just modify goToStack like so:

- (IBAction)goToStack:(id)sender {
    StackTableViewController *stackViewController = [[StackTableViewController alloc] init]; // shouldnt this get loaded from a NIB though???
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:stackViewController];
    [self presentViewController:navigationController animated:YES completion:nil];
}

Upvotes: 1

Related Questions