I'm learning how to develop for iOS and I want to begin with nib's before storyboard to get a better understanding.
I just set up 3 view controllers:
HomeVIewController
StackTableViewController
CreateViewController
The HomeVIewController.h:
#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController
- (IBAction)goToStack:(id)sender;
- (IBAction)goToCreate:(id)sender;
@end
The HomeVIewController.m:
#import "HomeViewController.h"
#import "StackTableViewController.h"
#import "CreateViewController.h"
@interface HomeViewController ()
@property (strong, nonatomic) UINavigationController *navigationController;
@end
@implementation HomeViewController
- (id)init {
self = [super initWithNibName:@"HomeViewController" bundle:nil];
if (self) {
// Do something
}
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 {
StackTableViewController *stackViewController = [[StackTableViewController alloc] initWithNibName:@"StackTableViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:stackViewController];
[self presentViewController:self.navigationController animated:YES completion:nil];
}
- (IBAction)goToCreate:(id)sender {
CreateViewController *createViewController = [[CreateViewController alloc]initWithNibName:@"CreateViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:createViewController];
[self presentViewController:self.navigationController animated:YES completion:nil];
}
This view controller has a nib file with a view and 3 buttons at the bottom of the page that I use to navigate.
The StackTableViewController.h:
#import <UIKit/UIKit.h>
@interface StackTableViewController : UITableViewController
@end
The StackTableViewController.m:
#import "StackTableViewController.h"
@interface StackTableViewController ()
@property (strong, nonatomic) UINavigationController *navBar;
@end
@implementation StackTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
self.navigationItem.leftBarButtonItem = anotherButton;
}
- (void)cancel {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
This table view controller have a nib file with a table view.
The CreateViewController.h:
#import <UIKit/UIKit.h>
@interface CreateViewController : UIViewController
@end
The CreateViewController.m:
#import "CreateViewController.h"
@interface CreateViewController ()
@property (strong, nonatomic) IBOutlet UITextView *myTextView;
@end
@implementation CreateViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButton;
}
- (void)viewWillAppear:(BOOL)animated {
[self.myTextView becomeFirstResponder];
}
- (void)cancel {
[self.myTextView resignFirstResponder];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
This view controller also have a nib file with a UITextView
and a cancel button to go back to the homepage.
I didn't use any data yet, and just wanted to know if I'm doing the transitions right since I'm very new to this.
Something feels weird about the way I use the navigation controller.
I'd love to get some feedback on this, especially some stuff in the view hierarchy of the UIViewController
lifecycle.
nib screenshots:
home:
stack:
create:
modal
, as there are no modal transitions here. – nhgrif Dec 12 '14 at 21:59