I just want to be able to swipe to a new nib, but am getting stuck. My code builds properly with no errors or warnings, but nothing happens.
Here is my code
- (IBAction) handleSwipeGesture:(UIGestureRecognizer *) sener {
NSLog(#"swipe left");
if(UISwipeGestureRecognizerDirectionLeft) {
SecondDetailViewController *tempController = [[SecondDetailViewController alloc]
initWithNibName:#"SecondDetailView"
bundle:nil];
newController = [tempController retain];
[tempController release];
}
}
The system sees that I am swiping (it gets logged) but it doesn't go to the new nib.
I'm not really married to my code, so if I need to completely rewrite this I'm OK.
Assuming you're handling swipe gesture inside a view controller (and you are using navigation controller), you should do something like this:
- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)sender {
if(sender.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(#"swipe left");
SecondDetailViewController *tempController = [[SecondDetailViewController alloc]
initWithNibName:#"SecondDetailView" bundle:nil];
[self.navigationController pushViewController:tempController animated:YES];
[tempController release];
}
}
This line pushes tempController on top of the navigation controller:
[self.navigationController pushViewController:tempController animated:YES];
if you want to present tempController modally, you should call this instead:
[self presentModalViewController:tempController animated:YES];
You are not doing anything with the newController object after you create it. You need to either push it onto your nav controller, present it modally, or grab the view and put it on the screen.
Related
In my app after pressing the login button in my loginViewController, it push to front viewController of my SWRevealViewController. In my RearViewController I have the sign out button. When I press it it should pop to the back viewController again (LoginViewController). But even though I set the button click event like this it doesnt navigate to the loginViewController.
-(void)signOutClick :(id)sender
{
[self performSelectorInBackground:#selector(moveToLoginView) withObject:nil];
}
-(void)moveToLoginView
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
But when I swipe the view it gose to the back view.I want to disable that swipe feature to the back view. And I want to go to the back view when click on the sign out button in my RearViewController. How can I do that? Please help me.
Thanks
I am not sure but instead of using
[self.navigationController popToRootViewControllerAnimated:YES];
try using,
[self.navigationController popViewControllerAnimated:YES];
And to stop swipe gesture you can use code like,
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.revealViewController.panGestureRecognizer.enabled = NO;
}
You need to reset the top view controller if you want to perform these kind of actions off of a click. E.g.
get an instance of the ECSlidingViewController
ECSlidingViewController *slidingController = self.slidingViewController;
get the view controller you wish to be on the top
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"mainViewController"];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
set the topViewController property
slidingController.topViewController = nc;
[slidingController resetTopViewAnimated:NO];
I am developing an iOS app which contains login/authentication functionality - basically first time a user logins, in they need to enter relevant login details - then they are passed to main app screens - subsequent visits to the app they will be automatically authenticated.
All above works fine - the issue I have is with the Navigation bar - it appears in the main screen in the main part of the app with a back button - I don't want this to be displayed as they should not be able to return to the login screen once authenticated. I guess it's using the root navigation controller which explains the logic, but is there a way to ignore the navigation controller of the login section so the back button is not displayed in the main app.
Below is a screenshot of the structure to help my explanation - left hand group of screens are the login process right hand is the main app structure.
The code used to switch screens is as follows -
SWRevealViewController *swRevealController = (SWRevealViewController *)navVC;
swRevealController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:controller animated:YES];
Don't push view controller. Create new hierarchy with:
Objective-C
[self.navigationController setViewControllers:#[controller] animated:YES];
Swift
navigationController.setViewControllers([controller], animated:true)
In the Screen which implements after login, the ViewDidLoad method add the line to hide back bar button.
self.navigationItem.hidesBackButton = YES;
Additionally you can add an 'Logout' option as
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonItemStyleBordered
target:self
action:#selector(LogoutClick)];
self.navigationItem.leftBarButtonItem = backBarButton;
-(void)LogoutClick {
[self showLogoutAlert];
}
-(void)showLogoutAlert {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#""
message:#"Do you want to Logout ?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Logout", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
Use a modal view controller for the login screen so its not part of your navigation controller hierarchy within your navigation root probably in view did load - sample code below:
- (void) viewDidLoad {
......
LoginVC *loginViewController = [LoginVC alloc] init];
loginViewController.parent = self;
[self presentViewController: loginViewController animated:YES completion:NULL];
return;
}
You may or may not dismiss the modal view from the root - if you do, you will need to be able to call the dismiss routine from loginViewController but there are other ways as well and you can put the dismiss inside the modal view (loginViewController)
(void) login_exit:(BOOL)success {
[self dismissViewControllerAnimated:YES completion:NULL];
if !(success) {
.... send message (UIAlertview and ask if he or she wants to try again)
}
else {
}
return;}
Very simple.. Just navigate to rootviewcontroller
SWRevealViewController *swRevealController = (SWRevealViewController *)navVC;
swRevealController.managedObjectContext = self.managedObjectContext;
//-- I think above lines not needed as per your question
[self.navigationController popToRootViewControllerAnimated:YES];
You need to hide the Navigation Bar in the ViewController before you push the SWRevealViewController by using the below line in viewDidLoad
Objective C
self.navigationController.navigationBarHidden = true;
Swift
self.navigationController?.navigationBarHidden = true;
It will not show the back button in the next view controller pushed
2022 answer
#IBAction func SomeScreen() {
let s = UIStoryboard ...
navigationController?.isNavigationBarHidden = true;
navigationController?.pushViewController(s, animated: true)
}
It's that easy.
Write this in viewDidLoad method
self.navigationItem.hidesBackButton = YES;
This will hide the back button for the first view.
ClassA* controller = [storyboard instantiateViewControllerWithIdentifier:#"ClassAIdentifier"];
if (controller != nil) {
[self.navigationController pushViewController:controller animated:YES];
[controller.navigationItem setHidesBackButton:YES animated:NO];
}
You MUST hide the navigationItem after push, otherwise it will be nil.
Is is possible to have a UIView preloaded so that it will load faster when the user taps on button to load it? Currently I've got a library of informaiton that I'm attempting to load when the user taps a button, and for now it seems to be "ok" , but it makes the navigation to the page choppy, because of all the information in the library it's loading.
Thanks in advance!
It should be possible to split the setup of a View Controller from code that displays the view after a button is pushed. This will eliminate the lag when the button but the task to setup the view controller still need to be done sometime during execution (You can for example put it in the ViewdidAppear method so it is executed while waiting for the button to be pushed.
Take this code for example:
-(IBAction) button_pushed {
/*setup */
NewVC *vc = [[NewVC alloc] init];
vc.var1 = var1;
vc.var2 = x;
[vc setup];
/*display */
[self.navigationController pushViewController:vc animated:NO];
return;
}
You can split the code that setup the view from the code that displays the view
into :
#synthesize vc;
…..
- (NewVC) setup {
//setup
NewVC *vc1 = [[NewVC alloc] init];
vc.var1 = var1;
vc.var2 = x;
[vc setup];
return(vc1);
}
-(void) ViewDidAppear {
if (setupready) {
vc = [self setup];}
return;
}
-(IBAction) button_pushed:(ID) sender {
//display
[self.navigationController pushViewController:vc animated:NO];
return;
}
I'm confused. I have a navigation controller with a BarItem which opens a first view. After some work is done, I want this view to disappear and I want a second view to open.
root view: navigation controller
first view: activity indicator, where some data is put together
second view: MFMailComposeViewController
In the root view, the BarItem runs these lines to open the first view:
IndicatorViewController *indicator = [[IndicatorViewController alloc] initWithNibName:#"IndicatorViewController" bundle:nil];
indicator.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:indicator animated:YES];
The first view (IndicatorViewController) does some work and finally runs
[self dismissModalViewControllerAnimated:YES];
This works fine. But - how do I open the second view?
I tried this:
I open the second view. After closing the second view, my first view pops up again (since it is still there) and get's dismissed at this point. This code is placed in the first view:
- (void) viewDidAppear:(BOOL)animated {
static BOOL firstTime = YES;
if (firstTime) {
//do stuff that takes some time (that's why I show the indicator)
MailViewController *controller = [[MailViewController alloc] init];
if (controller)
[self presentModalViewController:controller animated:YES];
firstTime = NO;
} else {
[self dismissModalViewControllerAnimated:YES];
}
}
Since the first view pops up again, the user can see the indicator one more time, after the second view is closed - and that is not what I want.
What am I missing here? What would be the better way to do this?
I would do something like this. Make a navigationController, and make the first view as the root controller. Then do something like this:
FirstView.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
}
- (void) nextView { // however you get to your next view, button/action/etc.
UIViewController *screen = [self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
[self.navigationController pushViewController:screen animated:YES];
}
Then in the second view:
SecondView.m
- (void) nextView { // however you get to your next view, button/action/etc.
UIViewController *screen = [self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
[self.navigationController pushViewController:screen animated:YES];
}
And finally in the rootview:
RootView.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *navStack = [NSArray arrayWithObject:self];
self.navigationController.viewControllers = navStack;
[self.navigationController setNavigationBarHidden:NO];
}
This will make your RootView the new rootview of the NavigationController.
self.navigationController.viewControllers
is the array with all the ViewControllers that are on the navcontrollers stack. The first object is the rootcontroller. If we replace the whole array with our own array, it knows only one item. You CAN go back by dismissing if that's what you want though. This isn't the prettiest way of doing it, but it's not the worst either.
I am new to iPhone programming. what I am trying is I have one screen with a button. And I want to change the view controller not only the view when I click that button (I know how to add subview) because from that 2nd view controler, I have to go to the third view which is not possible if I add subview in first place. Can anybody please help me with that? Is this possible? and if yes, how? All the views and view controller are created programmaticaly. I am not using IB.
EDIT: here is the relevant code that fires when clicking button
-(id)showCurrentLoc:(id)sender {
locationController = [currentLocController alloc];
[entry removeFromSuperview];
[newLoc removeFromSuperview];
[currentLoc removeFromSuperview];
[self.view setBackgroundColor:[UIColor clearColor]]; //[self.view addSubview: [locationController view]];
[self.navigationController pushViewController:locationController animated:YES]; [locationController release];
return 0;
} //Location Controller is the tableViewController
Thanks
Vik
You can do something like this
YourViewController *objYourViewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
[self.navigationController pushViewController:objYourViewController animated:YES];
[YourViewController release];
Usually, you use a navigation controller for this sort of thing so that the user can easily go back to the previous view. Your view controller would then do something like this:
[self.navigationController pushViewController:someNewViewController animated:YES];
If you want to manage the view controllers yourself, you can always just change the window's rootViewController property. Please read View Controller Programming Guide for the complete picture.
UINavigationController is what you need. It manages a stack of UIViewControllers and if you want to add new UIViewController just push it into this navigation stack. It automates back button behavior for you, and you can pop your current UIViewController from stack whenever you are done with it.
You could work with a UINavigationController. Adding your first UIViewController like this in the init method:
[self setViewControllers:[NSArray arrayWithObject:viewController]];
And then when a button is click or a choice is made, your push the second few controller with (in the first viewController):
[self.navigationController pushViewController:controller animated:YES];
This way you will also get an automatic (back button). Basicly you create a stack of UIViewControllers that you can push and pop like with a normal stack.
I hope this helps. Look into the following:
UINavigationController Class Reference
- (void) loadViewAtIndex:(NSInteger)index {
[self unloadViewAtIndex:activeViewIndex];
switch (index) {
case 1:
{
if (viewController1 == nil)
{
viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
}
viewController1.view.frame = CGRectMake(0.0, 0.0, viewController1.view.frame.size.width, viewController1.view.frame.size.height);
[window addSubview:viewController1.view];
}
break;
case 2:
{
if (viewController2 == nil)
{
viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
}
viewController2.view.frame = CGRectMake(0.0, 0.0, viewController2.view.frame.size.width, viewController2.view.frame.size.height);
[window addSubview:viewController2.view];
}
break;
default:
break;
}
activeViewIndex = index;
}
- (void) unloadViewAtIndex:(NSInteger)index {
switch (index) {
case 1:
{
if (viewController1 != nil)
{
[viewController1.view removeFromSuperview];
[viewController1 release];
viewController1 = nil;
}
}
break;
case 2:
{
if (viewController2 != nil)
{
[viewController2.view removeFromSuperview];
[viewController2 release];
viewController2 = nil;
}
}
break;
default:
break;
}
}