Display UIViewController as Popup without seguge - ios

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

Related

Open Storyboard on top of previous view

I have created storyboard which I want to open on top of another view or in childView so that when I close or destroy this view of storyboard the earlier view on which the storyboard is opened remains the same.
When I run the following code:-
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"neolick"];
[[UIApplication sharedApplication].keyWindow setRootViewController:detailViewController];
First, the storyboard opens but I don't know whether it opens on top of previous view or it destroys the previous view & then open.
Second, the functions needed on that storyboard runs automatically which is as I want but how these things are working.
If anyone can help me understand the above code and its working.
NOTE: I cannot call the earlier view again in same state because of some reason.
Thanks in Advance!!
Here you are setting root view controller
it will not keep your back screen as it is what you want
If you want to keep current screen and show other screen on that
you have two approaches
1) Present ViewController
2) Push View Controller
1) Present ViewController
for this you can present your screen on top of other screen which is visible
for example
- (IBAction)btnNextTapped:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"neolick"];
[self presentViewController:detailViewController animated:true completion:nil]
}
2) Push View Controller
For that you need NavigationController and need to push your ViewController from current visible screen
i.e
[self.navigationController pushViewController:vc animated:true];
EDIT
as per discussion you need to find current top view controller then you should present it
Add this method below your method
- (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
And Replace this method with code
- (void)goToNewPage:(CDVInvokedUrlCommand*)command
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"neolick"];
[[self topMostController] presentViewController:detailViewController animated:true completion:nil];
}

How to open new view controller after click button?

I added one ViewController to my project and created one class.
After I bind this class to my ViewController.
In another controller I have method:
- (IBAction)login:(id)sender {
// How here I can do redirect to controllerViewController
}
There are two ways to push view controllers in the application.
1) by Segue
2) By View controller identifier
1) By Segue :
[self performSegueWithIdentifier:"SegueIdentifier" sender:self];
2) By View controller identifier :
Yourclassname *gotoYourClass = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerIdentifier"];
[self.navigationController pushViewController:gotoYourClass animated:YES];
- (IBAction)login:(id)sender {
ViewController *vc = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil]; }
In the storyboard give your view controller an identifier (under the Attributes Inspector) then use the following code, to bring that view forward.
IF YOU WANT TO USE PUSH THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self.navigationController pushViewController:vc animeted:YES];
IF YOU WANT TO PRESENT THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self presentModalViewController:vc animated:YES];
Note: In UIViewController please enter your view controller name which you want to push into another view.
If you want to comeback to the current ViewController later then use Navigation ViewController or else just use the presentedViewController of self(your current viewController)- no going back business, like they have previously illustrated.
For a simple block of execution(demo or practice) it's all the same but for a project application it completely matters to make the correct choice.

self.navigationController is 'null' even after embed in a navigationcontroller

I want to add a navigationcontroller to an existing viewcontroller which is created using storyboard, i have embed it in a navigation controller, but the code for navigating (shown below) is not working even after embed in the navigation controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self.navigationController pushViewController:nextViewController animated:YES];
When i have tried to log self.navigationController, it shows null.
Update: It is fine when i am trying with presentViewcontroller , but i
want to push the viewcontroller with navigationController.
I am struggling with this for two days,Please help.
If self is a subclass of UINavigationController, you do not need to refer to the navigationController property. (Note that this won't work if EditProfileViewController is also a subclass of UINavigationController, as you can't push a UINavigationController inside a UINavigationController).
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self pushViewController:nextViewController animated:YES];
Otherwise, if you don't have a pre-existing navigation controller
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self presentViewController:nextViewController animated:YES completion:nil];

Confused about storyboards and pushing programmatically ios Objective C

I am really confused about the relationship between storyboards and pushing to views programmatically.
I am using SWRevealViewController to display a menu of items.
If I push to the storyboard using
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PhotosViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"PhotosViewController"];
[self presentModalViewController:controller animated:YES];
[self.navigationController pushViewController:controller animated:YES];
All of the information in my storyboard is displayed but there is no "back" button to the SWRevealViewController.
If I push to the view controller using
PhotosViewController *frontViewController = [[StreamScreen alloc] init];
newFrontController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
Then I can view everything that I have added programmatically but nothing from the storyboard.
My question is how can I access things from both storyboard and things Ive added programmatically.
if you present the view controller then it will not give you default back button because when you present a controller it will not added in navigation stack of NavigationController so it won't give you that option.
If you want push controller do not use presentModalViewController.
Try like below
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PhotosViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"PhotosViewController"];
[self.navigationController pushViewController:controller animated:YES];
and if you want to present controller then create manually add a back button like we have default in navigation back button and on it's click write below code to dismiss the controller.
[self dismissViewControllerAnimated:YES];
Hope this helps you.

How to pop all view and go back to login view when user tap logout button

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

Resources