Opening ViewController programmatically - ios

I have another ViewController inside the same story board to which I want to switch by clicking a button. The problem is when I try to control-Drag the second view controller to create an Outlet inside the ViewController.m file of the primary viewcontroller, an outlet isn't created.
In Android we can open a new Activity with different UI from inside of another activity. I am sure same can be achieved in iOS as well, so my question is how can I create an outlet of the second view Controller and open it programmatically?

It is not possible in iOS to create an IBOutlet from one ViewController to another ViewController, But you can use a UIStoryboardSegue for the purpose.
I will suggest you to follow Tutorial: Storyboards from Apple Documentation. It will help you understand how ViewControllers are actually connected.

Opening SecondViewController programmatically is possible by using Storyboard Identifier, You need to provide storyboard identifier name for the class you want to switch to, see this image , in my demo i have used secondViewController
Now use this code inside your buttonClick event method.
- (IBAction)buttonClicked:(id)sender {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
SecondViewController *secondViewController = (SecondViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"secondViewController"];
// Use this to show your UIViewController in modal transition.
[self presentViewController:secondViewController animated:YES completion:nil];
// Use this to show your UIViewController in push transition with navigation controller
//[self.navigationController pushViewController:secondViewController animated:YES];
}

Try this
Viewcontroller*homeVC;
[self.navigationController setNavigationBarHidden:YES];
UIStoryboard *storyBoard;
storyBoard=[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
homeVC=(HomeVC*)[storyBoard instantiateViewControllerWithIdentifier:#"HomeVC"];
[self.navigationController pushViewController:homeVC animated:YES];

As others mentioned, you cannot do this.
To override and launch VC and keep it's properties you can:
First, create segue between viewControllers: Zoom out the view by double clicking the background of interface builder. Ctrl+drag from one VC to another and choose type of segue (e.g. show).
Than create action for button, in which you can:
[self performSegueWithIdentifier:#"myIdentifier" sender:self];
After that, in -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender check identifier of segue, set new yourCustomClassViewController and voila!
if ([segue.identifier isEqualToString:#"myIdentifier"]) {
myCustomClassViewController *myCustomVC = [segue destinationViewController];
myCustomVC.oldProfile = newProfile;
}

Related

How to navigate to a view controller on a new storyboard file in objective-c

I got a source code for an existing app. throughout the app the developer didn't use storyboard for the user interface but rather he used interface builder xib files. As a new developer, i don't know how to develop iOS apps in xcode using xib files for viewcontrollers, i learnt the storyboard way. Now I want to add more screens to the apps source code, i created a new storyboard file to define my new screens, how do I navigate to the entry point of this new storyboard from a button action i defined in one of the xib files so that i can easily implement my features.
while searching i got some suggestions like this one
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"newStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"myViewCont"];
but i dont think i understand how to implement this even if this can be in a button IBAction method. and besides subsequent navigations would be segues through the view controllers i define in the new storyboard
It's right suggestion.
You should get storyboard object by its name. Then you should create your viewController using [storyboard instantiateViewControllerWithIdentifier:#"..."];
Then you may navigate to your new VC using appropriate navigation method. For example:
[self.navigationController pushViewController:vc animated:YES]
ViewController identifier you should set in Interface Builder under section "Identity Inspector" in field "Storyboard ID".
That code you should place in IBAction method. But better would be to extract it in separate method. Something like this:
- (IBAction)buttonPressed:(UIButton *)sender {
[self navigateToMyNewViewController];
}
- (void)navigateToMyNewViewController {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"myNewStoryboard" bundle:nil];
MyNewViewController *myNewVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MyNewViewController"];
[self.navigationController pushViewController:myNewVC animated:YES];
}
Navigation from MyNewViewController your can implement as you want. It may be segues.
MyNewViewController *myVC = [self.storyboard instantiateViewControllerWithIdentifier:#"YOUR_VC_NAME_HERE"];
This will instantiate your ViewController, from here you can present it like so
[self presentViewController:myVC animated:YES completion:nil]
The image shows where you set the storyboard name that you're referring to
Try This it's working for me ,
Use below line of code :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main"bundle:nil];
LoginViewController *loginVC = (LoginViewController*)[storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:loginVC animated:YES];
We need to set storyboard id as per mention in below,
you can navigate one storyboard to another storybord:
first you import your viewcontroller class to appdelegate.h
AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];
appDelegateTemp.window.rootViewController = [[UIStoryboard storyboardWithName:#"storyboard_name" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
Like the way you use storyboard, use
MainViewController *mainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil]
[self.navigationController pushViewController:mainVC animated:YES];

self.navigationController pushViewController: animated: gives me a black screen

I'm using Objective-C. I have this code in my .m file.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
detailViewController *detail = [[detailViewController alloc]init];
[self.navigationController pushViewController:detail animated:YES];
}
And I created my Detail View Controller Scene in storyboard. It looks like this [
And every time I run this in my simulator and tap on one of the tableview cell, it pushes the screen to a complete black screen like this:
Can someone help me? I would thank you very much!
Yes it will give a blank screen, because you haven't attached any visual interpretation on detailViewController class.
Use this method
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
DetailViewController *vc = [sb instantiateViewControllerWithIdentifier:#"detailViewController"];
[self.navigationController pushViewController:vc animated:YES];
You will have to set storyboard id by clicking on view controller and set storyboard id as shown in picture
Note : for above pic replace Storyboard id "MessagingVC" with "detailViewController"
When using a storyboard, you don't want to use alloc+init to create the view controller. Instead, you need to assign a Storyboard ID in Interface Builder and then use:
[self.storyboard instantiateViewControllerWithIdentifier:my_identifier];
A better option may be to set a push segue on the table view controller. This will cause the instantiation and push to happen automatically. You can override prepareForSegue:sender: to set properties, if needed.

Navigation controller can not push view controller in iOS 7 when project using autolayout

Now in my project I have an issue with auto layout.
In iOS 8: It's still working well.
However in iOS 7: It pushed to view and can not load content the view. In navigation I saw it changed title. I guess auto layout is the cause of the error.
Code:
MyViewController * viewcontroller = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[employeeListVC.view updateConstraintsIfNeeded];
[self.navigationController pushViewController:employeeListVC animated:YES];
Please help me to resolve it. Many thanks.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
MyViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self.navigationController pushViewController:controller animated:YES];
I guess auto layout is not the cause. One possible case is that the MyViewController is not embedded in a UINavigationController.
Check self.navigationController is nil or not.
Error message from console will be helpful (if exists).
Use Segue instead of Navigation Controller. That better one as per my personal opinion.
Steps:
1. Embed your viewcontrollers [Initial View Controller must be embed in Navigation Controller]
Give a name identifier your segue.
Write code in your event
[self performSegueWithIdentifier:#"SegueBlockedContacts" sender:self];
Implement prepareForSegue method.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"SegueBlockedContacts"]) {
BlockedContactTableVC *blockedContactVC = segue.destinationViewController;
[blockedContactVC.blockedContacts removeAllObjects];
blockedContactVC.blockedContacts = blockedContacts;
blockedContactVC.myDelegate = self;
}
}
Thats it.

Initiating a segue from uiview or uivewcontroller that is not on storyboard

How can I initiate segue from a custom UIView or custom UIViewController that is not on Storyboard? - they are created programmatically inside a parent UIViewController.
Although the destination UIViewController is on the storyboard.
Then just do it the old fashioned way. Instantiate it and present it modally or just push it if you are in a navigation controller. Hope this helps.
EDIT:
You can talk about segue only if it is in the storyboard. If Your source View Controller is not in it, you just present the next one as I said. You can instantiate your destination view controller from the storyboard:
MyViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self presentViewController:controller animated:YES];
From this point you will be "back in the storyboard" so you can perform segues in the destination View Controller.
First give storyboard id. Select the view controller than write a identifier(Storyboard ID)
Secondly, just open your .m file then write belove codes.
UIStoryboard * st = [UIStoryboard storyboardWithName:#"nameOfStoryboard" bundle:nil];
MyViewController *viewController = [st instantiateViewControllerWithIdentifier:#"YourStoryboardID"];
[self.navigationController pushViewController:viewController animated:YES];
Last line of code. Might also be
[self presentModalViewController:viewController animated:YES];

Linking a new viewcontroller to Storyboard?

There is probably a simple solution but I can't figure it out.
I am using storyboards for the interface.
I start with a tab bar controller, but before the user is allowed to use the app the user has to authenticate himself trough a loginview which is modally pushed at the start.
I want to configure the loginview at the same storyboard, but I can't seam to figure out how to link the view controller at the storyboard and my code.
What I have done:
Create a new UIViewController subclass trough file > new > new file.
Drag a new UIViewController in the story board
Set the class in the custom class tab
drags a UILabel for test purpose.
run
No label...
Pull on a new UIViewController that will act as the login view controller onto the MainStoryboard. In the attribute inspector change the identifier to LoginViewController (or something appropriate). Then add
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
}
to the First view controller and the login screen will be loaded from your storyboard and presented.
Hope this helps.
The answer by Scott Sherwood above is most correct answer I found after lot of searching. Though very slight change as per new SDK (6.1), presentModalViewController shows deprecated.
Here is very small change to above answer.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
HomeViewController * hvc = [sb instantiateViewControllerWithIdentifier:#"LoginView"];
[hvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:hvc animated:YES completion:nil];
I'm new in this field. But if the first view controller is a navigation view controller and its rootviewcontroller is a table view controller. If you want to push a view controller like the LoginViewController when you click the cell, and you also want to go back to the table view by using the navigation bar. I recommend this way:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *controller = [sb instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
In this way, you can have the navigation.
By the way, I don't know why this kind of problem you asked will appear. I guess when the loginviewcontroller is created in the code, its view is not the view in the storyboard. If someone know the cause, please tell me! thanks!

Resources