pushViewController iphone not working - objective-c

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

Related

Load view with the navigation controller?

I have an app where my main view is embedded in a navigation controller. From there, buttons push onto other view controllers. This all works fine. However, one of the view controllers it pushes to updates one of the root's values and presents it again. However this time, it only presents the ViewController without the navigation controller, and of course, pressing the button to go back will end in a crash. Hopefully this picture will help understand my issue. The pressing enter thing isn't really a big deal, I just call this function on return of the keyboard.
Code to go back to the main controller:
-(void)createNewMain:(NSString*)newAddress {
ViewController* newController = [self.storyboard instantiateViewControllerWithIdentifier:#"MainView"];
newController.labelText = newAddress;
newController.connected = self.connected;
[self presentViewController:newController animated:YES completion:nil];
}
The problem is simple, you're presenting the instantiated view controller modally.
Replace
[self presentViewController:newController animated:YES completion:nil];
with
[self.navigationController pushViewController:newController animated:yes];
Also, you can make a segue to do that from the storyboard. When the segue executes, it will create a new instance and will not use the previously created one.
Note: If you really don't need to create a new instance, consider using delegation to exchange information between objects.
You don't really want to "return" to a new instance of a root controller. What you need to do to properly return to the root controller is to pop all the other ones from the navigation controller's stack like this:
[self.navigationController popToRootViewControllerAnimated:YES];
Use Delegation to pass the expected/required message you want from your Pi Controller to your Root View Controller and set it up according to the message. You don't need to create a new instance of your Root View Controller from there. You can always go back to your root view controller from anywhere in the navigation stack by using
[[self navigationController] popToRootViewControllerAnimated:YES];

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.

Using KNSemiModal or MJPopUpView with storyboard

Whenever I try and dismiss the semi modal view, I am left with a crashed app. I think I've properly set up the dismiss modal view but it doesn't seem to be working. Here's what it looks like in the demo app using XIB files:
MJDetailViewController *detailViewController = [[MJDetailViewController alloc] initWithNibName:#"MJDetailViewController" bundle:nil];
[self presentPopupViewController:detailViewController animationType:MJPopupViewAnimationSlideBottomBottom];
Here's what I am trying to replace it with:
[self presentPopupViewController:[self.storyboard instantiateViewCOntrollerWithIdentifier:#"example"] animationType:MJPopupViewAnimationSlideBottomBottom];
The [self.storyboard instantiateViewControllerWithIdentifier:NSString] doesn't seem to be replacing the instWithNib (or whatever function that is) properly.
Any ideas why?
I did something similar.
I would just delete all the init methods in MJDetailViewController and also added "example" in the 'Storyboard ID' attribute of identity inspector then called
[self presentPopupViewController:[self.storyboard instantiateViewCOntrollerWithIdentifier:#"example"] animationType:MJPopupViewAnimationSlideBottomBottom];

loading a UINavigation controller from a UIView

i am designing an app that has a login screen. it is a UIView with username/password and a button to submit. once the user authenticated successfully, i want to load a new xib file that holds a navigation controller and a navigation bar. below the bar i want to load a tableView and switch between other views as i move along with the programming of it.
what i did is create a new class that inherits from UINavigationController and assembled the xib file to include the navigation controller. i hooked it back up to file's owner and i'm loading the navigation controller modally like this:
myNavController* navVC = [[myNavController alloc] initWithNibName:#"navXibFile" bundle:nil];
[self presentModalViewController:navVC animated:YES];
[navVC release];
this works okay as the navigation controller shows up. however, it shows up with no title, even though i've set one up in IB. moreover, the tableView's delegates are hooked up via IB but i cannot even see empty lines. all i see is an empty navigation bar at the top and blank view (one piece) below it.
thank you for your help.
so i figured it out... first it's a design decision right? is the app being managed by a navigation controller? if so (which is my case), expect the main (first) view, that is a login page, all you need to do is to hide the navigation bar in your ViewdidLoad for the main view:
[self.navigationController setNavigationBarHidden:YES animated:YES];
once the user logs in and you push the next view like this:
MainTableViewController* mainTableVC = [[MainTableViewController alloc]
initWithNibName:#"MainTableViewController" bundle:nil];
[self.navigationController pushViewController:mainTableVC animated:YES];
[mainTableVC release];
and lastly, in the ViewDidLoad of the next view controller:
[self.navigationController setNavigationBarHidden:NO animated:YES];
in case your app needs a navigation controller for a specific section of the app but not all if it, you will need to use the VC to manage this, in a similar way the appDelegate manages the sample navigation based sample app.
i hope this helps anyone struggling with wrapping their minds around the design patterns implemented here.
cheers.

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];