Why does self.navigationController become nil? - objective-c

I have this code:
ViewController2 *childView = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
[self.navigationController pushViewController:childView animated:YES];
I the first line, self.navigationController has a value.
On the second line, its value becomes 0. Why is that?

Unless you have overridden initWithNibName:bundle: in ViewController2 to do something truly unfortunate, there's extremely little chance that what you're saying can be true.
If you alter your code with this following assertion, does the assertion really trigger?
UINavigationController* navigationController = self.navigationController;
ViewController2 *childView = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
NSAssert(navigationController == self.navigationController, #"I was removed from the navigation controller!");
[self.navigationController pushViewController:childView animated:YES];

Related

How to drill down and show detail uitableview with splitview without storyboard

I implemented a splitview using 2 UITableViews and a UIViewController. Showing both on the screen with their own data is working fine.
Now in my DidSelectRowForIndexPath I did the following:
DetailViewController *nextController = [[DetailViewController alloc] initWithStyle:UITableViewStylePlain];
NSMutableArray *objects;
objects = [[NSMutableArray alloc] initWithObjects:#"A", #"B", nil];
nextController.title = #"Filter";
[nextController SetArray:objects];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:nextController];
[self presentModalViewController:nc animated:YES];
//used this before [self.navigationController pushViewController:nextController animated:YES];
[FilterView deselectRowAtIndexPath:indexPath animated:YES];
Maybe you know a better method as what I did to present the nextController
The nextController is always showing up from the bottom and moving to the top. How can I accomplish this default sliding animation where the detail view comes into the view from the right side?
If you have started the app from the master/detail template while creating the project. Then it will have the navigation controller set automatically in the appDelegate didFinishLaunching method so in the didSelect methods you have to just use
`[self.navigationController pushViewController:vc animated:YES];`
instead of
[self presentModalViewController:nc animated:YES];
Here is a sample code which uses split view with navigation to and from root view.
First you need to confirm that you have UINavigationController at base then you can pushViewController:nextController
[FilterView deselectRowAtIndexPath:indexPath animated:YES];
DetailViewController *nextController = [[DetailViewController alloc] initWithStyle:UITableViewStylePlain];
NSMutableArray *objects;
objects = [[NSMutableArray alloc] initWithObjects:#"A", #"B", nil];
nextController.title = #"Filter";
[nextController SetArray:objects];
[self.navigationController pushViewController:nextController animated:YES];

UINavigationController as ModalView

I have in my app class UINavigationController (with NIB) and I want to open this as a ModalView.
I do that so: (ColorInfo is just this UINavigationController class)
ColorInfo *InfoScreen = [[ColorInfo alloc] initWithNibName:#"ColorInfo" bundle:[NSBundle mainBundle]];
[InfoScreen setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:InfoScreen animated:YES];
It displays me an empty UINavigtionController element. Tell me please how could I open ColorInfo (as UINavigationController) in ModalView, where I will be able to navigate through?
I thought I can complete ColorInfo with all pushViewController methods and then open it over another screen in ModalView.
Tell me how to reach this effect.
From what you've stated you've inherited UINavigationController in ColorInfo which is not what you want. You want ColorInfo to be the root view of a UINavigationController like so
ColorInfo *InfoScreen = [[ColorInfo alloc] initWithNibName:#"ColorInfo" bundle:[NSBundle mainBundle]];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:InfoScreen];
[self presentModalViewController:nav animated:YES];
I recommend you read the navigation controller documentation for further clarity.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
Use this with InfoScreen controller and DON'T subclass UINavigationController ...
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:InfoScreen];
[navController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:InfoScreen animated:YES];

ModalViewController with embedded nav controller - Unable to dismiss

I present a modalViewController that is actually a navigation controller with one view, and a custom navigation bar. The modal view appears fine as expected, but when I attempt to remove it from view using [self dismissModalViewControllerAnimated:YES], I am hitting a "-[UINavigationController modalViewController]: message sent to deallocated instance". Can't seem to figure this out. Any ideas?
Instantiating the ModalViewController:
// Make a navigation controller and add the view inside it
MyViewController *evc=[[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
//UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:evc];
UINib *nib = [UINib nibWithNibName:#"UINavigationBarWithBackgroundImage" bundle:nil];
UINavigationController *nvc = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
[nvc setViewControllers:[NSArray arrayWithObject:evc]];
evc.delegate=self;
[evc release];
[self presentModalViewController:nvc animated:YES];
[nvc release];
and trying to remove it. This is where the error comes in:
[self dismissModalViewControllerAnimated:YES];
Not sure about this, but try it anyway:
Remove
[nvc release]
and see if
[self dismissModalViewControllerAnimated:YES];
now works.
Is there a reason you are loading two seperate nibs to show this modal? You do not need to load a nib containing a navigation controller to get this working.
Try something like this:
// Make a navigation controller and add the view inside it
MyViewController *evc= [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:evc];
evc.delegate=self;
[self presentModalViewController:navController animated:YES];
[evc release];
[navController release];

How to access navigationController from popoverController?

I am trying to push a new viewController into the navigationController from a popoverController but it doesnt work for me.
This is how I call to the popoverController:
PdfDetailViewController *vc=[[PdfDetailViewController alloc] initWithNibName:#"PdfDetailViewController" bundle:nil];
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 400, 280)];
vc.contentSizeForViewInPopover = CGSizeMake(700, 390);
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:vc];
[self.popoverController presentPopoverFromRect:cell.frame inView:self.tableView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[popoverView release];
[popoverContent release];
This is my code from the popoverController:
CommentsViewController *commentsViewController = [[CommentsViewController alloc] init];
commentsViewController.index = PdfID;
[self.parentViewController.navigationController pushViewController:commentsViewController animated:YES];
[commentsViewController release];
Nothing happen
Please help me... thank you!
UIPopoverController doesn't have a NavigationController unless you add one yourself.
For example
MyViewController *myViewController =
[[MyViewController alloc]
initWithNibName:#"MuViewController"
bundle:[NSBundle mainBundle]];
UINavigationController *navController =
[[UINavigationController alloc]
initWithRootViewController:myViewController];
UIPopoverController *popover =
[[UIPopoverController alloc]
initWithContentViewController:navController];
You should start by cleaning up your code so that it makes sense. Right now you are creating a UIView called popoverView for no reason and then just throwing it away. You're just confusing yourself and it's harder to understand what you are trying to accomplish when you show meaningles code.
Once you've done that, what are you trying to accomplish? Is the problem that this line doesn't work: self.parentViewController.navigationController pushViewController:? If so, I would suggest pulling it apart and logging to make sure that parentViewController and navigationController are the objects you think they are.

Cannot get Twitter-OAuth-iPhone to work

demo works as expected, no problems. But now I'm trying to integrate it into my project. I use no xib-s, code only:
OAuthTwitterDemoViewController *vc = [[OAuthTwitterDemoViewController alloc] init];
[[UIApplication sharedApplication].keyWindow addSubview:vc.view];
[vc release];
it compiles and runs with no errors, but the actual OAuthTwitterDemoViewController is never visible. I've also tried it from a custom viewController with [self.view addSubview:vc.view]
What's the secret??
Update:
OK, here's what I did with NavigationController:
TwitterAuthViewController *vc = [[TwitterAuthViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
[vc release];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
OK, the problems seems to be that I called [self presentModalViewController: controller animated: YES] from ModalViewController->NavigationController. That's where Cocoa touch OR Twitter-OAuth-iPhone (I don't really know which one exactly) have a problem. But this is how I need it.
Solution would be, as Ben already said, simply to push SA_OAuthTwitterController like [self.navigationController pushViewController:controller animated: YES];.
The only remaining problem is that SA_OAuthTwitterController internally creates own NavigationController bar, so that now I have 2 navigation bars visible.