Show UIAlertView on splash screen - iphone

I used to show a splash screen which in background load some data from web, I also check that if the location of the user is changed from one city to another city I want to show in alert to the user with the message that you are now in "CityName" would you like to see data from this city?
I have tabbed application and I have presented the splash screen as follow in the app delegate class.
SplashViewController *controller = [[SplashViewController alloc] initWithNibName:nil bundle:nil];
tabBarController.view.frame = [[UIScreen mainScreen] bounds];
controller.tabBarController = self.tabBarController;
[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[window addSubview:controller.view ];
//[window addSubview:tabBarController.view ];
[self.tabBarController presentModalViewController:controller animated:YES];
[window makeKeyAndVisible];
[controller release];
Now when I show the alert screen it crash the application with "EXC_BAD_ACCESS" message and the stack trace show that _buttonClick is released in UIAlertView class.
Please advise what should I do, I also tried with UIActionSheet but the same problem with this thing too.
I think there is some problem with the model thing with the current view (SplashView).
Thanks in advance.

Are you trying to display your UIAlertView inside of your SplashViewController viewDidAppear? If not, I would try that first. I would also make sure you have your UIAlertView clickedButtonAtIndex method setup properly to try and trap what is going on.
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Your Location Has Changed"
message:#"Your location has changed since you last opened opened THEAPP. Would you like to refresh your data?" delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.tag = 1;
[alert show];
[alert autorelease];
Then for the clickedButtonAtIndex method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//--NSLog(#"You clicked button #%i",buttonIndex);
if (alertView.tag == 1){
if (buttonIndex == 0) {
//--NSLog(#"CANCEL");
} else if (buttonIndex == 1) {
//--NSLog(#"OK");
}
}
}
Doing all of this on a splash screen should be fine as long as you take into account the HIG's requirements for using the users location. Hope this helps!

I resolve this issue, the problem was that my Splash View was a modeled view and invoked by
[self.tabBarController presentModalViewController:controller animated:YES];
what I did that I shifted the data downloading to another view controller and there I can show alerts, and can handle that

Related

How to navigate from app delegate class in window based iPhone application. How to write pushViewcontroller method in app delegate class

In my window base application I need to navigate to informationview from my appdelegate when i click on alert view button.
alert view works with NSlog.
But i need to push to the other view for this purpose i used
[self.navigationController pushViewController:info animated:YES];
but it doesn't pushes. just nslog only prints
- (void)applicationDidBecomeActive:(UIApplication *)application
{
//To count the number of launches
NSInteger i = [[NSUserDefaults standardUserDefaults] integerForKey:#"numOfCalls"];
[[NSUserDefaults standardUserDefaults] setInteger:i+1 forKey:#"numOfCalls"];
NSLog(#"the number of active calls are %d",i%3);
if(i%3==0 && i!=0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"you might prefer MedChart+" message:#"Get it now for more options" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok",nil];
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"canceled");
}
else if (buttonIndex == 1)
{
NSLog(#"Pushed to the information view ");
InformationViewCotroller *info = [[InformationViewCotroller alloc]initWithNibName:#"InformationViewCotroller" bundle:nil];
[self.navigationController pushViewController:info animated:YES];
}
}
(dont consider 'i' values it is part of my logic).
Thanks in advance
Before Navigate to any viewController , set the RootController for your navigationController of appDelegate.
Add navigationController.View as subview of window.Then your root controller will be the first ViewController.from there you can push to any viewController.

iPhone refresh AppDelegate

Ok I have an application that uses tab controller. However before I want the tab controller to load up, I want the user to enter a screen which allows them to enter their phone number first as I need to save the users phone number for use in the application. I save the number to the application like this.
[[NSUserDefaults standardUserDefaults] setInteger:1234 forKey:#"PhoneNumber"];
Basically when they first enter application, a window will open which will allow them to store enter their number in text field and it saved using the NSuserDefaults.
In the appDelegate, I have an if statement to check if a value is stored or not like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSInteger phoneNumber=[[NSUserDefaults standardUserDefaults] integerForKey:#"PhoneNumber"];
if(phoneNumber == 0){
//So this is the window which allow them to enter their phone number
UIViewController *phoneNumberVC = [[PhoneNumberVC alloc] initWithNibName:#"PhoneNumber" bundle:nil];
UIViewController *phoneNumberRootVC = [[UINavigationController alloc] initWithRootViewController:phoneNumberVC];
self.window.rootViewController = phoneNumberRootVC;
[self.window makeKeyAndVisible];
}else{
//Code here sets up tab controller and all the tabs etc
}
}
Now the code works fine, except for the fact that after the number is saved, I have to exit the simulator, and run it again, and then the tab screens appear. However, I want the tab controller to load up a soon as the user has pressed the button in the phone number screen to save the number. Current method in the phone number screen is this:
-(IBAction)testing{
NSString *st = [phoneTF text];
NSInteger pn = [st intValue];
NSString *display = [NSString stringWithFormat:#"%i", pn];
[[NSUserDefaults standardUserDefaults] setInteger:pn forKey:#"PhoneNumber"];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Warning"
message:display
delegate:self
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
}
The alert is just for testing purposes. So after the button is pressed, I want the tab controller to load up in the app delegate. Is there anyway I can refresh the application or the app delegate?
Would be very grateful if someone could point me in the right direction!!!
EDIT
Tried
[self.navigationController pushViewController:app.tabBarController animated:YES];
But as the tabBarController is not initialised yet, it won't run
Console error
Application tried to push a nil view controller on target <UINavigationController: 0x8056d40>.
So I need to run the AppDelegate again, so the tab controller and all its views will be initialised
replace
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:display
delegate:self cancelButtonTitle:#"Okay" otherButtonTitles: nil];
[alert show];
with code that asks the navigation controller to switch to the other view you want:
YourOtherViewController *yourOtherViewController = [[YourOtherViewController alloc] init];
[self.navigationController pushViewController:yourOtherViewController animated:YES];
Edit
If you want to access the AppDelegate from phoneNumberVC, you can do something like the following:
if(phoneNumber == 0){
UIViewController *phoneNumberVC = [[PhoneNumberVC alloc] initWithNibName:#"PhoneNumber" bundle:nil];
phoneNumberVC.appDelegate = self; // add this property to PhoneNumberVC
UIViewController *phoneNumberRootVC = [[UINavigationController alloc] initWithRootViewController:phoneNumberVC];
self.window.rootViewController = phoneNumberRootVC;
} else {
[self setUpTabViewController]; // add this function
}
[self.window makeKeyAndVisible];
And in PhoneNumberVC something like:
[self.appDelegate setUpTabViewController];

Why does my camera interface act weird when I use UIImagePickerController?

In my app I want the user to be able to take a picture or use one from the photo library. When the user clicks the button I made a alert view pops up at the user can choose between taking a new photo or one from the photo library. Here is the code I've used:
- (void)PictureAlert:(id)sender {
UIAlertView *AlertDialog;
// Setting up AlertDialog.
AlertDialog = [[UIAlertView alloc] initWithTitle:nil
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Choose From Library", #"Take New Picture", nil];
[AlertDialog show]; }
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *ButtonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([ButtonTitle isEqualToString:#"Choose From Library"]) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
// Pick photo.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
} else if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
// Setting up AlertDialog.
UIAlertView *AlertDialog;
AlertDialog = [[UIAlertView alloc] initWithTitle:#"Error accessing photo library"
message:#"Device does not support a photo library"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[AlertDialog show];
}
} else if ([ButtonTitle isEqualToString:#"Take New Picture"]) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// Take new photo.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.wantsFullScreenLayout = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
} else if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// Setting up AlertDialog.
UIAlertView *AlertDialog;
AlertDialog = [[UIAlertView alloc] initWithTitle:#"Error accessing camera"
message:#"Device does not support a camera"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[AlertDialog show];
}
}
}
The problem is that if the user wants to take a new picture the camera interface pops up, and then if you rotate the device the interface looks like this:
And then when the user rotate it back it suddenly looks like this:
A little side problem is that the camera takes a long time to load.
Any thoughts would be appreciated :)
A few things you might want to consider:
Setting the wantsFullScreenLayout property to YES will cause the view to ignore the status bar. But since you are using the default camera controls, the status bar hides automatically. This is the most likely cause for the 20 pixel grey area on the bottom of the image.
The default camera controls are designed to be in portrait mode only. Since your first image looks like you somehow rotated the screen, you should look into your code (probably shouldAutoRotate) and see why you are rotating the view like that. This should fix the problem of the zoom you are getting in your landscape picture.
You will have memory leaks if you create a UIImagePickerController, present it, and then have no reference to it to release it later. I would recommend setting the UIImagePickerController in the interface, and setting it up in the viewDidLoad method. Try:
.h
#interface yourView:UIViewController <UIImagePickerControllerDelegate> {
UIImagePickerController * picker;
}
.m
- (void)dealloc; {
[picker release];
[super dealloc];
}
- (void)viewDidLoad; {
[super viewDidLoad];
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; {
NSString *ButtonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if([ButtonTitle isEqualToString:#"Choose From Library"]){
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}
else{
// Setting up AlertDialog.
UIAlertView *AlertDialog;
AlertDialog = [[UIAlertView alloc] initWithTitle:#"Error accessing camera"
message:#"Device does not support a camera"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[AlertDialog show];
[AlertDialog release];
}
}
else if([ButtonTitle isEqualToString:#"Take New Picture"]){
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
else{
// Setting up AlertDialog.
UIAlertView *AlertDialog;
AlertDialog = [[UIAlertView alloc] initWithTitle:#"Error accessing camera"
message:#"Device does not support a camera"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[AlertDialog show];
[AlertDialog release];
}
}
}
This should clean-up the memory leaks, and improve the load time. Hope that Helps!
Some time its happened if you are using a old generation iphone that have current os for example you are having iphone 3G and you update its ios to ios5 then some of the app you installed can behave differently you can check your app to another device for rectify your problem.
Make sure you set the view controller hierarchy, with mainWindow.rootViewController and [vc addChildViewController:]. This propagates orientation information down to where you need it.
It looks like for my project this was happening because you have not written the shouldAutoRotateToInterface: method in your root view controller. The rotate message propagates all the way down to the root view controller's shouldAutoRotateToInterface delegate when UIImagePickerController is called. Your method should look like this:
- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
When I upgraded my project to iOS 5, I borrowed my root view controller from an iOS 3 project. iOS 3 did not automatically write this method in view controller classes Try it out and let me know.

load a different nib if phone is not connected to internet

I use Apple's Reachability class and it's working fine using an alert to tell the user that the connection is not available or the connection is lost. However, I want to change the alert to something more visual. I want to load a nib that tells the user no active connection is present but the nib is not loading. I also tried loading my other nibs but it also doesn't load the nib.
- (BOOL) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(#"The internet is down.");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"No Internet Connection" message:#"You are currently not connected to a WI-FI or cellular Data.\nPlease make sure you are connected." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
//NoConnection *noConn = [[NoConnection alloc] initWithNibName:#"NoConnecton" bundle:nil];
//[self presentModalViewController:noConn animated:NO];
//[NoConnection release];
self.isConnected = NO;
return NO;
break;
}
//more cases.........
the alert part is working just fine but the part for loading the nib is not. can you tell me whats wrong here? I'm calling this function in viewWillAppear. Thanks!
You can do the following:
if ( ! isConnected )
{
NoConnection *noConn = [[NoConnection alloc] initWithNibName:#"NoConnecton" bundle:nil];
[self presentModalViewController:noConn animated:NO];
[NoConnection release];
}
The code you have presented should work, sow the problem must be somewhere else probably in the nib - linking, you might have forgot to link something to the nib file.
try this
[self.navigationController presentModalViewController:noConn animated:YES];
Does your nib has NoConnection as a File's Owner (I guess NoConnection is a subclass of UIViewController, check it. I'll call this NoConnectionViewController bellow because you should name it like that for no mistake) ?
Is the file's owner view property linked with the graphical view ? Check it.
Are you working without status bar at top of the window ? That could be a problem.
Are your here inside a modalViewController ? If yes, your code won't work, you must use instead :
NoConnectionViewController* nextWindow = [[NoConnectionViewController alloc] initWithNibName:#"NoConnecton" bundle:nil]; // Check your nib name here, seems to be a mistake
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:nextWindow];
[self presentModalViewController:navController animated:YES];
[navController release];
[nextWindow release];
You need to use the delegate method of alert view
#pragma mark - AlertView Delegates
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 1)
{
NoConnection *noConn = [[NoConnection alloc] initWithNibName:#"NoConnecton" bundle:nil];
[self presentModalViewController:noConn animated:NO];
[NoConnection release];
}
}
don't forget to assign tag value of alertView to 1.
and also dont forget to conforms to the UIAlertViewDelegate protocol
Happy Coding :)

UIAlertView choice causing resignFirstResponder to fail

I'm having a similar issue to Anthony Chan's question, and after trying every suggested solution, I'm still stuck.
Somehow, only after interacting with my UIAlertView, I'm unable to dismiss the keyboard in another view of my app. It's as though the Alert is breaking my UITextField's ability to resignFirstResponder. Below I instantiate my UIAlertView, which then calls its didDismissWIthButtonIndex method. Then, I call the showInfo method, which loads another UIViewController.
UIAlertView *emailFailAlert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"error message text."
delegate:self
cancelButtonTitle:#"Not now"
otherButtonTitles:#"Settings", nil];
[emailFailAlert setTag:2];
[emailFailAlert show];
[emailFailAlert release];
Once the 'Settings' option is pressed, I'm calling this method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 2) {
if (buttonIndex == 1){
[self showInfo:nil];
}
}
}
My showInfo method loads the other ViewController, via the code below:
- (IBAction)showInfo:(id)sender {
FlipsideViewController *fscontroller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
fscontroller.delegate = self;
fscontroller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:fscontroller animated:YES];
[fscontroller release];
}
Upon clicking any textField in this Flipside VC, I'm unable to dismiss the keyboard as I normally can with - (BOOL)textFieldShouldReturn:(UITextField *)textField, and [textField resignFirstResponder]. I've omitted this code bc this question is getting long, but I'm happy to post if necessary.
The interesting part is that if I comment out the [self showInfo:nil] call made when the button is clicked and call it by clicking a test button (outside the alertView didDismissWithButtonIndex: method), everything works fine. Any idea what's happening here?
Thanks in advance!
When an alert, with more than one dismissal option, is called above a keyboard - the keyboard becomes un-dismissible with resignFirstResponder on the active textfield;
You will need to dismiss the keyboard before showing the alert.
Assuming your UITextField is called myTextField;
[myTextField resignFirstResponder]; //That's the only line I added
UIAlertView *emailFailAlert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"error message text."
delegate:self
cancelButtonTitle:#"Not now"
otherButtonTitles:#"Settings", nil];
[emailFailAlert setTag:2];
[emailFailAlert show];
[emailFailAlert release];
I hope this helps anyone who had to deal with this oddly obscure issue.
You should not call alertView:didDismissWithButtonIndex: directly. This delegate method will be executed automatically in all cases after the alert has disappeared. Otherwise the code will be run twice!