How can i make it so the nav bar is hidden on the first view only, i have some code that will let me do it but if i go to my second view then back to my first i can see the nav bar slide up is there any way to make it so it doesnt do that. here is the code i have.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:(BOOL)animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
In first view
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
In second View
-(void)viewDidLoad:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
You can just have an instance variable and keep track of whether it's the first time the view is appeared, ie. BOOL isFirstTime make sure to set it to YES in your viewDidLoad and pass it to setNavigationBarHidden: in viewDidAppear:
- (void)viewDidLoad
{
[super viewDidLoad];
isFirstTime = YES;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:(BOOL)animated];
[self.navigationController setNavigationBarHidden:isFirstTime animated:animated];
[super viewWillAppear:animated];
isFirstTime = NO;
}
Do something like this:
#interface MyViewController : UIViewController
#property (nonatomic) BOOL shouldHideNavBar;
#end
#implementation MyViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:self.shouldHideNavBar animated:animated];
[super viewWillAppear:animated];
self.shouldHideNavBar = YES;
}
#end
The first run though, the default value for shouldHideNavBar is NO. After viewWillAppear gets called for the first time, it is set to YES. So, next time viewWillAppear is called, your nav bar will be hidden.
Related
I am trying to build a splash screen so I want the 1st View Controller to move to 2nd ViwController automatically after 3.0 sec I have tried the below method but an infinite loop has started what should i do ,how should I stop on second view controller.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"%p", self);
NSLog(#"1st Controller");
[self.navigationController setNavigationBarHidden:YES animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self loadingNextView];
});
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)loadingNextView{
LoginViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
//LoginViewController.h
#interface LoginViewController : ViewController
#end
//LoginViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"2nd View Controller");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
see after three seconds how the loop is working .
Why don't you use NSTimer class. Simply create a timer for 10 seconds when 10 seconds passed timer will trigger the event and in that event you can move to another controller. Create a timer like this
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:NO];
- (void)targetMethod:(NSTimer*)timer {
[self loadingNextView];
}
It is easy to do, you can add a property to judge if is first come in the vc1:
The result:
In fitst VC:
#import "ViewController.h"
#import "LoginViewController.h"
#interface ViewController ()
#property(nonatomic, assign) BOOL isFirstTime;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_isFirstTime = YES;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
/* [self performSelector:#selector(loadingNextView)
withObject:nil afterDelay:1.0f]; */
if (_isFirstTime == NO) {
return;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self loadingNextView];
_isFirstTime = NO;
});
}
- (void)loadingNextView{
LoginViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
Try this
[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:false block:^(NSTimer * _Nonnull timer) {
[self loadingNextView];
}];
So a little advise, you should think about presenting a loginViewController instead pushing it to the NavigationController stack. If you push it, you have to remove the back buttons if you don't want the user to come back to the other ViewController. If you present it you can be sure that the user can't go back to the firstVc without entering his login data.
In the second Vc you then can dismiss the Vc or you can present a new ViewController.
[self presentViewController:vc animated:YES completion:nil];
You can achieve you goal by using this.
#import "DrawingViewController.h"
#import "SecondViewController.h"
#interface DrawingViewController ()
#end
#implementation DrawingViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[button removeFromSuperview];
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(target:) userInfo:nil repeats:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)target:(NSTimer*)timer {
SecondViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
Finally got the answer.
#import "ViewController.h"
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"%p", self);
[self.navigationController setNavigationBarHidden:YES animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSLog(#"1st controller");
[self loadingNextView];
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadingNextView{
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
//LoginViewController.h
#import "ViewController.h"
#interface LoginViewController : UIViewController
#end
//LoginViewController.m
#import "LoginViewController.h"
#interface LoginViewController ()
#end
#implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"2nd View Controller");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
I', performing a basic push segue (working with nibs) and from one view controller to another table view controller, and from some reason the 'Back' button is not appearing, that normally it does appear.
This is how i'm performing the push:
#interface HomeViewController ()
#end
#implementation HomeViewController
- (id)init {
self = [super initWithNibName:#"HomeViewController" bundle:nil];
if (self) {
// Do something
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (IBAction)goToStack:(id)sender {
StackTableViewController *stackViewController = [[StackTableViewController alloc] initWithNibName:#"StackTableViewController" bundle:nil];
[self.navigationController pushViewController:stackViewController animated:YES];
}
In your StackTableViewController, you should set setNavigationBarHidden to NO
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
To make sure the navigation bar reappears every time you navigate away from your view controller (HomeViewController), use this:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
I have view controller transition like vc1 -> vc2 -> vc1. And I need the UINavigationBar be hidden in vc1, and showing in vc2.
I do the following:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.navigationBarHidden = NO;
}
In iOS6, it works fine. But in iOS7, when I back to vc2 from vc1, the navigation bar is not hidden but moved upward behind the status bar, the bar got hidden after the transition animation finished.
How can I really hide the nav bar?
Try like this:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO];
}
or try like this :
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
In VC2 Controller put this in - viewDidLoad
[self.navigationController setNavigationBarHidden:NO];
In VC1 controller put this in -ViewDidLoad
[self.navigationController setNavigationBarHidden:YES];
then VC1 add this also
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
}
I hope it will be helpful for you...
So, I think that when I click outside of a popover, the method popoverControllerDidDismissPopover should be called. I know this isn't called when dismissPopoverAnimated is called.
I have a simple project that I have setup that shows popoverControllerDidDismissPopover just isn't called:
#import "ViewController.h"
#import "PopoverViewController.h"
#interface ViewController ()
{
PopoverViewController *controller;
UIPopoverController *popoverController;
}
#end
#implementation ViewController
#synthesize button;
- (IBAction)showPopover:(UIButton *)sender
{
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
} else {
CGRect popRect = CGRectMake(self.button.frame.origin.x,
self.button.frame.origin.y,
self.button.frame.size.width,
self.button.frame.size.height);
[popoverController presentPopoverFromRect:popRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
controller = [[PopoverViewController alloc] initWithNibName:#"PopoverViewController" bundle:nil];
popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
NSLog(#"Why am I never called!!!!");
}
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
return true;
}
#end
Please tell me where I'm going wrong or how I can detect when a popover is dismissed.
The whole project is here:
https://rapidshare.com/files/3182903825/PopoverDemo.zip
You never set the delegate for your popoverController to self.
_popoverController.delegate = self;
You didn't set the delegate of your popoverController. Add the following code to the end of the viewDidLoad method:
popoverController.delegate = self;
Good day to you guys
I have an application that has a UITabBarController for tabbed-navigation... The view-controllers are mapped to their respective TabItems via a URL, just the same as that of Three20's TTNavigationSample App.
My problem is that inside a view controller of mine, i have a button that calls to another view controller which is also attached to a TabItem. When i trigger the button, the application throws an error. How can I resolve this?
In my TabBarController, i have this inside the viewDidLoad method:
-(void)viewDidLoad {
[self setTabURLs: [NSArrayWithObjects:
#"tt://bulletinBoard",
#"tt://contacts",
nil
]];
}
Sample .m file
#import "HabBarController.h"
#implementation TabBarController
- (void)viewDidLoad {
//these are variables like "tt/feed"
[self setTabURLs:[NSArray arrayWithObjects:
kAppFeedURLPath,
kAppHotURLPath,
kAppPostPhotoURLPath,
kAppGeneralActivityURLPath,
nil]];
}
- (UIViewController*)rootControllerForController:
(UIViewController*)controller {
if ([controller canContainControllers]) {
return controller;
} else {
UINavigationController* navController = [[[UINavigationController
alloc] init] autorelease];
[navController pushViewController:controller animated:NO];
return navController;
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.tabBarController.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
#end