Using A Button To Change Pages InXCode - iphone

I'm making an app in xcode for iphone with multiple pages. How can I change to a different page in the app by clicking a button?

One easy way, as Aaron mentioned, is to embed your view controller inside a Navigation Controller. If you are using storyboard, then select your view controller on the storyboard, and click menu item Editor -> Embed In -> Navigation Controller. And for trying how navigation will work, add a push segue from your login button on the login view controller to another view controller (your new 'page' where you want to navigate to will be another view controller on the storyboard) by Control +
clicking + dragging from the button over to the other view controller.
You can also navigate to another view controller programmatically without the segue. On the method that's called on Login button click, you can call another view controller (new 'page') like this:
// Create a new view controller
UIViewController *myController = [[UIViewController alloc] init];
// If you have the view controller defined in the storyboard, and you have give it a Storyboard Id, let's say "MyViewController", you can get it like this:
// UIViewController *myController = [self.navigationController.storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
// Push/Navigate to the new view controller
[self.navigationController pushNavigationController:myController animated:YES];
Hope that helps.

You could use this:
UIViewController *viewControllerYouWant = [[UIViewController alloc] init];
[self presentViewController:viewControllerYouWant];
If you touch the login button you go to the other view. And if you are on the other view use the same code just including the viewcontroller you want to go.

Related

How to open other page on Click of button

I am working on an application for IOS .I made a menu bar which contains buttons , menu bar should be on each page of application. I made many View controller scenes , I want the functionality like this : when i click on any button it should open a View controller scenes.
How to open a View controller scenes on click of button ? I don't want to add other existing View controller scenes on my current View controller scenes as a subview. I want to open other existing scenes independently.
You have to look into the following
Navigation controller
Tab bar controller custom implementation
Have you tried this, just replace YourViewController with the name of the .xib you would like to load.
YourViewController *yvc = [[YourViewController alloc] init];
[self presentViewController:yvc animated:YES completion:nil];
This goes inside your IBaction for the navigation button, fyi.
You need to look into UINavigationController or UITabBarController. There are other ways too. The navigation controller is the easiest. Apple documentation has everything you need.
1) Insert View :
ButtonView *buttonView = [[ButtonView alloc] initWithNibName:#"ButtonView" bundle:[NSBundle mainBundle]];
buttonView.view.frame = CGRectMake(yourFrame);
[self.view addSubview:buttonView.view];
2) Remove View :
[buttonView removeFromSuperview];

How to create a navigation controller based view from a simple uiview?

I have a simple uiviewcontroller with 4 buttons. Every buttonclick event loads a different image on the view. However for the 4th button i want to launch a navigation controller based uiview with a uitableview. The table view can then have 3 levels which define the settings for the application.
On selecting a row in the uitableview on the 3rd level i need to return to my main view with the selected row index.
How can i add a navigation based view which will be launched on a button press event and the first view of the uinavigationcontroller should compose of a back button which will close this navigation view and return to main view.
TIA,
Praveen S
Edit:
My first(home) view does not need a navigation bar. The view launched from the home view should however consist of a navigation bar with back button.
if(nxtv12==nil){
nxtv12=[[v12 alloc] initWithNibName:#"v12" bundle:nil];
}
[self.navigationController pushViewController: nxtv12 animated:YES];
and for coming back to home.
[self.navigationController popToRootViewControllerAnimated:YES];
Create a UINavigationViewController object in the current UIViewController (or use self.navigationcontroller) and push the new UIView into the navigation controller. To come back to the same view, use popToRootViewControllerAnimated in the new UIView class.

How is a navigation controller added to an existing view

I have a tableView and want to add a new viewController so that when the user selects an item the new view shows an image and the bar at the top will allow the user to go back to the tableView.
This navigation bar at the top will be present in both views. Is it also possible to add a button e.g. share to it and link my own actions to the button?
Thanks.
Your tableView should live inside a UIViewController subclass that is itself inside of a UINavigationController. You can get this structure set up for you by selecting the "navigation-based" app in the project creation wizard. Or you can create this structure yourself in code. In your app delegate, create a UINavigationController, and set your app's first ViewController as the root view controller. Then pushing and popping view controllers is easy, just call:
MyDetailVC *vc = [[[MyDetailVC alloc] initWithNibName:#"MyDetailVC" bundle:nil] autorelease];
[self.navigationController pushViewController:vc animated:YES];
EDIT: so, to clarify, you don't "add navigation controllers to existing views," you "push and pop instances of your view controllers on to the navigation controller's stack."

Navigating between views using Toolbar

In my app, I require the main page to contain a toolbar at the top of the view. This view has a tableView. So my application cannot have a NavigationController.
Problem
When I want to navigate to the other view on click of the tableview cell then I am using the "pushViewController:animated:" method but it doesnt seem to work.
I checked the connections in IB. They are fine.
How can I navigate between pages without navigation controller??
I do not want to use the modal view for other views.
Please Suggest some Method.
You can use an instance of UINavigationController without having it displayed and using it for navigation.
Load your first view controller with your instance of UINavigationController in your appDelegate implementation.
Like:
FirstScreenController *fsc=[[FirstScreenController alloc]initWithNibName:#"FirstScreenController" bundle:nil];
UINavigationController *navigation=[[UINavigationController alloc]initWithRootViewController:fsc];
navigation.navigationBar.hidden=YES;
[window addSubview:navigation.view];
Here FirstScreenController can be your view controller having the toolbar and tableview.If you want to display another view on any click of the tableviewcell, call it this way.
[self.navigationController pushViewController:secondviewcontroller animated:NO];

Set a navigation controller without an app delegate

I would like to show a Navigation Controller after clicking a button. Every tutorial assumes the navigation controller will be the first screen so it links it to the App Delegate, but App delegate only appears at MainWindow.xib.
How do you guys add a navigation controller to a view different than the MainWindow?
Thanks!
Here is some sample code to expand on Roger's answer. The following method is linked to some user interaction on the current view controller (to compose an email for example). This will give the compose view the navigation bar across the top instead of coding buttons inside your custom view.
-(void) composeButtonPushed: (id) sender {
ComposeViewController *controller = [[ComposeViewController alloc] initWithNibName:#"ComposeView" bundle:nil];
UINavigationController *composeNavController = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentModalViewController:composeNavController animated:NO];
}
UINavigationController is to navigate a heirarchy of views with UIViewControllers. If you don't have a root UIViewController, it won't work (and doesn;t make sense). If you do have a UIViewController, you simply send a - (id)initWithRootViewController:(UIViewController *)rootViewController init message to a new navigation controller passing in your UIViewController.