How to add a confirming pop up window in objective? - iphone

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.

Related

How to show an AlertView with another AlertView

I want to show nested alertViews. Problem, which i am facing in nested alertViews is that when i click an "add" button of first alertView it shows the second alertView, in second alertView i have a textField and a "Save" button. I want to save data when i click on save button and then reload UITableViewData, which is already in the first alertView.
I am new in iphone, so please help me.
You should create your alert views with different tag property so that in delegate method you can easily differentiate which alert view is appeared on the screen.
For example :
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Info"
message:#"Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[alert setTag: 1001]; // give different tag to different alert views
[alert show];
[alert release];
Now in delegate method :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1001)
{
// do something
}
eles if (alertView.tag == 1002)
{
}
}
Hope it helps you..

about UIAlert jumps to one xib

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.

My UIActionSheet acting like non-modal?

NSLog(#"Display Action Sheet");
UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle:#"Warning" delegate:self cancelButtonTitle:#"Proceed" destructiveButtonTitle:#"Cancel (current data will be discard)" otherButtonTitles:nil];
[alert showInView:[self view]];
[alert release];
NSLog(#"Action Sheet Released");
This is my code that creates an action sheet. Before I see the action sheet, both "Display Action Sheet" and "Action Sheet Released" get output to the debugger console. Actually other codes that I want to execute AFTER I receive input from user are all executed before I am presented the action sheet.
This is rather weird. I thought I could use action sheet to execute codes based on user's input.
Action sheets aren't modal. Pretty much nothing in iOS is. You need to handle whatever the user chooses in the sheet in one of the UIActionSheetDelegate methods, like -actionSheet:clickedButtonAtIndex:.
Make sure you have the UIActionSheetDelegate in your header.
If I want the answer to a question without the user allowing the option of cancel, I do something like this (key is : "if (buttonIndex == -1) performSelector")... Basically just loop till happy...
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (debug==1) {
NSLog(#"Running %# '%#'", self.class, NSStringFromSelector(_cmd));
}
if (buttonIndex == -1) {
[self performSelector:#selector(selectDb) withObject:nil afterDelay:0.1];
} else {
[DataLayer selectDatabase: buttonIndex];
}
}
- (void) selectDb {
if (![DataLayer hasSelectedDatabase]) {
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Filter" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"Main", #"Training", #"Test 1", #"Test 2", #"Test 3", #"Test 4", nil];
[actionSheet showInView:[self view]];
}
}

iPhone, I want to create a popup menu from my tab bar, but what are they call, like copy / paste?

I'd like to create a popup menu, from my tab bar.
I've seen them, but I'm not sure what they called ?
Kind of like a speak bubble.
I think copy / paste is the sort of thing I mean.
You are searching for the UIMenuController.
This question might help you.
That could be the UIActionSheet. You might want to give it a try.
//Your delegate for pressed buttons.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Your Action for pressed button.
[actionSheet release];
}
//Declare a method to show your sheet
-(void)showActionSheet
{
UIActionSheet *menu = [[UIActionSheet alloc]
initWithTitle: #"Action Sheet Title"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Delete"
otherButtonTitles:#"Other Button1", #"Other Button2", nil];
[menu showInView:self.view];
}
But you said "bubble", so that could also be the UIAlertView.
Try this:
-(void)showAlertView
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert!"
message:#"This is the message that pops up in the bubble."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
On the iPad, you'd want UIPopoverController, but I think that on the iPhone the same sort of interactions are meant to be performed by a separate, full screen controller or by a UIActionSheet. There's no speech bubble on the iPhone such that I'm aware.

UIAlertView and detemining what is clicked

I have code that when a user hits the end of the game, it prompts them if the would like to play again:
-(void)showAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#" B U S T E D ! "
message:#"Sorry, you busted!\n\nWant to try your luck 1 More Time! ?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"New Game", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
//here is where we can close it
}
if (buttonIndex == 1)
{
[self createNewGame];
}
}
Now I want to also do a check when a user first starts the app to see if a prior game file exists and if so ask if they want to continue. I know I can do via:
-(void)priorGameExists
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#" Previous Game Exists ! "
message:#"A previous game currently exists. Would you like to resume that game?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Resumse", nil];
[alert show];
[alert release];
}
But how do I have it go to a new "custom" clickedButtonAtIndex? Am I correct in assuming it has something to do with setting a different delegate? And if so, how would I do that?
You don't necessarily need a different delegate. Read my answer to this question:
iPhone - Several UIAlertViews for a delegate
One solution is to declare some UIAlertView as private class instance like that:
#interface myViewControllerInterface : UIViewController {
#private
UIAlertView *newGameAlert;
UIAlertView *resumeGameAlert;
}
Then in your view controller you can create your alertViews using them:
-(void)showAlert {
newGameAlert= [[UIAlertView alloc] initWithTitle:#" B U S T E D ! "
message:#"Sorry, you busted!\n\nWant to try your luck 1 More Time! ?"
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"New Game", nil];
[newGameAlert show];
[newGameAlert autorelease];
}
-(void)priorGameExists {
resumeGameAlert = [[UIAlertView alloc] initWithTitle:#" Previous Game Exists ! "
message:#"A previous game currently exists. Would you like to resume that game?"
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Resumse", nil];
[resumeGameAlert show];
[resumeGameAlert autorelease];
}
And to finish you can make the difference between each alert view using their pointer:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (actionSheet == newGameAlert ) {
//do something
} else if (actionSheet == resumeGameAlert ) {
//do something
}
}
You could use a different delegate but an easier way would be to set the tag property to a unique value. If tag was, say, 10 you'd know it was from the original alert and if it was 20 it would be from the priorGameExits question. (You should probably use constants of course.)
in your clickedButtonAtIndex method test the title of the incoming alertview.
if ([actionSheet.title isEqualToString:#" B U S T E D ! "]) {
// do some busted stuff here
else if ([actionSheet.title isEqualToString:#" Previous Game Exists ! "]) {
// do some previous game stuff here
}
You'll probably want to set those titles using static strings, so you only have the string in one place in your code, but this is basically how you'd do it.