How can I manually switch between UIViewControllers in storyboard? - iphone

All I need is to view a UIView controller in same storyboard file manually with code. I use storyboard to make all forms and connections. My application starts in navigation controller, which provides me access to UIView (LoginViewController) and then it goes to tab bar controller, which provides 4 UIViews. According to every UIView I have .h and .m files. I know about segue method, it is simple, but I need manual method. Maybe I am doing something wrong.
I was trying to use this method for pushing view controller in IBAction:
[self.view pushViewController:LoginViewController animated:YES];
But it makes an error:
Unexpected interface name ‘LoginViewController’: expected expression
It took a lot of time to figure out what is wrong, but I had not succeed.
Here is my RollEnemyController.m file:
// RollEnemyController.m
#import "RollEnemyController.h"
#import "LoginViewController.h"
#implementation RollEnemyController;
#synthesize AttackButtonPressed;
- (IBAction)AttackButtonPressed:(id)sender {
LoginViewController* controller = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
[self.view pushViewController:controller];
}
#end
And this is header file:
// RollEnemyController.h
#import <UIKit/UIKit.h>
#interface RollEnemyController : UIViewController
- (IBAction)RollButtonPressed:(id)sender;
#property (weak, nonatomic) IBOutlet UIButton *AttackButtonPressed;
#end

I'm guessing that you are using a UINavigationController. Then you can simply do like this:
LoginViewController *controller = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
Update:
If you are using a UIStoryboard, you can set the identifier of your new viewcontroller, and then push it onto your navigationController. To set the identifier, choose your view, open the Attributes Inspector, and set the identifier ("LoginIdentifier" in my example). Then you can do this:
LoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginIdentifier"];
[self.navigationController pushViewController:controller animated:YES];
As a sidenote, I see that you are using capital characters for your methods. You should probably try to avoid that, and instead use lowered first-characters in your method names. And since you say you are learning Objective-C, you should check out this awesome thread here on SO: link.
Update 2:
Here is a zip file with a project showing how to do this. :-)

hello try to use this code
Storyboard put ID = "xxx * Name Desire"
mark use StoryboarID
UIStoryboard * storyboard = self.storyboard;
DetailViewController * detail = [storyboard instantiateViewControllerWithIdentifier: # "xxx * Name Desire"];
[self.navigationController pushViewController: detail animated: YES];

In this statement:
[self.view pushViewController:LoginViewController animated:YES];
it seems you are trying to push a class. You should push an object, your actual controller:
LoginViewController* controller = [[LoginViewController alloc] init...];
[self.view pushViewController:controller animated:YES];
this will at least compile, and if all the rest is fine, also give you the second controller.
EDIT:
I missed one point. You are pushing the view controller on to a view. That makes no sense, you should push the controller on to the navigation controller:
<AppDelegate> *del = (AppDelegate*)[UIApplication sharedApplication].delegate;
[del.navigationController pushViewController:controller animated:YES];
This is true, at least, if you created your project from the Navigation-based template (which creates an application delegate with a reference to the navigation controller). Otherwise, please provide details about how you create the navigation controller.

You mentioned in a comment that you're using UIStoryboard. Are you aware of UIStoryboardSegue? All you have to do it control-drag from the button to the next view controller to establish a segue. Then you can choose the type of transition. Be aware that your view controllers need to be part of a UINavigationController in the storyboard to perform a "Push" animation.

Related

How can i implement a UIModalTransitionStylePartialCurl in storyboard with Xcode 4.3?

I try this:
ViewController.h
#class SecondView;
#interface Introduccion : UIViewController{
SecondView *second;
}
-(IBAction)AnimatecreditsPage:(id)sender;
#end
ViewController.m
-(IBAction)AnimatecreditsPage:(id)sender{
second = [[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];
second.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:second animated:YES];
}
Im using Storyboards, and i already linked my viewcontroller with the corresponding classes, when i press the button, the iphone simulator just crash.. im using a navigation controller and tab bar controller.
Image of the viewcontroller
THANKS!! :)
Please Help Me.
The way you're trying is crashing because you're pointing to a xib that doesn't exist. Since with storyboards you can have multiple view controllers you have to add an identifier to the view controller you wish to use in the attributes inspector section of interface builder. This then allows you to use the following code to programmatically instantiate what ever view controller in your storyboard has the ID you specify.
second = [self.storyboard instantiateViewControllerWithIdentifier:#"someID"];
Instead of:
second = [[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];

Adding UITabBarController to a UIViewController

My app currently has a UINavigationController and I'd like to push a UITabBarController at some point when a button is clicked. I am trying to create it on Interface Builder (as opposed to programatically).
All online tutorials show how to create a tab bar based app, which involves dragging a UITabBarController into the MainWindow.xib which is obviously not what I want.
What I did was create a UIViewController, and its nib file, dragged a UITabBarController. Now pushing that UIViewController to the navigation controller will show an empty view (its empty view). Removing the view in the view controller will crash the app. How can I tell the UIViewController to load a UITabBarController instead of its own view?
For those down-voting me: it would be decent to at least provide a comment. The question is not a poor question. The questions is asking for suggestions for how to use a UITabBarController in an unorthodox way. I tried most of the suggestions and they do not work. If you are down-voting, at least write a comment.
You can see this this may help you
Since this is how you want your app to be: - Navigation Controller - Root View Controller - Other View Controllers - Tab Bar Controller - First VC under tab - Second VC under tab - Third VC under tab - more view controllers
in your view controller where you want to pushViewController to UITabBarController use this
//create a UITabBarController object
UITabBarController *tabBarController=[[UITabBarController alloc]init];
//FirstViewController and SecondViewController are the view controllers you want on your UITabBarController (Number of view controllers can be according to your need)
FirstViewController *firstViewController=[[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondViewController=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
//adding view controllers to your tabBarController bundling them in an array
tabBarController.viewControllers=[NSArray arrayWithObjects:firstViewController,secondViewController, nil];
//navigating to the UITabBarController that you created
[self.navigationController pushViewController:tabBarController animated:YES];
This tutorial might help. It comes with an example code.
Hi just make both nav controller and tabBar Controller in app delegate.
Initially add navController to your root view..
[self.window addSubview:navigationController.view];
and whenever you want to add tab bar then remove navController and add tabBarController.
-(void)addTabBarController
{
AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];
[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window]addSubview:[[appdelegte tabcontroller]view]];
[[appdelegte tabcontroller]setSelectedIndex:0];
}
If you get any problem then ask me again..
In YourView controller make IBOutlet of tabBarController
in .h file
#import <UIKit/UIKit.h>
#interface YourView : UIViewController
{
IBOutlet UITabBarController *tabBarController;
}
-(IBAction)loadTabBar:(id)sender;
#end
and in .m file
#import "YourView.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#implementation YourView
-(IBAction)loadTabBar:(id)sender
{
FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil];
[self.navigationController pushViewController:tabBarController animated:YES];
}
#end
The tabBarController IBOutlet must be connected to the UITabBarController that on the .xib file. And that UITabBarController with two view controllers named FirstViewController, SecondViewController.
I remember doing something similar to this...
I had to create a custom UITableViewController to do this, if you are going to use UINavigationController to 'push' to it.
Doing it only in interface builder may be a bit tricky, it's been a while since I've been at it, I do recall it was a bit of a nightmare to get going correctly.
The problem was, as I believe I've mentioned somewhere, is that the XIB does not have a UIView connected to it. When the UIView is deleted in a XIB file and a UITabBarController is added, the view property of the XIB has to be connected to the UITabBarController's view. I connected it and it worked. That was the reason why I was getting a SIGTRAP.
take a uiview of tab bar controller means create an interface builder with tabs and add that tab bar uivew in your classes where ever u wanted the tab bar
for example take a tab bar uiview of 3 tabs in that uiview take the three buttons in the interface builder
for every navigation of that classu should add this uiview class
-(IBAction)firt_button_pressed:(id)sender
{
}
-(IBAction)second_button_pressed:(id)sender
{
}

self.navigationController pushViewController not working

I have a View application with a Single UIViewController. I then add a UITableViewController through the IB, and I am trying to display the UITableViewController through a button press in the UIViewController (my main view). My button press (IBAction) contains the following code through which I am trying to push my UITableViewController view and display it:
DataViewController *dataController = [[DataViewController alloc] initWithNibName: #"DataViewController" bundle:nil];
[self.navigationController pushViewController:dataController animated:YES];
[dataController release];
My DataViewController is not at all getting pushed into the stack and displayed,
Also I have checked that in the code above, self.navigationController=nil
Probably this is the source of the problem. If so, how to rectify it?
Please help.
UINavigationController *navCtrlr = [[UINavigationController alloc]initWithRootViewController:yourfirstviewController];
[self.window setRootViewController:navCtrlr];
navCtrlr.delegate = self;
navCtrlr.navigationBarHidden = YES;
Create navigation controller in appdelegate.m then you can navigate to any uiviewcontroller
You need to actually create a UINavigationController. The navigationController property tells you whether your DataViewController is currently in a UINavigationController's hierarchy; if not (as in this case), the navigationController property returns nil.

UIViewController not responding to presentModalViewController for modal navigation

I am trying to implement a modal navigation controller as described in the Apple iOs Guide: Combined View Controller Interfaces
I have come to the conclusion that I am missing something both obvious and stupid as I simply cannot get anything to display, I get a blank white screen.
Swapping things out I can prove that the view controller that I am using as the navigation controllers RootViewController works fine on it's own (by adding it manually as a view subChild).
Further, implementing addSubView ([self.view addSubview:navController.view]) instead of presentModalViewController seems to work OK.
Can anyone point out my simple error because I am 5 minutes short of kicking my own face :D
header
#import <UIKit/UIKit.h>
#interface BaseViewController : UIViewController {
}
implementation
#import "BaseViewController.h"
#import "ScannedListViewController.h"
#import "ScannedItemViewController.h"
#implementation BaseViewController
- (void)viewDidLoad {
ScannedListViewController *listViewController = [[ScannedListViewController alloc] init];
ScannedItemViewController *itemViewController = [[ScannedItemViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:listViewController];
[navController pushViewController:itemViewController animated:NO];
[self presentModalViewController:navController animated:YES];
[listViewController release];
[itemViewController release];
[navController release];
[super viewDidLoad];
}
The RootControllerView is a basic test TableViewController with the following header
#interface ScannedListViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
Thank you in advance if your able to help
Why are you presenting something modally in a view controller's viewDidLoad method? I find that odd off the top. Generally you show a modal view controller in response to some action (like tapping a button).
Is there a reason you're showing a navigation controller with a second view controller already pushed on it after the root?
You should have [super viewDidLoad] as the first line, not the last line, of the method.
You do not need to have <UITableViewDelegate, UITableViewDataSource> after UITableViewController because it already adopts those protocols. Remove that bit.

iPhone: ViewController as a tab bar

Hey guys I need some help with this:
I have two view controllers, let's say:
FirstViewController (first) is inside a navigationviewcontroller
SecondViewController (second)
So in the first's viewDidLoad method I have this:
SecondViewController *second = [[SecondViewController alloc] initWithNibName:...];
[self.addsubview:second.view];
I've done this cuz I want to make my custom tab bar with my custom buttons and colors.
I have this when I press one of the buttons of my custom "tab bar" (seconviewcontroller)
ConfiguracionViewController *conf = [[ConfiguracionViewController alloc] initWithNibName:#"ConfiguracionView" bundle:nil];
[self.navigationController pushViewController:conf animated:YES];
[conf release];
but because the second view controller is not pushed or modal presented in the first view controller I can't acces the navigationController. I've tried also with this
[self.parentViewController.navigationController pushViewController:...];
But it didn't work either.
Please help me out, I need to learn how to do that and sorry for my bad english.
Best Regards,
Carlos Vargas
First, shouldn't you be adding the second view to the first view like this:
[self.view addSubview:second.view];
The property parentViewController will not work in this case since second is not part of a navigation hierarchy.
Instead, you can make your own property that references the "parent" view controller:
SecondViewController *second = [[SecondViewController alloc] initWithNibName:...];
// set new property
second.parentVC = self;
[self.view addSubview:second.view];
In SecondViewController.h you need to declare the instance variable and property for "parentVC" and in SecondViewController.m you need to synthesize the property.
Then, you should be able to access the navigation controller and push a view controller from SecondViewController.m like this:
[self.parentVC.navigationController pushViewController:...];