In Iphone I have to show an alert if the user clicks on home button immediately to indicate him that "The process will be stopped and he will loose the data".I have to tried to implement this using applicationWillResignActive and applicationDidEnterBackground.Can any one help me how can i achieve this?Is it appropriate to show such alert message on home button
Thanks,
Yogesh Chaudhari
Try this below code this will help you.
Its not really a way to quit the program, but a way to force people to quit.
UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:#"Hit Home Button to Exit" message:#"Tell em why they're quiting" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[anAlert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
[[NSThread mainThread] exit]
}
}
Related
like JOptionPane in java,
when user do something.
and i ask for confirmation like,
are you sure you want to delete file?? or whatever the case is.
user confirms it
then i detect what user has chosen between both buttons
then i do something according to user choice
is there anything like this in objective c ??
links plz and some guidelines
thank you every one
You are looking for UIAlertView controller.
I believe that you want UIActionSheet, which is what Apple Recommends for user choice:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Some Message" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"button To Destroy or nil" otherButtonTitles:#"Choice 1", #"Choice 2", #"Choice 3", nil];
What you want to do is check which button is being clicked under otherButtonTitles:
Something like this:
UIAlertView *yourAlert = [[UIAlertView alloc] initWithTitle:#"Your title" message:#"Some String" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Confirm",nil];
[yourAlert show];
And remember to set the delegate to self.
Then:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0){
//'Dismissing' code here
}
if (buttonIndex == 1){
//Detecting whatever was under otherButtonTitles:
//Right now it's checking if the user clicked "Confirm" -> So here you can put whatever you need
NSLog(#"You clicked Confirm");
}
}
Under the conditionals, you are checking which button the user is clicking.
i have an application in which i have implemented facebook in my app. In my app i have a page with a label and a button .when i click on the button the login page of facebook is opened.when i enter my username and password and click on the login button the login page dissapears. All this is working fine and but the problem is when i click again my button it should an alertview displaying that "this will logout from your previous account" and on the click of the ok button the session should get logged out displaying the login page.The problem is when i click the button it directly shows the login page without showing the alertview.Please help me in solving this problem.Thanks
Without seeing any of your code, i'm not too sure exactly how your logic is showing the alert, but if i was to show a confirmation alert, it would look something like this
- (IBAction)logoutButton_touchUpInside
{
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:#"Please Confirm"];
[alert setMessage:#"are you sure?"];
[alert setDelegate:self];
[alert addButtonWithTitle:#"Yes"];
[alert addButtonWithTitle:#"No"];
[alert show];
[alert release];
}
And implement the delegate method to catch the button click:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
// Yes, do something
}
else if (buttonIndex == 1)
{
// No, cancel etc...
}
}
I wish to know when the users are actually placing a call from my application (UIWebView) and not just tapping on the phone number.
How can I get the result of what the user selected when he receives the call/cancel popup after pressing a telephone link in my app?
There is some info regarding this on Opening tel: links from UIWebView.
How Things Work & What You Want
The OS is not going to handle showing the Call/Cancel alert dialog for you. That is up to you. It shows up in Safari because the Safari app's shouldStartLoadWithRequest method undoubtedly responds to the tel: scheme by showing a UIAlertView. Your conditional for if ([url.scheme isEqualToString:#"tel"]) should, when YES, trigger a UIAlertView with a Call and Cancel button. On Call, you will tell the sharedApplication to openURL; on Cancel, you will not issue the call & you will also want to return NO so your app does not attempt to loadWithRequest
Code Example:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
//OK clicked
} else {
}
}
- (void) _showAlert:(NSString*)title
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:#"Check your networking configuration." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
Hope this helped you in some way.
Best Regards
Linus
I have some three xib files, a b c
there is a button on b,and it will show an alert when click it,the code is
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Warning"
message:#"You will jump to a when you click 'Yes,Go',or click 'No,I am not' for exit"
delegate:nil
cancelButtonTitle:#"Yes,Go"
cancelButtonTitle:#"No,I am not"
otherButtonTitles:nil];
[alert show];
[alert release];
as its description, I want to jump to the a when I click the 'Yes,Go',this app will be closed if I click 'No,I am not'
So what should do?
Thanks
you can handle the event in alert view delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
here check for button index and do what you want.
First of write your UIAlertView like below,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"You will jump to a when you click 'Yes,Go',or click 'No,I am not' for exit"
delegate:self
cancelButtonTitle:#"Yes,Go"
otherButtonTitles:#"No,I am not"
,nil];
[alert show];
[alert release];
then you can handle which button is tapped by user as below code:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex ==0)
{
//handle it for "Yes,Go"
}
else if (buttonIndex ==1)
{
//handle it for "No,I am not"
}
}
We are missing a lot of details here to answer this question I think. What is the structure of your program? How do you show the xib files etc.
the situation is like this:
if i don't have internet connection, i show UIAlertView to the user.
when the user press "try again", i call "do something".
the problem is the UIAlertView still show for seconds.after the user click "try again"
i tried like:
[self.view setNeedsDisplay];
but,it not works, cause even in the documents,if said the refresh will be
only when code control will return to the system, and that is not the case here.
thanking in advance.
///////////////////////////////////
if(somecondition)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"NO INTERNET CONNECTION"
delegate:self cancelButtonTitle:nil otherButtonTitles:#"Try Again", nil];
[alert show];
[alert release];
return;
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
[self DoSomthing];
}
I use this method to detect when the alert is dismissed, I added a line of code to hide it and spawn a new URLrequest, if that's what you need.
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
alert.hidden=YES;
[webnews loadRequest:[NSURLRequest requestWithURL:home]];
}
The request uses my UIWebView 'webnews' and the NSURL 'home', you will have to adapt these of course.
If you do some lengthy process in the callback from the alert, it will not hide until that callback returns.
So in other words, your doSomething method should not directly try to reconnect to the internet, but just have it spawn off the asynchronous call for doing this.
EDIT:
You may delay/detach execution of doSomething with this line of code:
[self performSelector:#selector(doSomething) withObject:nil afterDelay:0];