Application tried to push a nil view controller on target - ios

I am creating an app without storyboards and I am having trouble moving from when a user clicks on a table view cell to the detail view of that cell.
in the class where I create and handle the table view I have the following code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PersonDetailViewController *detailViewController;
Person *person = [self.playerArray objectAtIndex:indexPath.row];
detailViewController.person = person;
[[self navigationController] pushViewController:detailViewController animated:YES];
}
So I am trying to push a detail view controller on to the screen and then display the user info on that detail screen. I have tried to find a solution to this but, most of the answers I have come across use storyboards, and I am not using storyboards.
in my PersonDetailViewController.m class I have the following code:
#import "PersonDetailViewController.h"
#interface PersonDetailViewController ()
#end
#implementation PersonDetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.personInfo = [[UITextField alloc] initWithFrame:CGRectMake(45, 30, 200, 40)];
_personInfo.backgroundColor = [UIColor whiteColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
I am not sure if this may be a cause for the issue but in my delegate I have the following:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.tableViewController = [[TableViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.tableViewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
I was thinking that maybe it has something to do with creating the navigation controller but I was playing around with it and I do not think it does.

You missed the initialization of the view controller in the tableView:didSelectRowAtIndexPath: method
PersonDetailViewController *detailViewController = [[PersonDetailViewController alloc] init]; // or initWith...

Related

pushViewController lead to black screen, NO STORYBOARD

I see all the other solutions and nothing works for me. When I initiate the application, it works fine, but when I click a table item and push a ViewController, the screen goes black, and keeps black forever. Here is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
ToDoItemViewController *itemViewController = [[ToDoItemViewController alloc] initWithNibName:#"ToDoItemViewController" bundle:[NSBundle mainBundle]];
ToDoItem *itemSelected = [[self toDoItems] objectAtIndex:indexPath.row];
itemViewController.toDoItem = itemSelected;
itemViewController.delegate = self;
[self.navigationController pushViewController:itemViewController animated:YES];
}
The name of the Nib is ok, and the ToDoItemViewController:
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
Thanks in advance,
Remember, I DON'T USE STORYBOARDS and I want to keep that
EDIT
Here is how I launch my first ViewController
ListadoViewController *listadoViewController = [[ListadoViewController alloc] initWithNibName:#"ListadoViewController" bundle:nil];
CustomNavigationController *navCtrl = [[CustomNavigationController alloc] initWithRootViewController:listadoViewController];
self.window.rootViewController = navCtrl;
I resolved it. The problem was that I implement the method "loadView", but I didn't know that that method is implemented by iOS, so I was not calling the super and it wasn't drawing anything.
Thanks all for your responses.

show side bar for iPad like facebook in landscapemode in ios

I am having an iPad app in which I want to implement a Side Bar functionality like we have in facebook app.
I am using this Demo for this.
With this I have successfully implemented the Side Bar functionality and its working well but with that my first view doesn't show me well.
Below is the screenshot.
As you can see from the screenshot, there is a black background and my whole view is not showing in full screen when the app launches.
It should be like below.
Also on clicking the button the Side View is showing like below.
It should be small as I have taken the view size with width = 300 and height = 768.
But it is showing bigger than that.
Here is my code which I change in my appdelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self createEditableCopyOfDatabaseIfNeeded];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:self.viewController];
SlidingViewController *slidingView = [[SlidingViewController alloc]initWithNibName:#"SlidingViewController" bundle:nil];
self.slideMenuController = [[SlideMenuController alloc] initWithCenterViewController:navController];
self.slideMenuController.leftViewController = slidingView;
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
- (IBAction)sideBarPressed:(id)sender
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.slideMenuController.position == TKSlidePositionCenter) {
[appDelegate.slideMenuController presentLeftViewControllerAnimated:YES];
} else {
[appDelegate.slideMenuController presentCenterViewControllerAnimated:YES];
}
}
I want this for my iPad for Landscape mode only.
Please tell me What is wrong here?
I am stuck here for quite a while.
Any help will be appreciated.
Thanks in advance.
So better u need to use UISplitViewController
//in app delegate do like this
//in appDelegate.h file
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, retain) UISplitViewController *splitViewCOntroller;
#end
//in appDelegate.m file
#import "AppDelegate.h"
#import "SplitMasterViewController.h" //create a UITableviewController
#import "SplitViewDetailController.h" //create a UIViewController
#implementation AppDelegate
#synthesize splitViewCOntroller = _splitViewCOntroller;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
SplitMasterViewController *masterController = [[SplitMasterViewController alloc]initWithNibName:#"SplitMasterViewController" bundle:nil]; //this is the master menu controller
UINavigationController *masterNavController = [[UINavigationController alloc]initWithRootViewController:masterController];
SplitViewDetailController *detailViewController = [[SplitViewDetailController alloc]initWithNibName:#"SplitViewDetailController" bundle:nil]; //this is the master detail controller
UINavigationController *detailNavController = [[UINavigationController alloc]initWithRootViewController:detailViewController];
masterController.detailViewController = detailViewController;
_splitViewCOntroller = [[UISplitViewController alloc]init]; //initilise split controller
_splitViewCOntroller.delegate = detailViewController; //set the delegate to detail controller
_splitViewCOntroller.viewControllers = [NSArray arrayWithObjects:masterNavController,detailNavController, nil]; //set the splitview controller
self.window.rootViewController = _splitViewCOntroller; //finally your splitviewcontroller as the root view controller
[self.window makeKeyAndVisible];
return YES;
}
//in SplitMasterViewController.h this must be a table that contains your side bar menu items
#import "ViewController.h" //comment this if it shows any error
#import "SplitViewDetailController.h"
#interface SplitMasterViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
#property (nonatomic, retain) SplitViewDetailController *detailViewController; //to get the detailview from masterMenu controller
#property (nonatomic, retain) NSArray *Names;
#end
// in SplitMasterViewController.m file
#import "SplitMasterViewController.h"
#import "SplitViewDetailController.h"
#interface SplitMasterViewController ()
#end
#implementation SplitMasterViewController
#synthesize Names;
#synthesize detailViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
Names = [[NSArray alloc] initWithObjects:#"apple", #"banana",
#"mango", #"grapes", nil];
[self.tableView selectRowAtIndexPath:
[NSIndexPath indexPathForRow:0 inSection:0]
animated:NO
scrollPosition:UITableViewScrollPositionMiddle];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [Names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// [self configureCell:cell atIndexPath:indexPath];
cell.textLabel.text = [Names objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SplitViewDetailController *detailController = self.detailViewController;
detailController.myLabel.text = [Names objectAtIndex:indexPath.row]; //set the name from the array
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
//in SplitViewDetailController.h
#import "ViewController.h"
#interface SplitViewDetailController : UIViewController<UISplitViewControllerDelegate>
#property (strong, nonatomic) id detailItem;
#property (strong, nonatomic) IBOutlet UILabel *myLabel;
#property (nonatomic, retain) UIBarButtonItem *leftBarButtonItem; //to show a button on left side
#end
//in SplitViewDetailController.m
#import "SplitViewDetailController.h"
#interface SplitViewDetailController ()
{
UIPopoverController *masterPopoverController;
}
#end
#implementation SplitViewDetailController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// _leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Menu" style:UIBarButtonItemStyleBordered target:self action:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//these are the call back to detail view controller to hide or show the button
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
_leftBarButtonItem = barButtonItem;
_leftBarButtonItem.style = UIBarButtonItemStyleBordered;
_leftBarButtonItem.title = #"Menu";
[self.navigationItem setLeftBarButtonItem:_leftBarButtonItem animated:YES];
}
// Called when the view is shown again in the split view, invalidating the button
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
}
#end
I successfully implemented this side bar menu functionality with this Demo code and its working perfectly well in my case.
Hope it works to someone else also.
Thanks for your help and suggestions.
i am using this and it's good
https://github.com/ECSlidingViewController/ECSlidingViewController

Navigation Controller not work after using Present model controller

I have an view with 3 buttons in that whenver user clicked on 1st button click it goes to seconViewController
Used this code.
PerformViewController * pvc=[[PerformViewController alloc]initWithNibName:#"PerformViewController" bundle:nil];
[self presentViewController:pvc animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
and another two buttons i used the [self.navigationController pushViewController:gmavc animated:YES];
it works fine but when used tapped on 1st button clicked present the secondViewController.
and whne back to it's viw controller i used this code
ViewController *vc=[[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
After back to parant view controller my another two button push navigation controller not working not why this happening.
#interface MainViewController : UIViewController
{
}
- (IBAction)presentClicked:(id)sender;
- (IBAction)nevigateClicked:(id)sender;
#end
#import "MainViewController.h"
#import "PresViewController.h"
#import "NavViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
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)presentClicked:(id)sender {
PresViewController *preView = [[PresViewController alloc]initWithNibName:#"PresViewController" bundle:nil];
[self presentModalViewController:preView animated:YES];
}
- (IBAction)nevigateClicked:(id)sender {
NavViewController *navView = [[NavViewController alloc]initWithNibName:#"NavViewController" bundle:nil];
[self.navigationController pushViewController:navView animated:YES];
}
#end
Define #property (strong, nonatomic) UINavigationController *navigationCntr; in YourAppDelegate
Use following code for presenting the controller
YourAppDelegate *del = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
YourViewController *viewCntrl = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:[NSBundle mainBundle]];
UINavigationController *navContrl = [[UINavigationController alloc] initWithRootViewController:viewCntrl];
navContrl.navigationBarHidden = YES;
[del.navigationCntr presentModalViewController:navContrl animated:YES];
For dismissing
YourAppDelegate *delegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.navigationCntr dismissModalViewControllerAnimated:YES];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MainViewController *mainView = [[MainViewController alloc]initWithNibName:#"MainViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:mainView];
self.window.rootViewController = nav;
return YES;
}
#import <UIKit/UIKit.h>
#interface PresViewController : UIViewController
{
}
- (IBAction)hideClicked:(id)sender;
#end
#import "PresViewController.h"
#interface PresViewController ()
#end
#implementation PresViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
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)hideClicked:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
#import <UIKit/UIKit.h>
#interface NavViewController : UIViewController
#end
#import "NavViewController.h"
#interface NavViewController ()
#end
#implementation NavViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
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.
}
#end

Why does my pushViewController not work

When I hit an element in my tableView it comes up with an error. I have checked all of the connections but still cant work it out. Here is my code
#import "CGViewController.h"
#import "CGAppDelegate.h"
#import "sls.h"
#import "patientController.h"
#interface CGViewController ()
#end
#implementation CGViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Patients";
CGAppDelegate *delegate =
(CGAppDelegate *)[[UIApplication sharedApplication] delegate];
patients = delegate.patients;
self.navigationItem.rightBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)];
self.navigationItem.leftBarButtonItem = addButtonItem;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark UITableViewDataSource Methods
- (UITableViewCell *)tableView:(UITableView *)tv
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"cell"];
if( nil == cell) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"cell"];
}
if (indexPath.row < patients.count) {
sls *thissls = [patients objectAtIndex:indexPath.row];
cell.textLabel.text = thissls.patientName;
}
else {
cell.textLabel.text = #"";
cell.textLabel.textColor = [UIColor lightGrayColor];
}
return cell;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
if ( editing != self.editing) {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
NSArray *indexes =
[NSArray arrayWithObject:
[NSIndexPath indexPathForRow:patients.count inSection:0]];
if ( editing == YES ) {
[self.tableView insertRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
} else {
[self.tableView deleteRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
}
}
[self.tableView reloadData];
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row < patients.count ) {
return UITableViewCellEditingStyleDelete;
}
else {
return UITableViewCellEditingStyleNone;
}
}
- (NSInteger)tableView:
(UITableView *)tv numberOfRowsInSection:
(NSInteger)section {
NSInteger count = patients.count;
if (self.editing) {
count = count + 1;
}
return count;
}
-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
CGAppDelegate *delegate =
(CGAppDelegate *)[[UIApplication sharedApplication] delegate];
patientController *patient = [[patientController alloc] init];
[delegate.navController pushViewController:patient animated: YES];
[tv deselectRowAtIndexPath:indexPath animated: YES];
}
-(void) tableView:(UITableView *)tv
commitEditingStyle:(UITableViewCellEditingStyle)editing forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editing == UITableViewCellEditingStyleDelete) {
[patients removeObjectAtIndex:indexPath.row];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationLeft];
}
}
#end
Any ideas on what is wrong if you know please can you help as soon as possible as i am on a deadline
Thanks in advance.
The error says Thread 1:signal SIGABRT
The error is in main.m
#import <UIKit/UIKit.h>
#import "CGAppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([CGAppDelegate class]));
}
}
it says that the error is here
patientController *patient = [[patientController alloc] init];
here is my patientController.h file
#import <UIKit/UIKit.h>
#interface patientController : UIViewController {
NSIndexPath *index;
IBOutlet UIImageView * pictureView;
IBOutlet UILabel * descriptionView1;
IBOutlet UILabel * descriptionView2;
IBOutlet UILabel * descriptionView3;
}
- (id)initwithIndexPath:(NSIndexPath *)indexPath;
#end
and my .m file
#import "patientController.h"
#import "CGAppDelegate.h"
#import "sls.h"
#interface patientController ()
#end
#implementation patientController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
CGAppDelegate *delegate =
(CGAppDelegate *)[[UIApplication sharedApplication] delegate];
sls *thissls = [delegate.patients objectAtIndex:index.row];
self.title = thissls.patientName;
self->descriptionView1.text = thissls.patientName;
self->descriptionView2.text = thissls.surnameName;
self->descriptionView3.text = thissls.dateOfBirth;
self->pictureView.image = thissls.patientImage;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id)initwithIndexPath:(NSIndexPath *)indexPath {
if ( (self == [super init]) ) {
index = indexPath;
}
return self;
}
#end
Please can anyone at all help
Use:
[self.navigationController pushViewController:patient animated: YES];
If you are currently in a navigationController (which you have to in order to push a new ViewController), then you can push your new controller by just calling these lines
-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
patientController *patient = [[patientController alloc] init];
[self.navigationController pushViewController:patient animated: YES];
}
Use
[self.navController pushViewController:patient animated: YES];
Every viewcontroller has its navigation controller reference in itself,no need to call the application delegate to invoke it[The navigation controller Must be initialised and all the viewcontrollers must be pushed into the navigation stack until this one to make it work]
So use this to setup the navigation controller in the appdelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Note : this is an example set like this with your viewcontroller instance
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window makeKeyAndVisible];
return YES;
}
To check where your exception is raised, in Xcode, in the breakpoints navigation tab click on the plus button in the bottom-left corner and add an Exceptions Breakpoint. Then run the app from Xcode. The next time an exception is raised, as in your case, the execution of your app will stop and Xcode will point at the line of code where the exception is raised.
Anyway, to make you understand how navigationControllers work:
UINavigationController is a UIViewController that controls a stack of other UIViewController instances. This other view controllers are said to be in the 'navigation stack'. The top item in the navigation stack is the view controller whose view you see at a given time onto the screen. You can push (present) or pop (dismiss) a view controller on/from the navigation stack.
A UINavigationController is initialized with a 'rootViewController' which is going to be it's first view controller onto the stack and therefore the first you see when you add the navigationController onto the screen (e.g. by setting it as the appDelegate window's rootViewController). Each UIViewController on the navigation stack references it's navigationController through the property 'navigationController'.
At any given point, you can use the UINavigationController methods pushViewController:animated: and popViewControllerAnimated: or popToViewController:animated: or popToRootViewControllerAnimated: to add or remove viewControllers from the navigationStack. To see the results of this methods, of course, you should have the navigationController on screen.
For more informations refer to the UINavigationController Class Reference.

UINavigationController State Restoration (without Storyboards)

I've been toying around with state restoration. In the code below, the scroll position of the UITableViewController gets restored, however, if I were to tap through into the detail view (pushing an instance of MyViewController onto the navigation stack), when the app restarts, it always returns to the first view controller in the navigation stack (i.e. MyTableViewController). Would somebody be able to help me restore to the correct view controller (i.e. MyOtherViewController)?
AppDelegate.m
- (BOOL)launchWithOptions:(NSDictionary *)launchOptions
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MyTableViewController *table = [[MyTableViewController alloc] initWithStyle:UITableViewStylePlain];
table.depth = 0;
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:table];
navCon.restorationIdentifier = #"navigationController";
self.window.rootViewController = navCon;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
});
return YES;
}
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return [self launchWithOptions:launchOptions];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return [self launchWithOptions:launchOptions];
}
MyTableViewController.m
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if(self)
{
self.restorationIdentifier = #"master";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Master";
self.tableView.restorationIdentifier = #"masterView";
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:#"Section %d", section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%d", indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyOtherViewController *vc = [[MyOtherViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}
MyOtherViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.restorationIdentifier = #"detail";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Detail";
self.view.backgroundColor = [UIColor redColor];
self.view.restorationIdentifier = #"detailView";
}
Because you are creating your detail view controller in code, and not instantiating it from a Storyboard, you need to implement a restoration class, so the system restoration process knows how to create the detail view controller.
A restoration class is really just a factory which knows how to create a specific view controller from a restoration path. You don't actually have to create a separate class for this, we can just handle it in MyOtherViewController:
in MyOtherViewController.h, implement the protocol: UIViewControllerRestoration
Then when you set the restorationID in MyOtherViewController.m, also set the restoration class:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.restorationIdentifier = #"detail";
self.restorationClass = [self class]; //SET THE RESTORATION CLASS
}
return self;
}
You then need to implement this method in the restoration class (MyOtherViewController.m)
+(UIViewController *) viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
//At a minimum, just create an instance of the correct class.
return [[MyOtherViewController alloc] initWithNibName:nil bundle:nil];
}
That gets you as far as the restoration subsystem being able to create and push the Detail View controller onto the Navigation Stack. (What you initially asked.)
Extending the Example to handle state:
In a real app, you'd need to both save the state and handle restoring it... I added the following property definition to MyOtherViewController to simulate this:
#property (nonatomic, strong) NSString *selectedRecordId;
And I also modified MyOtherViewContoller's viewDidLoad method to set the Detail title to whatever record Id the user selected:
- (void)viewDidLoad
{
[super viewDidLoad];
// self.title = #"Detail";
self.title = self.selectedRecordId;
self.view.backgroundColor = [UIColor redColor];
self.view.restorationIdentifier = #"detailView";
}
To make the example work, I set selectedRecordId when initially pushing the Detail View from MyTableViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyOtherViewController *vc = [[MyOtherViewController alloc] initWithNibName:nil bundle:nil];
vc.selectedRecordId = [NSString stringWithFormat:#"Section:%d, Row:%d", indexPath.section, indexPath.row];
[self.navigationController pushViewController:vc animated:YES];
}
The other missing piece are the methods in MyOtherViewController, your detail view, to save the state of Detail controller.
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.selectedRecordId forKey:#"selectedRecordId"];
[super encodeRestorableStateWithCoder:coder];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
self.selectedRecordId = [coder decodeObjectForKey:#"selectedRecordId"];
[super decodeRestorableStateWithCoder:coder];
}
Now this actually doesn't quite work yet. This is because the "ViewDidLoad" method is called on restoration before the decoreRestorablStateWithCoder method is. Hence the title doesn't get set before the view is displayed. To fix this, we handle either set the title for the Detail view controller in viewWillAppear: , or we can modify the viewControllerWithRestorationIdentifierPath method to restore the state when it creates the class like this:
+(UIViewController *) viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
MyOtherViewController *controller = [[self alloc] initWithNibName:nil bundle:nil];
controller.selectedRecordId = [coder decodeObjectForKey:#"selectedRecordId"];
return controller;
}
That's it.
A few other notes...
i presume you did the following in your App Delegate even though i didn't see the code?
-(BOOL) application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
- (BOOL) application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
I saw a bit of flakiness when running the demo code in the simulator with it correctly preserving the Scroll offset. It seemed to work occasionally for me, but not all the time. I suspect this may be because really restoration of MyTableViewController should also use a restorationClass approach, since it's instantiated in code. However I haven't tried that out yet.
My testing method, was to put the app in the background by returning to springboard (required for the app state to be saved.) , then relaunching the app from within XCode to simulate a clean start.
the only problem with your code is navigation controller
these two lines
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:table];
navCon.restorationIdentifier = #"navigationController";
i dont know why navigation controller never get its restoration class. i had the same problem. i found some alternative solution to create a navigation controller stand alone in storyboard and use that.
here is code
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle:nil];
UINavigationController* navigationController = [storyboard
instantiateViewControllerWithIdentifier:
#"UINavigationController"];
navigationController.viewControllers = #[myController];
it will work fine .
just follow this http://petr-pavlik.squarespace.com/blog/2013/6/16/stare-restoration-without-storyboard
Since you are doing this in code and not via Storyboard, you will need to provide not only a restorationIdentifier but also a restorationClass to you detail view controller.
You could leave it unassigned in which case -(UIViewController *)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder gets called on the application delegate to make the object (view controller with a restorationIdentifier but no restorationClass).
Try the following (please note the UIViewControllerRestoration protocol):
#interface MyViewController ()<UIViewControllerRestoration>
#end
#implementation
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
}
if( [self respondsToSelector:#selector(restorationIdentifier)] ){
self.restorationIdentifier = #"DetailViewController";
self.restorationClass = [self class];
}
return self;
}
#pragma mark - State restoration
+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
UIViewController *viewController = [[self alloc] init];
return viewController;
}
#end
Also note that this is a very simple example, usually you might have a lot more interesting things going on in -viewControllerWithRestorationIdentifierPath:coder:

Resources