Can anyone provide me source code to call storyboard programatically in iOS 7 without using any segue. I am new to iPhone development.
Thank in advance
i am using like this :
UIViewController *pageOneController = [[UIViewController alloc]init] ;
[self.presentedViewController:pageOneController animated:YES completion:nil];
NSString * storyboardName = #"Main.Storyboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"pageOne"];
[self presentViewController:vc animated:YES completion:nil];
and i am getting this error:
[self.presentedViewController:pageOneController animated:YES completion:nil]; has error saying No visible #interface for 'UIViewController' declares the selector ':animated:completion:'
The error you are getting has nothing to do with Storyboard. Your code is a big mess and this is the line that crashes your app:
[self.presentedViewController:pageOneController animated:YES completion:nil];
From the error message you can understand, that presentedViewController property does not respond to this selector animated:completion:. So what you are doing wrong here is probably referencing the presentedViewController property instead of calling presentViewController method.
Try fixing this line with:
[self presentViewController:pageOneController animated:YES completion:nil];
Notice the difference that I removed the dot between self and presentViewController and changed from presentedViewController to presentViewController. This is the correct call and I am assuming that self is some kind of UIViewController class.
Take a look on this:
UIStoryboard *theStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *theViewController = [theStoryboard instantiateViewControllerWithIdentifier:#"viewController"];
And then you can just push the view using modal presentation.
But my recommendation is to get rid of Storyboards and go directly to code the interfaces with Objective-C. You'll have a better performance and control over them. And if you're learning about Objective-C programming you'll find coding interfaces, more interesting and educational than create them with Storyboards.
Related
My question: How can I switch between viewcontrollers with 1 rootNavigationController in code, while maintaining customization I set in Storyboard and data that I load up in my viewcontrollers?
Currently I am implementing REMenu, which provides a simple dropdown tableview to change views with. When one of those cells is pressed in the dropdown, I want to switch my view. For example, if I press "Home", I want to go to my MasterViewController The method to switch views is called from within rootnavigationcontroller.m, and looks like the following:
REMenuItem *homeItem = [[REMenuItem alloc] initWithTitle:#"Home"
subtitle:#"Return to Home Screen"
image:[UIImage imageNamed:#"Icon_Home"]
highlightedImage:nil
action:^(REMenuItem *item) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController *vc = [sb instantiateViewControllerWithIdentifier:#"myNewTableView"];
// vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:vc animated:NO completion:nil];
}];
Clearly, however, something is wrong with this implementation, because it ignores the UI Customization I made in Storyboard, as well as a datacontroller I invoke in MasterViewController.m Any help on this matter would be greatly appreciated!
For Example, I have 2 Storybards. In the first Storyboard I have a View with a button and when you press the button the second storyboard should appear.
How can I do that?
Take a look at the docs for the storyboard class
You can create a storyboard using
UIStoryBoard *storyboard = [UIStoryboard storyboardWithName:#"secondStoryboard" bundle:nil];
and get view controllers from it like
UIViewController *initialViewController = [storyboard instantiateInitialViewController];
or
UIViewController *otherViewcontroller = [storyboard instantiateViewControllerWithIdentifier:#"otherController"];
Once you've got your view controllers you can just push them onto your navigation controller I guess.
However, I don't know what will happen with using two storyboard objects in the same view hierachy - it's probably fine but you never know :)
Even though your question is a bit confusing, I think I know what you are trying to do.
You want to use two storyboards. UIStoryboard has a method to retrieve an instance of any storyboard with a given name. So first, set a name for your storyboards and view controllers in Xcode and then load them up, and then from within any view controller:
UIStoryboard *anotherStoryboard = [UIStoryBoard storyboardWithName:#"SomeStoryboardName" bundle:nil];
Afterwards, instantiate the desired UIViewController from any storyboard:
UIViewController *anotherViewController = [anotherStoryboard instantiateViewControllerWithIdentifier:#"SomeViewControllerName"];
You could then push it into your navigation stack, for instance:
[self.navigationController pushViewController:anotherViewController animated:YES];
Modifided to be generic here's the way I do this in my app...
UIStoryboard *alternateStoryboard;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate_iPad" bundle:nil];
} else {
alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate_iPhone" bundle:nil];
}
AlternateController *altController = [alternateStoryboard instantiateInitialViewController];
[altController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:altController animated:YES];
If your app is only iPhone or iPad that can be reduced to...
UIStoryboard *alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate" bundle:nil];
AlternateController *altController = [alternateStoryboard instantiateInitialViewController];
[altController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:altController animated:YES];
You'll probably want to change the last 2 lines to suit the presentation style that you want.
im trying to load another nib of a UIViewController after expected time. Everything does work but wenn i load the UIViewController i get a exception when it is called. I'm doing it that way:
UIViewController *overView = [[UIViewController alloc] initWithNibName:#"overView2" bundle:nil];
[super presentViewController:overView animated:YES completion:nil];
i also tried to insert [NSBundle mainBundle] instead of "nil" but the exception still occures.
When i push the UIViewController in following was, no exception occures but after the first view is removed nothing happends:
[self.parentViewController addChildViewController:overView];
i were searching long time but couldn't find a solution...
SOLUTION
I just found the mistake.
I imported the H-File of the UIViewController in the M-File where i wanted to load the new View.Than i created a new object and passed it through.
overView2 *overView = [[overView2 alloc] init];
[self presentViewController:overView animated:YES completion:nil];
sorry, I'm new to objectiv-c...maybe will help some other beginners too :P
A standard implementation of presenting a view controller is:
[self presentViewController:overView animated:YES completion:nil];
Unless I'm missing something. Is there any more information that you can give?
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.
I have 2 storyboard files in my app and I'd like to transition between a ViewController in one to a ViewController in the other. I've hooked up an IBAction in response to a button press on the first ViewController, which calls a method in the AppDelegate. I have verified that this signal reaches the AppDelegate method.
Here is the relevant method I have in the AppDelegate, however, no transition occurs. Can anyone tell me why, or is it a silly idea to have 2 storyboards?
-(void) presentSecondViewController {
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController* mainViewController = [mainStoryboard instantiateViewControllerWithIdentifier:#"main_viewcontroller"];
UIStoryboard* secondStoryboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController* secondViewController = [secondStoryboard instantiateViewControllerWithIdentifier:#"second_viewcontroller"];
[mainViewController presentViewController: secondViewController animated:YES completion: NULL];
}
You create a second instance of the initial view controller of the first storyboard. That instance was never shown on the screen as it is different from the one being already shown and thus probably won't show your second view controller. You need the instance of the view controller already being shown. The best way would be to change your implementation to
-(void) presentSecondViewControllerFromViewController:(UIViewController *)sourceController
{
UIStoryboard* secondStoryboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController* secondViewController = [secondStoryboard instantiateViewControllerWithIdentifier:#"second_viewcontroller"];
[sourceController presentViewController: secondViewController animated:YES completion: NULL];
}
and call it by passing the view controller that contains the button.