Can't modal to next viewcontroller - objective-c

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.

Related

return to ViewController after presentViewController

Im trying to return to a specific ViewController in it's current state after going from that ViewController to another using presentViewController.
But when I try to close the other ViewController (with dismissViewController) I get a white screen.
RootViewController *rootViewController
= [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil]
instantiateViewControllerWithIdentifier:#"RootViewController"];
[self presentViewController:rootViewController animated:YES completion:nil];
This isn't an option either because that instantiates a new viewcontroller and I want the old ViewController in its current state.
Do I need to pass the RootViewController as an argument when presenting the other ViewController or is there another option to return to the RootViewController in its current state?
Yes, there is a way to return original screen.
I met just like problem but solved it with following code line
[self dismissViewControllerAnimated:YES completion:nil];
one way to address this is to avoid having one view controller responsible for presenting and dismissing the other one.. what you can do is create a controller of controllers (give it a singelton method).. and have that object basically keep a reference to any view controller you are interested in maintaining its state. That way you wouldn't have worry about what's going on behind the scenes when you dismiss or present a view controller.

Perform Seque of initial view controller inside the App Delegate

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.

presentViewController - view solid black, viewDid___ methods not called

...
SecondViewController *svc = [SecondViewController new];
[self presentViewController:svc animated:YES completion:NULL];
}
This code is exactly the same as what I used in another app, but here I'm using presentViewController rather than presentModalViewController
(completion:NULL makes them effectively identical. Same result, at least.)
Both attempts at creating a modal view are structured the same way. Those lines in the main view, a view controller in the Storyboard, and matching .h and .m files. The only difference is that here I want a programmatic trigger, so it's impossible to drag a segue and be done with it.
I have an object set to recognize a gesture and call the transition method. That's probably what's causing the problem (part of it, at least), but it is necessary.
Using a UIButton would be cheating. No deadline, no shortcuts.
EDIT: NSLog output shows something odd.
2012-04-05 10:41:12.047 MyApp[5962:707] <SecondViewController: 0x1d8c130>
2012-04-05 10:41:12.479 MyApp[5962:707] <SecondViewController: 0x1d8e360>
So I'm doing something stupid again that happens to have a very simple fix, right?
Edit again: presentViewController… was being called more than once. Fixed it. Still black, though.
Back to performSegueWithIdentifier:sender: instead of the much easier presentViewController:animated:completion:
Terminating app due to uncaught exception 'NSInvalidArgumentException", reason: 'Receiver … has no segue with identifier …'
I told it to perform a segue, but there isn't one in the Storyboard (I can't add one, there is no Storyboard Segues section under 'Connections inspector' for the object I'm attempting to use), so it crashes. This is normal behavior.
What I want is to have a modal view without needing to create a segue. I've done it, so I know it's possible.
All I need is a bit of help getting it to work.
.
performSegueWithIdentifier:#"Identifier" sender:nil NSSInvalidArgumentException
presentViewController:viewController animated:YES completion:NULL Empty View
Got it.
Replace the lines in the question with this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard"
bundle:nil];
SecondViewController *viewController =
[storyboard instantiateViewControllerWithIdentifier:#"SecondView"];
[self presentViewController:svc animated:YES completion:NULL];
Credit for this solution goes to IturPablo's self-answered question:
TabBarController, overwriting shouldSelectViewController to do a segue
Are you looking for performSegueWithIdentifier:sender:? The docs seem to match your description:
"you can call this method to trigger a segue programmatically, perhaps
in response to some action that cannot be specified in the storyboard
resource file"

Replacing Storyboard Segue with pushViewController causes strange behaviour

I can't seem to figure this out for the life of me. I have a custom table view cell, in that cell I have a few buttons configured. Each button connects to other view controllers via a storyboard segue. I've recently removed these segues and put a pushViewController method in place. Transition back and forth across the various views works as expected however the destination view controller is not displaying anything! I have some code below as an example.
Buttons have this method set:
[cell.spotButton1 addTarget:self action:#selector(showSpotDetails:) forControlEvents:UIControlEventTouchUpInside];
// etc...
[cell.spotButton4 addTarget:self action:#selector(showSpotDetails:) forControlEvents:UIControlEventTouchUpInside];
// etc...
showSpotDetails Method contains this code:
- (void)showSpotDetails:(id)sender
{
// determine which button (spot) was selected, then use its tag parameter to determine the spot.
UIButton *selectedButton = (UIButton *)sender;
Spot *spot = (Spot *)[spotsArray_ objectAtIndex:selectedButton.tag];
SpotDetails *spotDetails = [[SpotDetails alloc] init];
[spotDetails setSpotDetailsObject:spot];
[self.navigationController pushViewController:spotDetails animated:YES];
}
The details VC does receive the object data.
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"spotDetailsObject %#", spotDetailsObject_.name);
}
The NSLog method below does output the passed object. Also, everything in the details view controller is as it was. Nothing has changed on the details VC. It just does not render anything ever since I removed the segue and added the pushViewController method. Perhaps I am missing something on the pushViewController method? I never really do things this way, I try to always use segues...
Any suggestions?
Welcome to the real world. Previously, the storyboard was a crutch; you were hiding from yourself the true facts about how view controllers work. Now you are trying to throw away that crutch. Good! But now you must learn to walk. :) The key here is this line:
SpotDetails *spotDetails = [[SpotDetails alloc] init];
SpotDetails is a UIViewController subclass. You are not doing anything here that would cause this UIViewController to have a view. Thus you are ending up a with blank generic view! If you want a UIViewController to have a view, you need to give it a view somehow. For example, you could draw the view in a nib called SpotDetails.xib where the File's Owner is an SpotDetails instance. Or you could construct the view's contents in code in your override of viewDidLoad. The details are in the UIViewController documentation, or, even better, read my book which tells you all about how a view controller gets its view:
http://www.apeth.com/iOSBook/ch19.html
The reason this problem didn't arise before is that you drew the view in the same nib as the view controller (i.e. the storyboard file). But when you alloc-init a SpotDetails, that is not the same instance as the one in the storyboard file, so you don't get that view. Thus, one solution could be to load the storyboard and fetch that SpotDetails instance, the one in the storyboard (by calling instantiateViewControllerWithIdentifier:). I explain how to do that here:
http://www.apeth.com/iOSBook/ch19.html#SECsivc

AppDelegate can't add subView

Since appDelegate does not have a view, just window, its hard to figure out how to load a view from it. My problem has for long been that when didReceiveLocalNotification fires i cant load a new view with that event. I have been working around it til the point that i must do something about it. When i tries to addSubview, xcode gives me the error:
Receiver tupe 'UIWindow' for instance messages does not declare a method with selector 'addSubView'
for this: (at [self.window addSubView:view];)
screwLightBulbViewController *view = [screwLightBulbViewController newMyView];
[self.window addSubView:view];
I understand that the appDelegate file does'nt have a addSubview but i want to switch to a particular view when it fires.
I have tried many other ways, like calling a function in screwLightBulbViewController and make a view from that. My function in the viewController now looks like this:
+(id)newMyView
{
UINib *nib = [UINib nibWithNibName:#"MyView" bundle:nil];
NSArray *nibArray = [nib instantiateWithOwner:self options:nil];
screwLightBulbViewController *me = [nibArray objectAtIndex: 0];
return me;
}
any help in any way would be realy appreciated and thanks for you time. :)
It's addSubview not addSubView:. UIWindow is a subclass of UIView.
Adding a view directly as a subview to the window is not usually recommended, so instead you should try and add the view as a subview to the top controller view. If you can spare some time you should look over the view programming guide and view controller programming guide, it will be useful in the future.