I have looked at all the tutorials I can find on this one, and I still don't have the answer. I need to call another view from the code. I am using UIStoryboards. I have changed the view many times by control-dragging from UIButtons, but now it must be from the code. I am trying to call the info page from the main menu if it is the first time the user has opened the app. I cannot seem to find a way to change the views from the code, however. All my views are controlled by the same files (ViewController2). The identifier of my main menu is ViewControllerMain, and the identifier of the info page is ViewControllerInfo. First I tried this:
[ViewControllerMain presentViewController: ViewControllerInfo
animated:YES
completion: NULL];
Then I tried making different UIViewControllers for each and saying:
[ViewController2 presentViewController: ViewController
animated:YES
completion: NULL];
Neither worked. For the first one, it says:
Use of undeclared identifier ViewControllerMain.
In the second one, it says:
unexpected interface name 'ViewController': expected identifier.
What can I do?
To create a view controller:
UIViewController * vc = [[UIViewController alloc] init];
To call a view controller (must be called from within another viewcontroller):
[self presentViewController:vc animated:YES completion:nil];
For one, use nil rather than null.
Loading a view controller from the storyboard:
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self presentViewController:vc animated:YES completion:nil];
Identifier of your view controller is either equal to the class name of your view controller, or a Storyboard ID that you can assign in the identity inspector of your storyboard.
You need to instantiate the view controller from the storyboard and then show it:
ViewControllerInfo* infoController = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerInfo"];
[self.navigationController pushViewController:infoController animated:YES];
This example assumes that you have a navigation controller in order to return to the previous view. You can of course also use presentViewController:animated:completion:. The main point is to have your storyboard instantiate your target view controller using the target view controller's ID.
Swift
This gets a view controller from the storyboard and presents it.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewControllerId") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
Change the storyboard name, view controller name, and view controller id as appropriate.
You can call ViewController this way, If you want with NavigationController
1.In current Screen : Load new screen
VerifyExpViewController *addProjectViewController = [[VerifyExpViewController alloc] init];
[self.navigationController pushViewController:addProjectViewController animated:YES];
2.1 In Loaded View : add below in .h file
#interface VerifyExpViewController : UIViewController <UINavigationControllerDelegate>
2.2 In Loaded View : add below in .m file
#implementation VerifyExpViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.delegate = self;
[self setNavigationBar];
}
-(void)setNavigationBar
{
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"B_topbar.png"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Btn_topback.png"] style:UIBarButtonItemStylePlain target:self action:#selector(onBackButtonTap:)];
self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Save.png"] style:UIBarButtonItemStylePlain target:self action:#selector(onSaveButtonTap:)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];
}
-(void)onBackButtonTap:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)onSaveButtonTap:(id)sender
{
//todo for save button
}
#end
Hope this will be useful for someone there :)
There's 2 ways you can do this:
1, Create a segue to your ViewController in your Storyboard as explained in my answer here: How to perform a segue that is not related to user input in iOS 5?
2, Give your ViewController and identifier and call it using the code in my answer here: Call storyboard scene programmatically (without needing segue)?
main logic behind this is_,
NSString * storyboardIdentifier = #"SecondStoryBoard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: nil];
UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:#"YourviewControllerIdentifer"];
[self presentViewController:UIVC animated:YES completion:nil];
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone_iOS7" bundle:nil];
AccountViewController * controller = [storyboard instantiateViewControllerWithIdentifier:#"accountView"];
// [self presentViewController:controller animated:YES completion:nil];
UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topRootViewController.presentedViewController)
{
topRootViewController = topRootViewController.presentedViewController;
}
[topRootViewController presentViewController:controller animated:YES completion:nil];
Import the view controller class which you want to show and use the following code
KartViewController *viewKart = [[KartViewController alloc]initWithNibName:#"KartViewController" bundle:nil];
[self presentViewController:viewKart animated:YES completion:nil];
Related
I have a button inside my nib file. How can I programmatically present another view controller when the user taps on this button?
As I understood , you want to push new viewController on Button click . Please try this
- (IBAction)buttonClicked {
MyViewController *myViewController = [[MyViewController alloc]init];
[self presentViewController: myViewController animated:YES completion:nil];
}
First you need to create an action for your button, normally via Ctrl+drag. On that action implementation you should alloc the destination ViewController. The code would be something like this:
- (IBAction)buttonTapped {
MyViewController *modalViewController = [MyViewController new];
[self presentViewController:modalViewController animated:YES completion:nil];
}
Since you said that your file is a nib, I'm assuming that you aren't on a storyboard with both ViewControllers on it and the ViewController to be presented is code based. If this isn't the case, please elaborate your question.
As requested: Loading a VC from a storyboard
- (IBAction)buttonTapped {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
RSSViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"RSSVC"];
// OR If is initial
// RSSViewController *viewController = [storyboard instantiateInitialViewController];
viewController.feedURL = #"...";
[self presentViewController:viewController animated:YES completion:nil];
}
If you need, ViewController identifier should be set in the Identity Inspector in the Storyboard.
Refer the below screenshots
First : control+drag the button
Second: Once you release the mouse,it shows you the box
Third:Click Connection Dropdown box.It shows you 3 option
Fourth:Choose or Select Action from the drop down.
Five : Finally give action name
now in ViewContoller.h the action method is
- (IBAction)actionGoNextPage:(id)sender;
Then in ViewContoller.m
#import "SecondViewController.h"
- (IBAction)actionGoNextPage:(id)sender
{
SecondViewController *secondVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self presentViewController:secondVC animated:YES completion:nil];
}
I'm looking for a way to present a modal view over my current UIViewController to basically show a UIActivityIndicator and force users to wait while data is being loaded.
in BaseViewController.m (base class of all my UIViewControllers):
// show loading view
-(void) showLoading
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
LoadingViewController *loading = [storyBoard instantiateViewControllerWithIdentifier:#"loadingView"];
loading.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.7];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:loading animated:NO completion:nil];
}
This works great, but how can I go back to the background view after the loading view should be done?
Need a stopLoading method to go back to the original view:
// stop loading
-(void) stopLoading
{
// code here
}
If I try to present a new view after I present the loading view like so:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *view = [storyBoard instantiateViewControllerWithIdentifier:#"loadingView"];
[self presentViewController:view animated:YES completion:nil];
The debugger gives Warning:
Attempt to present PropertyPickerViewController: 0x8af6010 on ViewController: 0x8ab23c0 which is already presenting LoadingViewController: 0x8acf530.
Try:
[self dismissViewControllerAnimated:YES completion:nil];
In fact, I'm not sure that it'a great idea to present new controller with animated gif.
The best option is (imo) show UIActivityIndicator + place a view on top on all other views to prevent user from clicking anything.
You must [self dismissViewControllerAnimated:YES completion:nil] first.
Check the Apple Documentation.
In my iPhone App, I have 2 Storyboard, where I switch from Main.storyboard to Camera.storyboard and back. When I am on the Camera.storyboard I want to have the Backbutton functionality and look like the backbutton of the navigation conroller.
How do I do this best, without navigation controller? Is it possible to clone the navigationbar and backbutton somehow, which I then can connect with my back function?
Update
Had a missunderstanding in my thinking and now a solution with NavigationController. I embeded the CameraViewController in a NavigationController with IB in the Camera.storyboard and added the following code in the CameraViewController:
-(void)viewDidLoad {
}
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(back)];
self.navigationItem.rightBarButtonItem = cancelButtonItem;
}
I did use a cancel button instead a back button as, I did not find the back button and more important, the cancel was logical more correct in this situation.
Currently I switch to the Camera.storyboard with this code:
ViewController on Main.storyboard
#pragma mark - Segue to CameraView
-(void)toCameraView:(id)sender {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Camera" bundle:nil];
UIViewController *cameraViewController = [mainStoryboard instantiateInitialViewController];
cameraViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:cameraViewController animated:YES completion:nil];
}
and Back with:
CameraViewController on Camera.storyboard
- (IBAction)upload:(id)sender {
[[WebApi sharedInstance] uploadImage:self.imageView.image];
[self dismissViewControllerAnimated:NO completion:nil];
}
- (IBAction)submitButtonClicked:(id)sender{
NSLog(#"here");
SuccessOrFailViewController *sfvc = [[SuccessOrFailViewController alloc] init];
[self presentViewController:sfvc animated:NO completion:NULL];
}
I am trying to open a new page when a user clicks the submit button in the app. However, this opens completely black screen with nothing on it. How do I make it open the viewController instead ?
You are not entering the nib name for the user Interface:
SuccessOrFailViewController *sfvc = [[SuccessOrFailViewController alloc] initWithNibName:#"SuccessOrFailViewController" bundle:nil];
[self presentViewController:sfvc animated:NO completion:NULL];
For storyboard this is the way to go:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
SuccessOrFailViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"SuccessOrFailViewController"];
[sfvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:sfvc animated:YES];
Swift
var next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController
self.presentViewController(next, animated: true, completion: nil)
don't forget to set ViewController StoryBoard Id in StoryBoard -> identity inspector
Incase anyone else finds this, make sure you haven't set self.view.backgroundColor = UIColor.clearColor() in the view to be shown... This solved it for me.
For Storyboard iOS7
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
SuccessOrFailViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"SuccessOrFailViewController"];
[sfvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:sfvc animated:YES completion:nil];
I've had this happen after adding and deleting View Controllers in the Storyboard and forgetting to update the View Controller with the latest Storyboard identifier.
For example, if you are programmatically moving to a new View Controller, like this (note Identifier in the code is "FirstUserVC"):
let firstLaunch = (storyboard?.instantiateViewControllerWithIdentifier("FirstUserVC"))! as UIViewController
self.navigationController?.pushViewController(firstLaunch, animated: true)
The make sure in Interface Builder you have the Storyboard ID set like this with FirstUserVC listed in the Identity for Storyboard ID:
I have this code
PlaceViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:#"PlaceView"];
[self presentViewController:newView animated:YES completion:nil];
And I can change view, but I would push this view for when I return at this page, the state persists.
I try to put this code:
[self.navigationController pushViewController:newView animated:YES];
but doesn't do anything.
Thanks
Objective-C:
PlaceViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:#"storyBoardIdentifier"];
[self.navigationController pushViewController:newView animated:YES];
Please do notice below points,
your storyboard identifier is correct.
The root view have navigation Controller.
Swift:
let newView = self.storyboard?.instantiateViewController(withIdentifier: "storyBoardIdentifier") as! PlaceViewController
self.navigationController?.pushViewController(newView, animated: true)
For Storyboards, you should use performSegueWithIdentifier like so:
[self performSegueWithIdentifier:#"identifier goes here" sender:self];
Use:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"STORYBOARD_NAME" bundle:nil];
PlaceViewController *newView = [storyboard instantiateViewControllerWithIdentifier:#"PlaceView"];
[self presentViewController:newView animated:YES completion:nil];
Or:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"STORYBOARD_NAME" bundle:nil];
PlaceViewController *newView = [storyboard instantiateViewControllerWithIdentifier:#"PlaceView"];
[self.navigationController pushViewController:newView animated:YES];
Swift 3.x
let viewController = storyboard?.instantiateViewController(withIdentifier: "storyboardIdentifier") as! UIViewController
navigationController?.pushViewController(viewController, animated: true)
By default, Xcode creates a standard view controller. we first change the view controller to navigation controller. Select the Simply select “Editor” in the menu and select “Embed in”, followed by “Navigation Controller”
Step 1. Select Main story board
Step 2.Click on "Editor" on top of your Xcode application.
Step 3. Click on "Embed In"
Xcode automatically embeds the View Controller with Navigation Controller.