Perform Seque of initial view controller inside the App Delegate - objective-c

ive got a navigation controller with a simple view as its rootsview. it has a button connected via a seque to a second view controller. The seque cause the second view controller to show up modal. If a condition isnt true i directly want to show up this second view controller after application launched.
I tried out a lot of code but it just wont work. Here is a screenshot of my scene :
Any Ideas how to do this?
I tried the following :
UIStoryboard *storybord = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController *vc = [storybord instantiateInitialViewController];
[[vc.viewControllers objectAtIndex:0] performSegueWithIdentifier:#"simple" sender:self];
but i got the following error message :
Attempt to present <UINavigationController: 0x71b2ab0> on <UINavigationController: 0x71b12c0> whose view is not in the window hierarchy!

I assume you're trying that sample code from app delegate? You need to make sure you don't try to perform the segue until after the first view is loaded. Coincidentally, if you wait until then, its much easier as all you need to do the performSegueWithIdentifier. Thus, in the viewDidLoad of the first view controller, e.g.:
if (bShouldAutomaticallyPerformSegueToSecondVC)
[self performSegueWithIdentifier:#"simple" sender:self];
Obviously, replace bShouldAutomaticallyPerformSegueToSecondVC with whatever logic is appropriate to determine when you automatically want to go to the second view controller.

Related

How to navigate from subview to other view controller?

Assume that I am having one view controller called MainPage(This is rootViewController).
In that view controller , I am adding one sub view.
See Below :
FirstPage1=[[FirstPage alloc] initWithNibName:#"FirstPage" bundle:[NSBundle mainBundle]];
[self.view addSubview:FirstPage1.view];
In that subview , I need to navigate to next page.
so that i have used presentViewController like below code but its doen't worked well.
See Below Code:
[self.view.window.rootViewController presentViewController:AnotherViewController animated:YES completion:nil];
It works well on first time but if I called for next time , I got below error.
Error :
whose view is not in the window hierarchy!
Having separate view controllers being linked together is a good use for UINavigationController. Just make a UINavigationController and add its view to self.view on your example. Then, do [navController pushViewController: animated:] for each view as needed. Or pop the last view controller, then push the new one if you don't want old pages to be accessible. From how your project seems to be set up, that's the easiest way.

storyboard instantiateViewControllerWithIdentifier:#"myIdentifier" shows blank view controller

I am making an application which shows a UILocalNotification and when user clicks on the notification, the app will show a new ViewController (designed in Storyboard) which doesn't have any segue from anywhere in my application.
To detect if the user has clicked on the notification I am doing the following in didReceiveLocalNotification :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *checkListVC = [storyboard instantiateViewControllerWithIdentifier:#"MyCheckListVC"];
self.window.rootViewController = checkListVC;
So when user clicks on the notification, above code initiatizes my view controller (lets call it CheckListVC) which has the identifier "MyCheckListVC".
This CheckListVC has tableView, whose delegate and dataSource methods get called (numberOfRowsInSection & cellForRowAtIndexPath) when the view controller is initialized. But the problem is the simulator shows blank screen when CheckListVC is initialized instead of showing tableView.
I am not able to debug it because I don't even know where the problem is. The view controller gets initialized properly and all the delegate/dataSource methods are getting called properly, but the screen goes blank.
I think you forgot to assign identifier to UIStoryboard..
1.select storyboard
2.assign the identifier name in "MyCheckListVC" in Identity inspector ..
check this image for clarity..
follow these steps and try.. i think this will work.. :)

Can't modal to next viewcontroller

I feel like I am just missing something simple here. I am trying to modal to my next view controller. I imported the next view controller in my first .m file first. After I did that I wrote this code
CRHViewController *nextViewController = [[CRHViewController alloc]init];
[self presentModalViewController:nextViewController animated:NO];
Also, I am working with storyboard and not nibs.
What happens when I run this is as soon as it goes to modal to the next viewcontroller it just goes black.
Am I missing something simple? Does anyone have an suggestions to fix this problem?
Probably you're not initialising it correctly. For testing purposes I would try to display CRHViewController as first root viewController from AppDelegate and find if it is initialising at all. Then check if it gets to its methods:
initWithNibName
loadView
awakeFromNib
viewDidLoad
viewWillAppear
viewDidAppear
In this order. Its 90% certain that one of them fails. Check if it gets to every method you have implemented correctly in this order.
If your CRHViewController is in your storyboard, then you should be instantiating it with:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"yourStoryboardName" bundle:nil];
[storyboard instantiateViewControllerWithIdentifier:#"myIdentifier"];
You should give your view controller an identifier in IB to pass in as the identifier parameter in the above method.

pushViewController iphone not working

i am unable to get the pushViewController to work on a View Based Application on the iPhone. On my 'ProjectViewController' i have a IBAction with the following code :
-(IBAction)switchAugmented
{
ARViewController *viewController = [[ARViewController alloc] initWithDelegate:self];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
}
When i run the program and press ibaction nothing happens. Besides that statement above do i need to do anything else to make the view appear? what am i missing?
(...) on a View Based Application (...)
You just have no UINavController! Try to embed your main view in UINavigationController and everything will start working.
Double check to make sure that the button you are pressing is connected to the right method in interface builder. Also try putting an NSLog statement in the switchAugmented to see if the method is getting called.
You also have to check and see if you have a UINavigationController instance, otherwise you won't be able to push a new view controller.
You won't be able to push a new view controller in a View Based Project. You need to create a Navigation Based Project or add an instance of the UINavigationController in your Main.nib (if your using a nib file) only then will the push view controller will work

How do I make a reusable XIB with it's own UIViewController?

I am trying to create a reusable "picker". It is basically like a keypad of a phone. Since I will be using this a lot in my iPhone app, I have been frustrated with trying to make it appear.
It is in its own XIB file and has its own UIViewController subclass as the FileOwner.
However, when I instantiate this with:
MonthPickerViewController *mpvc
= [[MonthPickerViewController alloc] initWithNibName:#"MonthPicker"
bundle:nil];
Nothing happens on the screen. Yet is does fire the -viewWillAppear methods, etc.
So, what am I doing wrong either in code or in InterfaceBuilder that is preventing my view to appear?
Are you pushing the view controller?
[[self navigationController] pushViewController:mpvc animated:YES];
Or are you adding the view controller's view as a subView of your current view?
First, make sure you've hooked everything up right inside Interface Builder. An easy gotcha is to forget to connect the View object up to the view outlet of your UIViewController subclass.
Then, as Adam says, you need to actually display the view. Assuming you're doing this inside the code of another view controller, you'd need something like the following if you just wanted the new view to appear ontop of your current view:
[self.view addSubview:mpvc.view];
Of if you are using a navigation controller to stack views:-
[[self navigationController] pushViewController:mpvc animated:YES];