Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I get this alert code when I press a button it opens a message:
- (void)patchButtonPressed
{
UIAlertView *patchAlert = [[UIAlertView alloc] initWithTitle:#"" message:#"After clicking `OK` you will be redirected to Cydia" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[patchAlert show];
[patchAlert release];
But I want that after clicking OK, it redirects to an URL (cydia://package/mypackage).
How can I do this?
set the delegate of the alertView and in the didDismiss method open the url
UIAlertView *patchAlert = [[UIAlertView alloc] initWithTitle:#"" message:#"After clicking `OK` you will be redirected to Cydia" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
pathAlert.delegate = self;
[patchAlert show];
[patchAlert release];
...
- alertView:(id)alert didDismissWithButton:(int)index {
[[UIpplication sharedApplication] openURL:[NSURL URLWithString:#"BLA"];
}
You don't actually need pathAlert.delegate = self. You've already set the delegate in the initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: method call.
In your .h file, you need to do this:
#interface YourViewControllerName : UIViewController <UIAlertViewDelegate>
And in the .m file add this method:
- alertView:(id)alert didDismissWithButton:(int)index {
[[UIpplication sharedApplication] openURL:[NSURL URLWithString:#"foo"];
}
Alternatively, and possibly better might be to give the user the option of "Ok" to be redirected or "Cancel" to not navigate to that page. In which case, you need to create the alert as such:
UIAlertView *patchAlert = [[UIAlertView alloc] initWithTitle:#""
message:#"After clicking `OK` you will be redirected to Cydia"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
Then, you can modify the handle method as such:
- alertView:(id)alert didDismissWithButton:(int)index {
if(index == 1) {
[[UIpplication sharedApplication] openURL:[NSURL URLWithString:#"foo"];
}
}
Now the URL is only opened if they click "OK", otherwise nothing happens.
Related
I want the user to not be able to click the cancel button on UIAlertView, yet I still want it to be there, but shaded out. I know a shouldEnableFirstButton function is there but it doesn't involve cancel button. Do you have any idea how to do it?
Thanks.
Use the following:
UIAlertView *vw = [[UIAlertView alloc] initWithTitle:#"Information" message:#"This is a message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel", nil];
[vw show];
So, essentially you are setting the "Cancel" as other button.
Now, the following will solve your problem:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return NO;
}
If you want to perform some action in OK button press you can always do that here:
- (void)alertViewCancel:(UIAlertView *)alertView
I am creating a drawing application where the user can draw using their finger strokes. I am trying to make a button that asks the user if they would like to clear the canvas. This alert has two buttons "Yes" and "No". I have the alert view appearing correctly but I have spent all day trying to figure out how to hook the buttons up to actions. I have so far had no success even after reading and watching from many instructional sources. From everything that I have read I can't understand why it would not be working. I have included UIAlertViewDelegate in my .h file also.
Here is my alert view:
- (IBAction)clearButton:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Clear Canvas"
message:#"Are you sure?"
delegate:nil
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
[alert show];
}
Here is my clear canvas method:
- (void)clearCanvas:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1)
drawImage.image = nil;
}
Any help or pointers would be greatly appreciated! I'm self taught and still very much a beginner!
Thanks!
- (IBAction)clearButton:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Clear Canvas"
message:#"Are you sure?"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
[alert show];
}
Notice the difference in the delegate parameter. You must conform to the delegate you have declared in the .h
Secondly, use the delegate method -alertView:clickedButtonAtIndex
I just started iOS development, and now I'm stuck. I have a table cell, where the user cann see data. Next to it, there are two buttons. One to delete, one to edit. The edit part is where I'm stuck. I have a label field. When the user clicks on the edit button a prompt should come where the user can type in data. I already found something to do it. But there's no save button and I don't know how I could then save the variable to an NSString
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Name der neuen Liste" message:#"" delegate:self cancelButtonTitle:#"Abbrechen" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
I currently have this. lNow a prompt pops up, where I can write something, but not save. I just can press cancel afterwards. How do I work with user input in iOS? I don't want to use the label as an UITextField since it has to be clickable to perform another action.
Try this:
- (void)noteInput
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"input"
message:#"input"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
UITextField *noteText = [alertView textFieldAtIndex:0];
NSString *note = noteText.text;
}
}
Try adding a Save button in otherButtonTitles. Then, in your UIAlertViewDelegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// Save button pressed
}
}
I have this code
- (IBAction)save:(id)sender {
self.imageView.image = [UIImage imageNamed:#"loading.gif"];
[self.view setUserInteractionEnabled:NO];
MBProgressHUD *hudSave = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hudSave.labelText = #"Saving...";
NSLog(#"save");
UIAlertView *deco;
if (!isVideo) {
UIImageWriteToSavedPhotosAlbum (imageView.image, nil, nil , nil);
deco = [[UIAlertView alloc]initWithTitle:#"Save" message:#"Your photo has been saved." delegate: nil cancelButtonTitle:#"oK" otherButtonTitles:nil];
}else{
//UIAlertView *explain;
//explain = [[UIAlertView alloc]initWithTitle:#"Wait during processing" message:#"Your video is being filtered, this process may be long, depending of both video and device. Please do not close this app until task is finished." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
UIAlertView *explain;
explain = [[UIAlertView alloc]initWithTitle:#"Wait during processing" message:#"Your video is being filtered, this process may be long, depending of both video and device. Please do not close this app until task is finished." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[explain show];
[self InitialisationRefiltrage:urlVideo];
//[self InitialisationRefiltrage:urlVideo];
deco = [[UIAlertView alloc]initWithTitle:#"Save" message:#"Your video has been saved." delegate: nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
}
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
[deco show];
[self.view setUserInteractionEnabled:YES];
}
The problem part is if my file is a video, it goes directly too "initialisationRefiltrage" (who is working fine) but without displaying the MBProgressHUD and the alert view explain, and after my video traitement, it display everything (explain, deco, and the MBProgressHUD) at the same time.
I try something with dispatch, thread, etc... But i think a don't do it correctly, so can you please give me a clue too how to do that.
Have a nice day.
Put the code [self InitialisationRefiltrage:urlVideo] in the delegate method of your UIAlertView so that it is executed only when the alert has been displayed and user has tapped on a button of the alert.
You may also use instead some third-party UIAlertView subclasses that uses completion blocks to make your code only execute when the alert is dismissed. See my class that does this for example.
Besides, you should respect coding conventions and use a method name beginning with a lowercase letter to make your code more readable.
The UI is updated in the "run loop".
The calls you're making tell iOS to display some views (alert, MUD...) and it'll do that on the next run through the loop.
What you need to do is wait for the user to respond to the alert before continuing. You do this by setting yourself as the UIAlert's delegate, then responding to the event:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
...
}
There are also libraries available that allow you to pass a block to the alert view, thus simplifying the whole thing. (https://github.com/jivadevoe/UIAlertView-Blocks, for example)
P.S. I see that you're new to Stack Overflow - please tick my answer if you're happy that it has responded to your question...
So I'm completely drawing a blank on the name of the class that draws one of these modal prompts with multiple options. I've attached a screenshot. It's the prompt that's confirming whether or not the user wants to sign out.
That's a UIActionSheet, Read UIActionSheet Class Reference.
Example
In your header, you need to add the <UIActionSheetDelegate> protocol. Then in your #implementation this is how you would call this specific sheet:
UIActionSheet * sampleSheet = [[UIActionSheet alloc] initWithTitle:#"Are you sure you'd like to sign out from Path?" delegate:self
cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Sign Out" otherButtonTitles:nil];
[sampleSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[sampleSheet showInView:self.view];
And this is how you would handle the sheet:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch ( buttonIndex )
{
case 0:
NSLog(#"User Chose Cancel");
break;
case 1:
NSLog(#"User Chose to Sign out.");
break;
}
}
You want UIActionSheet Class Reference
See UIActionSheet, I think that's the class you're looking for.
UIActionSheet *popupQuery;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//give the user an option to choose an image from their library or take a new photo...
popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Choose From Library", nil];
popupQuery.tag = 0;
}
else {
//give the user an option to choose an image from their library or take a new photo...
popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Choose From Library", nil];
popupQuery.tag = 1;
}
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:[self.view window]];
[popupQuery release];