I have three UIViewControllers ViewController, HomeViewController and RegisterViewController using storyboard. Their Storyboard ID is Login, Home and Registration respectfully. There is a button on RegisterViewController view named 'Register'. After successful registration i want to go to home page i.e., HomeViewController. How to do it. Please help I'm new to iOS.
thanks
- (IBAction)yourRegisterButtonAction:(id)sender
{
[self performSegueWithIdentifier:#"YOUR_SEUGE_NAME" sender:nil];
}
Try This
- (IBAction)buttonRegister:(id)sender {
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// replace Main with your storyboard name
HomeViewController *homeVC = [storyboard instantiateViewControllerWithIdentifier:#"Home"];
[self.view addSubview:homeVC.view];
}
Implement a navigation controller and do this.
-(IBAction)registerButtonTapped:(id)sender{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
HomeViewController *viewController =(HomeViewController*) [storyboard instantiateViewControllerWithIdentifier:#"Home"];
[self.navigationController pushViewController:viewController animated:YES];
}
Related
I created a SingleView application then use "Embed In Navigation Controller" to get a navigation control.
when push a controller, the viewcontroller's backgroundColor is black.
i know use this code :
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];//UIStoryboard(name: "Main", bundle:nil)
UIViewController *roomController = [storyBoard instantiateViewControllerWithIdentifier:#"controlerID"];
but what's the reason ?
and if i don't want to use the storyboard completely ,only use this code
UIViewController *roomController = [[UIViewController alloc]init];
what can i do? is only set the viewcontroller's backgroundColor?
You need to use the instantiateViewControllerWithIdentifier: method of your UIStoryboard instance and then you can push the controller.
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *roomController = [storyboard instantiateViewControllerWithIdentifier:#"controlerID"];
[self.navigationController pushViewController: roomController animated:YES];
// if You make controller with out storyboard Than change You view color
UIViewController *roomController = [[UIViewController alloc]init];
[self.navigationController pushViewController: roomController animated:YES];
- (void)viewDidLoad
{
self.view.backgroundColor =[UIColor whiteColor];
}
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
I used code from this question Display UIViewController as Popup in iPhone it is working fine but I don't want to use segue or I can say I can't use segue because all the code is created programmatically, so I cant set segue on button.
I used this code but it didn't work
- (IBAction)open:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *newVC = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"Second"];
[ViewController setPresentationStyleForSelfController:self presentingController:newVC];
[self.navigationController pushViewController:newVC animated:YES];
}
Edit 1:
View after popup
You should check if this VC self.navigationController is not nil. and i think you want to presentViewController: not pushViewController:...
The below code is work in ipad,but not in iphone.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *controller = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
UINavigationController *navgiate = [[UINavigationController alloc]initWithRootViewController:controller];
[self.navigationController pushViewController:navgiate animated:YES];
i cannot able to load MoreApsviewController by instatinating. View Controller is not pushing Please help me how to do it. Thanks in Advance
If your container is a navigation controller, you can simply intilize a VC and push it like this;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *controller = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
//No need to create a nav controller here
[self.navigationController pushViewController:controller animated:YES];
If you want to add another navigation controller you can present it or add as child VC, but you can't push another navigationVC.
These 2 steps should be checked.
If the View Controller you are trying to access is embedded in a Navigation controller you are correct in referencing the correct view, otherwise, change UINavigationController *navgiate to UIViewController * navigate (OR UITableViewController * navigate if it is a Table View).
Check your referencing of View Controllers. If you have the name (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"]; I will assume that you have named your View Controller MoreAppsViewController. You need to name and reference the UINavigationController as it is the point of reference.
This is the result for View Controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *controller = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
[self.navigationController pushViewController:controller animated:YES];
This is the result for Navigation Controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
//You need to name your Navigation Controller "MoreAppsViewController" for this to work
UINavigationController *controller = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
[self.navigationController pushViewController:controller animated:YES];
I use a navigation controller for login view.From there i push to a tabbar controller using this code
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UITabBarController *obj=[storyboard instantiateViewControllerWithIdentifier:#"tab"];
self.navigationController.navigationBarHidden=YES;
[self.navigationController pushViewController:obj animated:YES];
How can navigate back to login view when user tap logout from profile screen (one of the tabbar controller view). I want a navigation bar in the login view. Can anyone please help.
I am using Storyboard.
Try this:
[[self.parentViewController navigationController] popToRootViewControllerAnimated:YES];
Since you are pushing a TabBarController, you will need to pop using the TabBarController. self.parentViewController should give you the TabBarController.
You could use the following :-
//This would pop to the given VC
[self.navigationController popToViewController:loginViewController animated:YES];
//This would pop to the root VC
[self.navigationController popToRootViewControllerAnimated:YES]
Update:-
- (IBAction)loginBtn{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
//U provided UITabBarController so replace at with your tabBar controller class like below ,TabViewController class is created.
TabViewController *obj=[storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
self.navigationController.navigationBarHidden=YES; //navigation bar hidden so won't appear when back again to login if want to see then make it true in viewWillAppear/viewWillLoad.
[self.navigationController pushViewController:obj animated:YES];
}
Also for coming back from tabBar to login VC follow below,
- (IBAction)popbtn:(id)sender{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Also assgin the class to tabBar controller in storyboard along with storyboard id as below:-
You can use just one line of code.
[self.navigationController pushViewController:viewController animated:YES];
id rootController = [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] nextResponder];
if([rootController isKindOfClass:[LoginViewController class]]){
//do something
[self.navigationController popToRootViewControllerAnimated:YES];
}
else{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"login"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
}