presentModalViewController inside UIViewController class - iphone

View *view1 = [[View alloc] init];
[self presentModalViewController:view1 animated:YES];
The code above works when connected to a UIButton. It does not work upon launch of an application by putting it in the viewDidLoad: method. I would like to run this upon launch.

Look very closely at the method you're calling: presentModalViewController: presents a controller, not a view.
The correct pattern is something like this:
MyViewController* myViewController = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
[self presentModalViewController:myViewController animated:YES];
[myViewController release];

Since iOS 6 you should use following method, because of deprections:
MyViewController* myViewController = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
[self presentViewController:myViewController animated:YES completion:nil];

Related

pushViewController doesnt work even though i have a navigationController

I have a UIViewController with two UITableView's in it. When i select a row in the first UITableView it has to push the same UIViewController which doesnt happen.
In UIViewController.h i have,
FirstTableViewController *firstController;
SecondTableViewController *secondController;
In UIViewController.m i have,
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
if (firstController == nil) {
firstController = [[FirstTableViewController alloc] init];
}
if (secondController == nil) {
secondController = [[SecondTableViewController alloc] init];
}
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstController];
firstController.product = self.product;
[firstTable setDataSource:firstController];
[firstTable setDelegate:firstController];
firstNavigationController.view = firstController.tableView;
}
In FirstTableViewController.m, didSelectRowAtIndexPath i have,
[self.searchDisplayController setActive:YES animated:YES];
UIViewController *controller = [[UIViewController alloc]initWithNibName:#"UIViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.product = prod;
NSLog(#"Navigation controller is %#",self.navigationController); //not null
[[self navigationController]pushViewController:controller animated:YES];
Please help.
EDIT #1: UIViewController is called from the FlipsideViewController of the UtilityApp.
Add a property to your AppDelegate UINavigationController *nav
add this line to AppDelegate.m in application didFinishLaunching method
navigationControl = [[UINavigationController alloc] initWithRootViewController:yourFirst ViewController];
Go to yourFirstViewController, add an UINavigationController *nav property and add these lines to viewDidLoad method
AppDelegate *app = [[UIApplication sharedApplication] delegate];
self.nav = app.nav;
Use this line to push any viewController
[self.nav pushViewController:controller animated:YES];
If you want to show a view controller modally, you should use presentViewController:animated:completion: and not pushViewController:animated:.
[self presentViewController:controller animated:YES completion:nil];
Use this Code in "didSelectRowAtIndexPath"
UIViewController *controller = [[UIViewController alloc]initWithNibName:#"UIViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[viewController release];
LoginViewSimpleController *loginViewSimple = [[LoginViewSimpleController alloc]init];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginViewSimple];
[self setNavigationController:navController];
Have you identify both tableviews ? Can you Post log status here ?

Popping to first view controller of the project

I have a UINavigationController. I load a presentmodalviewcontroller over it. And I push 2 more view controllers over the presentmodalviewcontroller. If I need to move to my first view controller, what should be done?
Edit: I am also loading some UIView over the UIViewController on top of my stack. I have successfully removed that.
I have tried
[self.navigationController popToRootViewControllerAnimated:YES];
[self.navigationController dismissModalViewControllerAnimated:YES];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
but still its not working
This is how I am adding each view controller
First viewcontroller
FirstViewController *firstViewController =
[[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UINavigationController *navcontrol =[[UINavigationController alloc]initWithRootViewController:firstViewController];
[self presentModalViewController:navcontrol animated:YES];
[navcontrol release];
Second viewcontroller
SecondViewController *secondViewController = [[SecondViewController alloc]init] ;
[self.navigationController pushViewController: secondViewController animated:YES];
[secondViewController release];
Third viewcontroller
ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];
[self.navigationController pushViewController: thirdViewController animated:YES];
[thirdViewController release];
Ok. I got it.
myAppDelegate *appDelegate = (myAppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate.navigationController dismissModalViewControllerAnimated:YES];

presentModalViewController not responding from NSObject class

I have to need to call a presentModalViewController from NSObject class.
ViewController *controller = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[[appDelegate navigationController] presentModalViewController:controller animated:YES];
[controller release];
Above code is working fine for iPhone but not responding for iPad View.
may be your appdeligate or the navigation controller are getting nil,
check your appdelegate declaration once or use the line
[[[[UIApplication sharedApplication] delegate] navigationController] presentModalViewController:imagePicker animated:YES];

Remove modalViewController from view?

I have a view which pulls in another view using this code:
secondView = [[SecondView alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondView];
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
The cancel button on this 2nd view has this:
[self.navigationController.modalViewController dismissModalViewControllerAnimated:YES];
But it doesn't appear to do anything. The view is still there and I can still interact with it. How do i remove it?
try:
[self.navigationController dismissModalViewControllerAnimated:YES]

presentModalViewController:animated: crash after dismiss

I'm currently having an issue with UIViewController's presentModalViewController:animated:.
I use the following code to set up and show the modal view controller:
UINavigationController *navigationController = [[UINavigationController alloc] init];
AddSerialController *serialController = [[AddSerialController alloc] initWithNibName:#"AddSerial" bundle:nil];
[navigationController pushViewController:serialController animated:NO];
[self.parentViewController presentModalViewController:navigationController animated:YES];
[serialController release];
[navigationController release];
The Application (running in iPhone Simulator) crashes as soon as dismissModalViewControllerAnimated: is called. GDB says it crashes at objc_msgSend.
If I comment out the last line of code (release of the navigation controller) everything works but I'm leaking a UINavigationController (as expected).
What the hell is going on here?
When you create a UINavigationController, you should give it a root view controller:
AddSerialController *serialController = [[AddSerialController alloc] initWithNibName:#"AddSerial" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:serialController];
[serialController release];
[self.parentViewController presentModalViewController:navigationController animated:YES];
[navigationController release];