ModalViewController goes to background on application start - iphone

I am migrating my app code base to iPhone SDK 4. In one of the screens I use
-(IBAction) LoadSearch {
MySearchViewController *mySearchController=[[MySearchViewController alloc] initWithNibName:#"MySearchViewController" bundle:nil];
[self.navigationController presentModalViewController:mySearchController animated:YES];
}
That launches the ModalViewController. While the Modal is open, if the user clicks home button, the application goes to suspended state.
If I relaunch the application, immediately after launch the Modal becomes invisible. It seems like the Modal goes to the background of navigationController views.I can see the modal when I rotate the iphone to landscape mode (the modal is being rotated appropriately).
Has any one seen this issue before? Any suggestions are appreciated.

Solved this issue my self.
I was calling the [window addSubview:tabBarController.view] in applicationDidBecomeActive.
So, this resulted in adding tabBarController.view to the window and any modalViewControllers are pushed to background.

Related

How to re-present modalViewController on iOS

My app has two windows, mainWindow (for almost all views) and loginWindow (just for login view).
When applicationDidEnterBackground I switch mainWindow with loginWindow:
[self.loginWindow makeKeyAndVisible];
[self.mainWindow setHidden:YES];
When I came back in app the loginWindow with its views is shown. That works just fine.
However I have a problem with modalView presentation in mainWindow.
After applicationDidEnterBackground windows are switched again. After going back to mainWindow the modalView is not visible.
Method self.modalViewController gives me the exact modalView that was presented earlier, but is not visible anymore.
I've tried to show this modalView again with no luck like this:
[self presentModalViewController:self.modalViewController animated:NO];
How can I "re-present" this hidden modalView again?
Probably you need some logic here. Like Crio stated, dismiss it when entering the background, but remember it in a property or smth. else.
// applicationDidEnterBackground
self.savedModalViewController = self.modalViewController;
[self.mainWindow dismissModalViewControllerAnimated:NO];
// after login but before presenting the mainWindow again
[self.mainWindow presentModalViewController:self.savedModalViewController animated:NO];
If your showing the "modalView" modally, then on applicationDidEnterBackground dismiss the modal view:
[self.mainWindow dismissModalViewControllerAnimated:NO];
and then the modal should work fine.

pop one or more viewControllers when app enters in foreground

My app downloads data from internet when it starts, during the splash screen.
It does the same when, from the background, it enters in foreground (when the user open the app from the background).
When the app is open, the user can push some views in order to read the informations downloaded.
I want that when the app is open from the background state, the viewControllers are popped until the first view is showed...
I want to do something like this in my AppDelegate:
while ([self.view isNotMainView]) //of course this method doesn't exists
{ [self.navigationController popViewControllerAnimated:NO]; }
is it possible?
Thanks
Just use:
[self.navigationController popToRootViewControllerAnimated:NO];
Hope that Helps!
hope Following link will help...
How are people popping their UINavigationController stacks under a UITabBarController?
You can keep a reference or Current navigation controller in your appdelegate OR you can write this in you viewDidUnload OR viewWillDisapper for popping navigation to root when application goes to background.
You could just compare the view against your main view if you have a reference hanging around:
while (self.view != myMainView)
etc. (Assuming that self.view is the correct reference as well.)

load view controller when application becomes active

I will like my application that always starts as the first time I open it. I have several view controllers and when I exit my application and I open it again I see the view controller where I left of. Maybe I have to call the method applicationWillTerminate method.
I use this code to open a new view:
UIViewController *control = [[SomeViewController alloc] initWithNibName:#"SomeViewController"
bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc]
initWithRootViewController:control];
[self presentModalViewController:navControl animated:NO];
[navControl setNavigationBarHidden:YES];
[control release];
[navControl release];
this code works great when linking it to buttons. But when I place that code in the applicationDidBecomeActive method it does not work.
The easiest way is to set UIApplicationExitsOnSuspend in Info.plist.
That really isn't the expected behaviour, though. Users expect to see the app "where they left off", especially if they've only briefly left the app (e.g. because they got a phone call).
Your code snippet adds a view controller, but is unlikely to work since your app delegate is not a UIViewController. It also doesn't do anything about removing the old view controllers.
EDIT: If all you need to do is display a splash screen (or something), then it's something like this:
In -applicationDidEnterBackground:, add a "splash screen" view (not a view controller) to self.window. (iOS takes a "screenshot" after you return from -applicationDidEnterBackground: and uses this in the app-switch animation; you want this to be what the user sees when switching back to your app)
In -applicationWillEnterForeground:, do whatever animations you want and eventually remove the view from the window (call -removeFromSuperview).
EDIT 2: The same will work in -applicationWillResignActive:/-applicationWillBecomeActive:, except this happens on a sleep/wake event, which might not be what you want...
I'd avoid using view controllers for this, because trying to shoehorn a view controller in the view controller hierarchy is likely to be problematic (for example, you have to figure out which VC to present it from, and you have to do the "right thing" if the user backgrounds your app while the VC is on screen, and...)
The reason it doesn't work in applicationDidBecomeActive is that method is only sent to the Application delegate, which doesn't know about presentModalViewController.
I suggest instead that in your appDelegate, implement applicationWillEnterForeground:, which should restore the state to a newly launched application (equivalent to what the state is at the end of application:didFinishLaunchingWithOptions: ).
OR...(edits)
If you just want a certain viewController to run (which is still loaded, right?)...For example, if you have a tab controller and just want to go to the root of the first view controller, put the following code into applicationWillEnterForeground:
UITabBarController * myTabBar = self.tabBarController;
myTabBar.selectedIndex = 0;
[[myTabBar.viewControllers objectAtIndex:0] popToRootViewControllerAnimated:NO];
Temporary solution:
I made my application crash on applicationWillResignActive method and it works. My application needs to run an animation when I launch it. But this works because next time the application runs it starts like the first time I opened it.

iphone - Modal view controller disappears?

So let's say I have a viewController named homeViewController, and another view controller named listViewController
I display listViewController on top of homeViewController as a modal.
If the user clicks the off button, and then comes back to the app the modalViewController is gone.
ListViewController *listViewController = [[ListViewController alloc] init];
[self presentModalViewController:listViewController animated:NO];
[listViewController release];
Note: Application doesn't startup from scratch when this occures and the previous state is still visible
I'm assuming that by "off button" you mean the user locks the iDevice.
I just tried this in one of my apps and the modal view controller is still there after unlocking. My guess would be that it's something unrelated to the code you have posted. I would check your - (void)applicationWillResignActive:(UIApplication *)application method in your app delegate class and see if there's anything there that would dismiss the modal view controller.
Here is what the problem was.
When the user locks the screen I remove homeViewController from window
[homeViewController removeFromSuperview];
When user starts the app again I do
[windows addSubview:homeViewController];
that brings homeViewController on top of its modeal

IPhone UITextField not showing edit caret or clearing placeholder text when tapped

I have a window within an iPhone application, which is displayed modally to allow the user to enter their settings for a web service upon 'first run'.
The text fields have helper text set, and when you tap them the keyboard shows and allows you to enter text.
Unfortunately the text fields do not clear the helper text, show the edit caret or show the text being entered (as in the screenshot below).
Any suggestions?
The window is being displayed with [self presentModalViewController:<controller_name> animated:YES];, which may or may not be the cause of this issue - when I run the UI via the Interface Builder 'test' application the text boxes respond like normal.
Clear when editing begins has been set for both fields.
Thanks in advance!
Edited: More information
After the info Bart Gottschalk provided I thought I should add some more information. First, the application is a Navigation Based Application.
Secondly, the test app Bart recommended worked fine, so that takes the modal window and the view out of the equation.
Third, I was presenting the modal view when the -(void)viewWillAppear... delegate method was being called - which may very well be the wrong place... however I'm not 100% sure if I should be presenting the modal view from within the didFinishLaunchingWithOptions of the App Delegate...
(this is happening on Simulator and iPhone 3.1.3)
In Interface Builder did you check the box for "Clear When Editing Begins"? With that checked the text field should clear any value once the use taps to edit which is the behavior I think you're looking for.
You can also set the same property programatically using clearsOnBeginEditing if that is convenient in your code.
My guess is that you've done this and it's not behaving as you expect. Just checking on this as a first step in helping you debug.
Also, does this happen in both the Simulator and on a testing device?
Bart
Edited Below...
This seems strange. Let's strip away everything but the basics of presenting a modal view when the application starts and see what happens.
I've recreated the most basic app (that I know of) to test presenting a modal view controller at launch and verify that field editing works fine. What happens for you when you do the same/similar in a new project?
Here is what I'm doing:
1) Create a new view-based app in Xcode called "ModalViewTest"
2) Create a new UIViewController with xib called ModalViewController
3) In ModalViewController.h add a method
-(IBAction)closeModalView;
4) In ModalViewController.m add the method implementation as
-(IBAction)closeModalView {
[self dismissModalViewControllerAnimated:YES];
}
5) In the ModalViewController.xib create two text fields and set the placeholder text for each to abcd1234 and confirm that "Clear When Editing Begins" is checked.
6) In the ModalViewController.xib add a button "Close" and set Touch Up Inside to fire "closeModalView"
7) In the application delegate (ModalViewTestAppDelegate) add the following import
#import "ModalViewController.h"
8) In the application delegate (ModalViewTestAppDelegate) applicationDidFinishLaunching add the following after the line containing [window makeKeyAndVisible];
ModalViewController *modalViewController = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[viewController presentModalViewController:modalViewController animated:YES];
9) Save everything
10) Build and Run this new app
Does editing of the text fields work as expected? If yes, what is different about how you are building and presenting your modalView? If no, then we'll need to dig further to determine what is going on in your environment.
Second Edit Below...
When creating a navigation-based application I did the following to present the modal view at application start. Does this work for you in both your test app as well as your real app?
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
ModalViewController *modalViewController = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[navigationController presentModalViewController:modalViewController animated:YES];
}
Well, I just figured it out, but honestly without the persistence and awesome help from Bart it would have taken much longer and been much more frustrating.
It turns out the problem was that I was using a Window instead of a View in the XIB file. This was why when showing the modal view within the Navigation controller it wouldn't display properly (i.e. only a white screen) and why the UITextField would not work properly when showing the view from the RootViewController.
So, to recap - modal views should have UIView, not UIWindow in the XIB/NIB File.
Thanks for your help Bart!
I have the same problem but in iOS7 only. I solved it by changing the tint color of textField to blue in the Storyboard