presentViewController shows black page - ios

- (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:

Related

How to switch from one storyboard to another storyboard

Team,
I have two storyboard.
One For authentication
And another for My application Dashboard.
For Authentication Storyboard The initialisation screen is loginScreen.
After login successfully i am loading Dashboard Storyboard.
For dashboard storyboard the initial screen is MainViewController.
Here I am implemented logout from in DashboardStoryboard. So now i want to switch back to my Authentication Storyboard.
Here its going back to loginScreen.
But I thing its not proper way for implementing.
It will be more helpful is there a way I can do better?
-(void)logout{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Authentication" bundle: nil];
LoginViewScreenController *loginViewScreenController = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewScreenController"];
[self.navigationController pushViewController: loginViewScreenController animated:NO];
}
Your feedback is highly appreciated.
It's Very easy using segue and Storyboard Reference. Please follwo steps and screenshots.
Step-1)
Drag and drop Storyboard Reference from Object Library in First(Main) Story board.
Step-2)
Add segue from your source ViewController to Storyboard reference.
Step-3)
Select another(second) storyboard.
Reference ID: StoryboardID of your destinationViewControler(second View Controller) which is available in Second.Storyboard
-(void)logout
{
UIViewController *aVCObj = [[UIApplication sharedApplication]delegate].window.rootViewController;
if ([aVCObj isKindOfClass:[UINavigationController class]]) {
UINavigationController *aNavController = (UINavigationController *)aVCObj;
[aNavController popToRootViewControllerAnimated:YES];
}
}
Here is a trick to do this by setting up a key true using NsuserDefaults when user logged in otherwise false and navigate your application when starts accordingly using presentViewController method without animation so the user will not get any option to go back previous vc.
Have a look below code illustrating above sentence:
if ([[[NSUserDefaults standardUserDefaults] valueForKey:#"isloggedIn"] isEqualToString:#"true"]) {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"loginView"];
[self presentViewController:vc animated:NO completion:nil];
}else{ // when logout
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"logoutView"];
[self presentViewController:vc animated:NO completion:nil];
}
If you need to apply some effects when vc appear just ad these two lines before presentViewController method see:
[vc setModalPresentationStyle:UIModalPresentationCustom];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
Note: set the key false when user logout.
let sb = UIStoryboard(name: "Main", bundle: nil)
let homeVC = sb.instantiateViewController(withIdentifier: "TabBarController")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = homeVC

iOS 7 presentViewController shows black screen and SIGABRT

I use two buttons on right UIBARBUTTON by writing code in the method[ViewDidLoad].
http://i.stack.imgur.com/VcOEW.png
http://i.stack.imgur.com/vG3eT.png
http://i.stack.imgur.com/f0xsB.png
I want to use +plus button to move to AddNameViewController but it results in a Fail[sigabrt].
http://i.stack.imgur.com/MBq2m.png
http://i.stack.imgur.com/Hdw8T.png
I think the error is in the following code.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
AddNameViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"AddNameViewController.m"];
[sfvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:sfvc animated:YES completion:nil];
Change this:
AddNameViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"AddNameViewController.m"]
For this:
AddNameViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"AddNameViewController"]
You need the name of the View Controller, not the file.
Like Antonio said, you need the correct identifier for your view controller. The reason it crashes is because you get a nil view controller pointer back from the instantiateViewControllerWithIdentifier: call and passing nil to presentViewController:animated:completion: causes your crash.
check this
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
AddNameViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"AddNameViewController"];
[sfvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:sfvc animated:YES completion:nil];
and add identifier AddNameViewController in storyboard ID
Sorry, I did not check carefully before asking. Because I tried many ways to improve it. Although, I change AddNameViewController.m to AddNameViewController ,
It still show sigabrt.

UIStoryboard with object

I have multiple storyboards within my app. I want to pass an object when a new storyboard is opened.
I'am doing this:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"SetupStoryboard" bundle:[NSBundle mainBundle]];
UINavigationController* initialHelpView = [storyboard instantiateInitialViewController];
SetupViewController *setup = (SetupViewController*) [initialHelpView topViewController];
setup.data = self.data;
initialHelpView.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:initialHelpView animated:YES completion:nil];
But when the storyboard is presented the setup.data is nil in viewDidLoad, viewWillAppear etc of the SetupViewController...
Why is that?
I don't see anything wrong with this code. Problem may be elsewhere.

How to call a View Controller programmatically?

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];

iOS - Push viewController from code and storyboard

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.

Resources