Push view controller not working - ios

I'm using UINavigation and UIPageView controller in a UIViewController. I have 3 buttons and here is my code :
- (IBAction)StartButtonClicked:(id)sender {
NSLog(#"start button pressed");
[_Timer invalidate];
_Timer = nil;
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
CardViewController* controller = [storyboard instantiateViewControllerWithIdentifier:#"CardViewController"];
[self.navigationController pushViewController:controller animated:NO];
}
}
- (void)pageViewController {
self.pvc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pvc.view.frame = self.view.bounds;
[self.view addSubview:self.pvc.view];
[self.pvc setViewControllers:#[[viewControllerArray1 objectAtIndex:0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
- (IBAction)rightButtonPressed:(id)sender {
[self pageViewController];
[self.pvc setViewControllers:#[[viewControllerArray1 objectAtIndex:1]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
- (IBAction)leftButtonPressed:(id)sender {
[self pageViewController];
[self.pvc setViewControllers:#[[viewControllerArray1 objectAtIndex:2]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
}
When I click on right button and came back to this view controller my start button's push view not working.

Please check the answer .
iOS - Push viewController from code and storyboard
Don't initialize Storyboard always,
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
You can set the rootViewController from the UtoryBoard itself.
Set initialViewController as a UINavigationController and Set the FirstViewController as RootViewController.
Edit :
For Push use the below code,
CardViewController* controller = [storyboard instantiateViewControllerWithIdentifier:#"CardViewController"];
[self.navigationController pushViewController:controller animated:NO];
and Back. If use the default backButton not need to write any code.
If use are using custom backButton,
[self.navigationController popViewController];
Check 2 things,
Your storyboard identifier is correct.
The root view have navigation Controller.
Your StoryBoard should be like this,

Related

Navigation bar not appearing when pushing a controller from a presented ViewController

I have following code to present a UIViewController (named A) with UINavigationController.
I have kept navigation bar hidden as I do not want it on A by using following code
[self.navigationController.navigationBar setHidden:YES];
but when I push UIViewController named B I want the navigation bar to be displayed but it's not working even If set
self.navigationController?.setNavigationBarHidden(false, animated: false)
on UIViewController B. Can someone help me in this?
Here is the code to present UIViewController A
AppDelegate* delegate = [AppDelegate applicationDelegate];
UIStoryboard *story = [UIStoryboard storyboardWithName:STORYBOARD_MAIN bundle:nil];
SelectionViewController *tutorial = [story instantiateViewControllerWithIdentifier:#"SelectionViewController"];
tutorial.providesPresentationContextTransitionStyle = YES;
tutorial.definesPresentationContext = YES;
[tutorial setModalPresentationStyle:UIModalPresentationOverCurrentContext];
tutorial.delegate = self;
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:tutorial];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
[navController.navigationBar setHidden:YES];
navController.providesPresentationContextTransitionStyle = YES;
navController.definesPresentationContext = YES;
[navController setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[delegate.tabBarController presentViewController:navController animated:YES completion:NULL];
Here is the code to push UIViewController B
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"AppDetail" bundle:nil];
DetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
[self.navigationController pushViewController:vc animated:YES];
[[self navigationController] setNavigationBarHidden:NO];
Additional information
UIViewController A is in different storyboard and B is in different
A's code is written in OBJ-C and B's code is in swift.

Remove a view from controller from any other class?

I have three screens Google Map,Settings,Login.By default google map is visible.When i want to show settings screen then i don't move to settings view controller i just add "Settings"screen as subview of "Google Map"Screen.
menusettings=[self.storyboard instantiateViewControllerWithIdentifier:#"SET"];
[menusettings.view setFrame:CGRectMake(0, 0, 700,600)];
[self.view addSubview:menusettings.view];
After upadating settings i move to login screen
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"iPhone" bundle:nil];
UINavigationController *ppc = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"LOGIN"];
[self presentViewController:ppc animated:true completion:nil];
But settings screen is still behind login screen but it is not removed. I tried two scenerios
1.I tried to remove settings view before moving to login screen
[self.view removeFromSuperview];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"PRG_iPhone" bundle:nil];
UINavigationController *ppc = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"LOGIN"];
[self presentViewController:ppc animated:true completion:nil];
but it does not move to login screen.
2.When i tried removing settings view after moving to login screen
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"PRG_iPhone" bundle:nil];
UINavigationController *ppc = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"LOGIN"];
[self presentViewController:ppc animated:true completion:nil];
[self.view removefromsuperview];
Then it moved to login screen but does not remove settings view
So i thought why not remove Google Map Screen's subview which is setting screen from login screen but i don't know how can i remove a google map screen child view which is settings screen.Please guide how to do this ?
Change this
[self.view removeFromSuperview];
to and try
[menusettings.view removeFromSuperview];
update
[menusettings.view removeFromSuperview];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"PRG_iPhone" bundle:nil];
UINavigationController *ppc = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"LOGIN"];
[self presentViewController:ppc animated:true completion:nil];
Choice-1
[self.view.subviews makeObjectsPerformSelector: #selector(removeFromSuperview)];
choice-2
Initially assign the tag for your View for e.g
menusettings=[self.storyboard instantiateViewControllerWithIdentifier:#"SET"];
[menusettings.view setFrame:CGRectMake(0, 0, 700,600)];
menusettings.tag = 5; //you can use any number you like
[self.view addSubview:menusettings.view];
Remove
UIView *Remove = [self.view viewWithTag:5];
[Remove removeFromSuperview];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"PRG_iPhone" bundle:nil];
UINavigationController *ppc = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"LOGIN"];
[self presentViewController:ppc animated:true completion:nil];
Choice-3
for (UIView *subVie in self.view.subviews)
{
if (subVie.tag == 5)
{
[subVie removeFromSuperview];
}
}

Black screen after button click

I'm quite new to Objective-C. I want to go to another view controller but it shows me only the black screen after the button click. Is there something that I'm missing here?
Here is my code:
- (IBAction)measuring1stscan:(id)sender {
measuringscan *second = [[measuringscan alloc]initWithNibName:nil bundle:nil];
[self presentViewController:second animated:NO completion:NULL];
}
*measuringscan is the name for another view controller
viewConreoller default is no color, it is black. just add second.view.backgroundColor = [UIColor redColor];
- (IBAction)measuring1stscan:(id)sender {
measuringscan *second = [[measuringscan alloc] init];
second.view.backgroundColor = [UIColor redColor];
[self presentViewController:second animated:NO completion:NULL];
}
#rose is right try to give different color and then check.
Still you face same issue then try to run below code.
Embadded viewcontroller into navigation controller and give unique identifier to second viewcontroller.
- (IBAction)measuring1stscan:(id)sender
{
UIStoryboard *st = [UIStoryboard storyboardWithName:[NSString stringWithFormat:#"StoryboardWithName"] bundle:NULL];
measuringscan *second = [st instantiateViewControllerWithIdentifier:#"viewcontrollerIdentifier"];
[self.navigationController pushViewController:second animated:YES];
}
If you are using storyboard then use following
measuringscan *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"StoryboardIdentifier"];
[self presentViewController:controller animated:YES completion:nil];
or
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[NSString stringWithFormat:#"StoryboardName"] bundle:nil];
measuringscan *controller = [storyboard instantiateViewControllerWithIdentifier:#"viewcontroller'sIdentifier"];
[self presentViewController:controller animated:YES completion:nil];
And if you aren't using storyboard instead you are using xib's then use following
measuringscan *controller = [[measuringscan alloc] initWithNibName:nil bundle:nil];
[self presentViewController:controller animated:YES completion:nil];
Points to remember (as said by #rmaddy)
Class name should be start with capital letter
Variable name should start with small letter
It is the naming convention that should be followed by a developer.
For more details click here

How to make a Method display a new ViewController?

How can I make a Method display a new ViewController? I am trying to make the
Currently what I have :
-(void)gameOver{
[groundMovement invalidate];
[guyMovement invalidate];
topGround.hidden = YES;
bottomGround.hidden = YES;
guy.hidden = YES;
scoreLabel.hidden = YES;
gameViewController *viewController = [[UIStoryboard storyboardWithName:#"gameOver" bundle:nil] instantiateViewControllerWithIdentifier:#"gameOverViewController"];
[self presentViewController:viewController animated:YES completion:nil];
if (score > highScore) {
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:#"HighScoreSaved"];
}
}
-(void)terminate{
MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
// do any setup you need for myNewVC
[self presentModalViewController:myNewVC animated:YES];
}
Since your are using storyboards in your project, first make sure that your view controllers have a storyboard identifier. You can set it through the identity inspector in xCode. Then, you can instantiate a new view controller by calling:
YourViewControllerClass *viewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"ViewControllerId"]; // Replace "MainStoryboard" and "ViewControllerId" with your storyboard name and the storyboard id for your view controller respectively
Then, depending on how your needs you can either push the new view controller (if for example you're using a navigation controller):
[self.navigationController pushViewController:viewController animated:YES];
or present it modally
[self presentViewController:viewController animated:YES completion:nil];

Switching back and forth in views

very new to obj-c.
I´ve made a simple MainViewController, loading its view from a xib. (From a standard single view template)
On the main view is a button that takes me to another view. Below is the Method:
-(IBAction)forward:(id)sender
{
tvc = [[TrackViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
[self.view addSubview:tvc.view];
}
On the other view (also loaded from xib) i have a button to take me back to the main view.
Here is my IBAction for returning to the main view:
-(IBAction) back:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
However it does not return to the main view. Not with popToRootViewControllerAnimated: either.
Any response will be appreciated!
This answer applies when you are not using UINavigationBar
In your Forward button you need to write.
TrackViewController *lf = [[TrackViewController alloc]initWithNibName:#"TrackViewController" bundle:nil];
[self presentViewController:TVC animated:YES completion:nil];
and in the Backward screen you should right.
[self dismissViewControllerAnimated:NO completion:nil];
You should load your MainViewController in your AppDelegate in an UINavigationController
MainViewController *mainVC = ...
UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = naviVC;
Then you can go forward in your MainViewController:
-(IBAction)forward:(id)sender;
{
tvc = [[TrackViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:tvc animated:YES];
}
and go back in your other ViewController:
-(IBAction) back:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
If your MainViewController is inside a UINavigationController, then the forward method should look like this:
-(IBAction)forward:(id)sender
{
tvc = [[TrackViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:tvc animated:TRUE];
}

Resources