Iphone Log Out from App problem - iphone

i have an application based on an navigation controller. At first, it presents a Modal View with the Log In interface. After the user logs in, i dismiss this view and present the rootViewController.
Then, in the nav bar i have a button in order to show the Profile view in wich i can log out.
The problem is the following. When i press log out in the Profile View i do two things:
Present a new Modal View with the log in screen.
Pop the profile view controller.
But for a moment, i can see the pop action and the RootViewController before the Log In View is shown.
How can avoid this behavior?
Thanks

You need to specify that you do not want to see the animation. Where are you popping the last view controller from?
It should probably be something like this
[navController popViewControllerAnimated:NO];

Hey,
I think you must have the animated flag either on - (void)dismissModalViewControllerAnimated:(BOOL)animated or
on - (UIViewController *)popViewControllerAnimated:(BOOL)animated set to YES so you are actually able to see it switching views. If you change them to NO the "Log in" view will come up right away.
I hope this is useful for you.
Regards

Related

Xcode: Display Login View in applicationDidBecomeActive

In my app I would like to show a login screen - which will be displayed when the app starts and when the app becomes active. For reference, I am using storyboards, ARC and it is a tabbed bar application.
I therefore need to do the process in the applicationDidBecomeActive method:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if ( ... ) { // if the user needs to login
PasswordViewController *passwordView = [[PasswordViewController alloc] init];
UIViewController *myView = self.window.rootViewController;
[myView presentModalViewController:passwordView animated:NO];
}
}
To an extent this does work - I can call a method in viewDidAppear which shows an alert view to allow the user to log in. However, this is undesirable and I would like to have a login text box and other ui elements. If I do not call my login method, nothing happens and the screen stays black, even though I have put a label and other elements on the view.
Does anyone know a way to resolve this? My passcode view is embedded in a Navigation Controller, but is detached from the main storyboard.
A variety of answers finally led me to an answer which doesn't seem too complicated so I will post it here - and it actually looks really good if I am honest.
Firstly, my password view is embedded in a Navigation Controller (Editor -> Embed In) and this is connected to the main tab bar controller using a modal segue with an id, in my case 'loginModal'.
In the applicationDidBecomeActive method put something like this:
[self performSelector:#selector(requestPasscode) withObject:nil afterDelay:0.2f];
And then put this function somewhere in the App Delegate
-(void)requestPasscode{
if ( /* If the user needs to login */ ) {
[self.window.rootViewController performSegueWithIdentifier:#"loginModal" sender:self];
}
}
This will present your login view whenever the app begins or enters the foreground (for example, when switching apps).
NOTE: The above line will not work if the root of your app is embedded in a navigation controller.
There are however two bugs;
If the user was previously viewing a modal view when they dismissed the app
If the user dismissed the app on the password view.
Both of these cause the app to crash so the following line goes in the applicationWillResignActive method.
[self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
It basically dismisses all modal views that are presented. This may not be ideal, but modal views are more often then not, used for data entry and so in many cases, this is a desired effect.
You should init PasswordViewController viewcontroller from xib or if you store UI in Storyboard you should use Segue for present this controller.
I can't say about another parts but that part seems to me very weird.
My passcode view is embedded in a Navigation Controller, but is detached from the main storyboard.
in storyboards you can store view controllers and view inside of view controllers so it's not good to store some view outside of viewcontroller because you will not be able to load this view from storyboard after receiving memory warning. Please correct me if I didn't get what do you mean.
If we are going by your way there is no difference load PasswordViewController at applicationDidBecomeActive or at your first view controller at Storyboards because you calling present view controller from first loaded view controller. So you can do it in your first view controller. Also you can store some hidden view inside of your first viewcontroller and show this view if the user needs to login.
I tested it. So at first your controller become loaded and then you got method applicationDidBecomeActive. So it's better to put your code inside -(void)viewDidAppear:animated method of your first viewcontroller.
Best regards,
Danil

setting a tab bar controller view controller programmatically

Is there a way to set the view controller on the tab bar controller programmatically? Lets say I want it to show the second's tab view controller programmatically, is there a way to do that?
This is useful if I logout from my app, which is done from my third tab, when the user logins it should start from the 1st tab again. When I logout I am just showing a present modal view controller on top of what the previous view is, so I somehow needs to reset it again to the first tab bar without re-initializing it all over again.
The issue is now how do I do this?
From Apple's documentation it looks to me like you could just call the following two functions:
[myTabBarController setSelectedIndex:0];
[myTabBarController setSelectedViewController:[myTabBarController.viewControllers objectAtIndex:0]];
Hi ye you can do this
You might have tabbarcontroller object in appDelegate.
So on logout button
make object on your appDelegateClass and do this:-
appDelegate.tabBarController.selectedIndex=0;
Have a look at the reference on UITabBarController. Work with selectedIndex and selectedViewController.

Navigation Bar Back Button Pop View Controller

When hitting the Back button on a navigation controller, does the view get popped off the stack? If so, how can I test that or make sure that it does it correctly (in other words, created a proper navigation controller)?
I need to see if the view is actually getting popped. Is there anything I can NSLog that shows me the stack or something?
Thanks,
Yes, the view is popped from the stack. You can check the size of the stack (number of views) to confirm this.
The viewController is getting popped from the stack yes. You can also NSLog(#"%#", self.navigationController.viewControllers); in - (void)viewWillAppear and - (void)viewDidAppear methods of the parent viewController to see the differences if you don't trust that Apple engineers did a good job with it.
NSLog(#"%#", self.navigationController.viewControllers); would give the viewcontrollers array in navigation stack.you can nslog them before the view disappears and after the other view appears

Refresh View when Tab becomes active

I have a TabBarApplication for an iPad App, which is switching between two ModalViews (LoginForm / Memberarea) in one of the Tabs by checking Loginstatus. All works fine, but when I switch to another Tab of the Application and then switch back, no modal view is shown and the view don't refresh to check for status again.
Is there any way to keep the modal view on the TabView, even if user switches to another tab?
Or is it possible to refresh the View when its tab becomes active?
Would be great if someone can help me with this problem!
EDIT: Problem solved!
I solved it on my own. =)
The problem was: After switching to another TabView the modal don't show up, but is not dismissed. There was a error in my log displaying that the modal can't be viewed.
So to solve it I used the "viewWillDissapear" method and dismiss my modalView before switching Tabs, like this:
[self dismissModalViewControllerAnimated:YES];
The Modal is dismissed, and after switching the View loads again and displays the deserved modalView. =)
Thanks for your answers.
You can do the refresh code you were talking about by implementing viewWillAppear in your view controller
Call your code which clls modalview controller in viewwillappear method.

iPhone Development, How to Programmatically Call a NavigationController back button from another tab?

Here's the setup:
I have a tab bar controller with two tabs. There is a navigation controller on top the second tab, so that I can view details from a table.
On tab #1, I have a button. When this button is pressed, it switches the selected tab over to tab #2.
My problem is this: Let's say I go to tab #2, then I select a line from my table to view the detailed information. Then, instead of hitting the "back" button to return to the base view, I select tab#1. Then I press my button. Tab#2 loads, but it is still showing the detailed view, instead of the table view that I want.
I know how to programmatically press the button.
[self.navigationController popViewControllerAnimated:YES];
But how would I do that when the button pressed method is inside the class for tab#1, which knows nothing of tab#2's navigation controller?
Hope that makes sense. Thanks in advance for any help!
Okay, I finally got it to work. The solution that was suggested gave me a good jumping off point. Here is the code that finally worked for me:
ARFinderAppDelegate appDelegate = (ARFinderAppDelegate)[[UIApplication sharedApplication] delegate];
[appDelegate.booksNavigationController popViewControllerAnimated:YES];
self.tabBarController.selectedIndex = 1;
I had to access the class that contained the NavigationController through an instance of my application delegate. I also had to remove the "objectAtIndex:1" because my application would crash.
Thanks for all the help, this has been a great learning experience for this iPhone development newbie!
use your delegate singleton.
[[delegate.tabBarController.viewControllers objectAtIndex:tab2Index] popViewControllerAnimated:YES];