Open ViewController on button click - iphone

I am new to iOS, can any one please help me to open and activity on Button click.
I have tried below methods, but the app remains on same ViewController, i am using Singleview Application type
Second *sec = [[Second alloc] init];
[self.navigationController pushViewController:sec animated:YES];
Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondV"];
[self.navigationController pushViewController:sec animated:YES];
can anyone please help me to solve this.

if you are using storyboard then Embed your viewcontroller to UINavigationController Editor->EmbedIn->NavigationController try this

If not using Storyboard, then following might help:
Second *sec = [[Second alloc] initWithNibName:#"Second" bundle:nil];
[self.navigationController pushViewController:sec animated:YES];
For Storyboard enabled code:
Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:#"YourIdentifierForVC"];
[self.navigationController pushViewController:sec animated:YES];
BTW, I strongly suggest you to google the problem for solution & search SatckOverflow - first, before posting as a new question.
Also, you should reconsider the naming for the class. Just a suggestion.

to push to another view controller you need to set a UINavigationController in front of your UIViewController in storyboard. Your self.navigationController might be nil, that's why it is not pushing your view controller.
Therefore drag and drop a Navigation Controller to your storyboard and delete the default TableViewController (make sure to click on empty space in storyboard before selecting and deleting it) and connect the UINavigationController to your own 'single view controller' by right-clicking on the yellow arrow and connecting the rootViewController outlet with it).
Then, select the UINavigationController on your storyboard and in Attributes Inspector (the fourth symbol in the right panel) under the section View Controller select Is Initial View Controller.

Related

UIViewController displayed behind current view when presented modally

On iOS 5, when I try to present any view controller from another one, using presentModalViewController, the modal view is presented behind the current view.
Since it works fine on iOS 4 and knowing that presentModalViewController has been deprecated in iOS 5, I tried using presentViewController with no luck.
This is the first time I encounter this issue, any ideas on what could lead to this weird behavior?
I believe the issue is that you have not set a proper modal presentation style.
http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIModalPresentationStyle
This sample should trigger a full screen modal over top of your existing view controller.
[self setModalPresentationStyle:UIModalPresentationFullScreen];
ViewController2 *vc = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
[self presentViewController:vc animated:YES completion:NULL];
Not sure if you're using a button to present the view controller, but this should work if you are. Create a new function in your view controller like the one below. This instantiates your view and a navigation controller in your view so it can be dismissed afterwards.
- (void)buttonPressed {
UIViewController *yourViewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navigationController = [[UINavigationController] alloc] initWithRootViewController:yourViewController];
[self presentModalViewController:navigationController animated:YES];
}
And then in viewDidLoad you'd have something like this (if you were presenting it from a button). The code below is for a UIBarButtonItem, but other buttons should work in a similar manner. Just make sure you set the action parameter to #selector(buttonPressed), or whatever the name of the function you want called when the button is pressed.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] init]
target:self
action:#selector(buttonPressed)];
I have finally found the issue. For some awkward reasons, the rootViewController of the root window wasn't set properly, leading to strange behaviors with modal views.
What is the more puzzling is that it worked fine on iOS 4 so far and failed on iOS 5. I believe I'm still missing the true reasons leading to such trouble, but correctly setting the rootViewController in AppDelegate solved the problem.

Xcode 4.1 Add and View TabBarController with existing ViewController

First of all sorry for my bad english and Im pretty new in these forums and Xcode programing.
So, Im writing an IPhone app with Xcode 4.1, that has Login and Register stuff visualized with UIViewController. When Im logged in, I need to visualize TabBar with different views.
I tried a lot of stuff and watched a lot of tutorials, all of them just start with the TabBarController, but I don't need it from the beginning, I just need to call it later.
The right way I believe should be just create new file .h, .m and .xib, then add the TabBarController and do a relation between TabBarController - view and File's Owner - view... but it don't let me do this thing. Obviously it don't visualize the right window.
How is the right way to do it?
Please help me, before my hair fall off...
Use the UITabBarController as the root view controller, but display a modal registration / logon view controller over the top when the app begins.
Once the user has logged in, dismiss the modal view controller to reveal the tab bar controller below.
You just use this code in your login button click or next viewcontroller viewwillappers method
UITabBarController *tabbar1 = [[UITabBarController alloc] init];
firstViewcontroller *second = [[firstViewcontroller alloc] initWithNibName:nil bundle:nil];
second.title=#"";
SecondViewController *third=[[SecondViewController alloc]initWithNibName:nil bundle:nil];
third.title=#"";
thirdViewController *one=[[thirdViewController alloc]initWithNibName:nil bundle:nil];
one.title=#"";
tabbar1.viewControllers = [NSArray arrayWithObjects:one, second,third,nil];
tabbar1.view.frame=CGRectMake(0, 0, 320, 460);
[self.view addSubview:tabbar1.view];
I am sure it will work for you i always use this code for create tab bar in any view.

Kill View and back to rootViewController

I am new to IOS, sorry in advance if I ask a stupid question.
I use UITabBarController and navigationController to control view.
At my last view, I would like to have a button when the button is pressed, view will return to rootViewController which I set by MainWindow.xib file and kill any process which run in app background.
this is my code in the last view before I want to back to rootViewController:
-(IBAction)doneButtonPressed:(id)sender{
JourneyIndexViewController *journeyIndexVC = [[JourneyIndexViewController alloc] initWithNibName:#"JourneyIndexViewController" bundle:nil];
[journeyIndexVC setDistanceLabelValue:self.distanceLabelValue];
[self.navigationController pushViewController:journeyIndexVC animated:YES];
[journeyIndexVC release];
[self dismissModalViewControllerAnimated:YES];
}
JourneyIndexViewController is the rootViewController that I set in MainWindow.xib.
Thank you very much for your advance support.
try
[self.navigationController popToRootViewControllerAnimated:YES];
You should take a look at this: UINavigationController Class Reference for better understanding
I am making a few assumptions here, but if JourneyIndexRootViewController is your rootViewController and is created in IB (in a nib), you do not need to re-crete it when pushing the button. It sounds like you simply need to remove the UINavigationController that you added on top of the rootViewController.
Try this. This should pop you back to the Previous View Controller.
[self.navigationController popViewControllerAnimated:NO];
Hope this helps

How do you properly set up a secondary view to support a navigation Controller on the iPhone?

I have an iPhone app that shows a simple view (View 1) that has a button. When the user presses this button, View 2 slides into view using the call
[self presentModalViewController:self.view2 animated:YES];
I want View 2 to support a navigation controller. All the code I find tells you how to set up a Navigation Controller App, but I can't figure out how to set this up using IB.
What I have done is to create a plain view2.xib file. I set the file's owner class to view2.
I add a navigation Controller to the XIB. I create an IBOutlet called view2Nav in view2.h for a UINavigationController. I link view2Nav to the NavigationController in view2.xib.
I then create a view3 class with view3.xib. I set the RootViewController in view2.xib to be of class view3 and set its NIB name to view3.
Then I go back and run the program. When I press my button on view 1, the app crashes as it tries to create view 2.
I know I must be missing a setting or something.
MySecondViewController *secondVC = [[MySecondViewController alloc] initWithNibName:#"MySecondViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondVC];
[self presentModalViewController:navigationController animated:YES];
[secondVC release];
[navigationController release];
Forget about IB. Do anything in code :) It is faster and you will exactly know why and how it works.
I'm not sure whether you can pass a self.view2 to presentModalViewController. If self.view2 is a subclass of UIViewController, you can. If it is a simple UIView, you shouldn't. If fact you can't at all.

Navigation controller crashing on second time

I am using one navigation controller in my application. I am having one main view (with main view controller) and few options views. Options views are viewed by navigation controller when a button clicked on main view's toolbar.
Everything works as expected for first time. When I came back to main view from navigation controller and tries again to go to option view (i.e. navigation controller) my application crashes.
Following is my code,
//Jump to navigation controller from main view controller
optionsViewController *optionsView = [[optionsViewController alloc] initWithNibName:#"optionsView" bundle:nil];
navControllerSettings = [[UINavigationController alloc] initWithRootViewController:(UIViewController *) optionsView];
[self presentModalViewController:self.navControllerSettings animated:YES];
//Code to go back to main view from navigation controller
[self.navigationController dismissModalViewControllerAnimated:YES];
What is correct mechanism to handle navigation controller? Do I need to release/dealloc the navigation controller or options view?
Sample code will help better.
Your navigation controller only gets set up once, then you push and pop other views from its stack.
UINavigationController *navController = [[UINavigationController alloc] init];
UIViewController *yourMainViewController = [[yourMainViewControllerClass alloc] init];
// when you are ready to go to your options view
optionsViewController *optionsView = [[optionsViewController alloc] initWithNibName:#"optionsView" bundle:nil];
[navController pushViewController:optionsViewController animated:YES];
// the back button and the pop from the stack when it is hit is handled auto-magically for you
Thanks for the reply.
But my application architecture is, I am having one main view on which I display few things and having toolbar. Toolbar contains one button, after clicked on that this navigation controller gets created and displayed on the screen.
using back button I came on the main view. But then after I could not go on the navigation again as it is crashing while executing following line
[self presentModalViewController:self.navControllerSettings animated:YES];
What might be the problem here?
Vishal N
I had a similar problem to this with it crashing in presentModalViewController: on second attempt. The problem was caused by calling [self becomeFirstResponder] in viewDidAppear: inside the first UIViewController that I presented, but I failed to call [self resignFirstResponder] in viewWillDisappear:.