I am just getting into Xcode programming, and I need code so that when I click a button, it is a confirmation to take me into another ViewController.
I have an X button on the page, and when clicked I need a UIAlertview to pop up saying 'cancel' and 'next', and when next is pressed, i want it to change to another ViewController. These cancel and next buttons need to be side by side.
First thing you need to do is make sure the view with a button is a delegate of UIAlertView.
#interface ViewController() <UIAlertViewDelegate>
Then you need to add a touch up inside action to the button in your view. When the button is pressed, it creates the alertView and shows it. Note, its delegate is self.
- (IBAction)clickButton:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Test" message: #"Message" delegate: self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Next", nil];
[alert show];
}
Lastly, you create the delegate function. This is triggered whenever a button is clicked in the alertView. buttonIndex corresponds to the button that was clicked, so we want to present the view when the 1st button is pressed (0 corresponds to cancel, 1 corresponds to next). Realistically, you'd probably want to present a custom view controller that you define.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){
UIViewController* newView = [[UIViewController alloc] init];
[self presentViewController:newView animated:YES completion:nil];
}
}
Related
I have a modally presented view controller which I want to unwind. I have it set up that if I press "cancel", it will execute an unwind segue. But, I want to make a confirmation button using UIAlertView. As in, "Are you sure you'd like to cancel?". How can I use the UIAlertView buttons to trigger an unwind segue?
In your .h file, declare UIAlertViewDelegate. And then in your .m file, you can write code in cancel button action method:
- (IBAction)confirmationButtonPressed:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"Are you sure you'd like to cancel?" delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No",nil];
[alert show];
}
And then you can add UIAlertView Delegate method in your .m file:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
[self dismissViewControllerAnimated:YES completion:nil];
}
}
Declare a manual unwind segue from the storyboards by selecting the whole view controller and ctrl dragging the mouse to exit.
Give that segue an identifier and fire the segue from delegate of the UIAlertView using performSegueWithIdentifier method
I am using XCode 5.1.1 for development purposes and using the iOS simulator to test my app.
I have an action sheet that has Save, Cancel and Delete options. I haven't yet coded the action handling for Save and Delete, but tapping on cancel should go back to the previous screen (I have used navigation controller to move between screens). But tapping on cancel, throws me "ViewController actionSheet:clickedButtonAtIndex:]: message sent to deallocated instance" error, which I am not able to solve. I have hooked up all the elements to their outlets/actions correctly. Below is my code. Please help. (I am trying to display the action sheet when a "return" button is clicked. And in the action sheet, when I tap cancel, the previous screen had to be displayed - I guess this can be done by dismissViewControllerAnimated which dismisses the current controller and displays the previous controller.)
-(IBAction)returnModalAction:(id)sender {
[self dismissViewControllerAnimated: YES completion: NULL];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"What do you want to do with the user values?"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:#"Delete"
otherButtonTitles:#"Save", nil];
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 2) {
NSLog(#"You have pressed the %# button", [actionSheet buttonTitleAtIndex:buttonIndex]);
}
}
On your method returnModalAction: you are dismissing the view, so the garbage collector will release all the references of self(the view controller) in memory that's why when you try to to show the action sheet [actionSheet showInView:self.view]; you get the error because the reference in memory doesn't exist.
So what you have to do is to perform [self dismissViewControllerAnimated: YES completion: NULL]; when you actually want to display the previous screen, in your case that would be on the actionSheet:clickedButtonAtIndex: method, based on the index of the button.
As my title said that clickedButtonAtIndex delegate method is call even tap on out side of UIActionSheet.
My actionSheet is display when I tapped on UIButton.
Code of my UIActionSheet declaration.
-(void)btnComplexityTapped:(UIButton *) sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select your complexity" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Easy", #"Medium", #"High", nil];
[actionSheet showFromRect:sender.frame inView:self.view animated:YES];
[actionSheet release];
}
And in delegate method
- (void)actionSheet:(UIActionSheet *)myActionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
[self.btnComplexity setTitle:#"Easy" forState:UIControlStateNormal];
}
else if(buttonIndex == 1)
{
[self.btnComplexity setTitle:#"Medium" forState:UIControlStateNormal];
}
else
{
[self.btnComplexity setTitle:#"High" forState:UIControlStateNormal];
}
}
I know that this issue is fix by set last condition in delegate method to set else if(buttonIndex == 1) instead of only else. But I does not want to apply any fixing but I want to know why delegate method is called even I am not tapping on any button of UIActionSheet
See this video for more clear picture.
As above video default title of button is "Medium" when I click on button then actionSheet is open and I click on out side of actionSheet (such like self.view) then delegate method clickedButtonAtIndex is called so last else condition is become true and thats way my button's title is changed "High".
Can any body tell my why this is happening ?
In iPad action sheet is presented in a popover. No cancel button because touching out of popover is equivalent to a touch on cancel. So, your delegate receives the message as if cancel button has been touch.
Because it is presented in POPOVER (iPad) which does not have cancel button on ActionSheet.Its Cancel is triggered if you tap out of ActionSheet.
I have a slide out menu in my app, which has a button that will display a UIActionSheet. This slide out menu just partly covers whatever view controller you previously were on.
- (void)cameraButtonPushed:(UIButton *)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Create String" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Choose from Library", nil];
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
//[actionSheet showInView:self.parentViewController.view];
}
As you can see I have tried two ways of showing the action sheet and they both appear to work the same.
The problem that I'm having has to do with the action sheet buttons actually working. It shows up in the parent view controller (view controller that you were on), but if I press any of the buttons they do not work.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
EditVC *newEditVC = [self.storyboard instantiateViewControllerWithIdentifier:#"editVC"];
[editVC setHidesBottomBarWhenPushed:YES];
[self.parentViewController.navigationController pushViewController:newEditVC animated:YES];
//[self.navigationController pushViewController:newEditVC animated:YES];
} else if (buttonIndex == 1) {
EditVC *newEditVC = [self.storyboard instantiateViewControllerWithIdentifier:#"editVC"];
[editVC setHidesBottomBarWhenPushed:YES];
[self.parentViewController.navigationController pushViewController:newEditVC animated:YES];
//[self.navigationController pushViewController:newEditVC animated:YES];
}
}
Both of the above methods are in the same menu view controller where the button is pressed. I put some break points to test it out and the action sheet delegate method is being called. The if statement is working correctly, but the new VC's are not being pushed onto the navigation controller the way they should. It just runs over those lines and nothing happens. I have also tried setting it up in a way that parent view controller's also have the actionSheet:clickedButtonAtIndex: method in them, but that method is not called.
I believe that it has to do with setting up the actionSheet with an improper delegate, but I don't know for sure.
How can I call this action sheet from one view controller, display it and have actions performed in another? I have it appearing in the correct VC, but the action's aren't working.
I need to show an UIAlertView before a user leaves a certain view, either by tapping a 'back' navigation bar button or by tapping one of the tab items in the tab bar I have, in order to ask him for confirmation. It would be a two-button alert, a 'Cancel' one to stay in the view, and an 'Accept' one to leave. I need to do this because I have to make the user aware that unsaved changes will be lost if leaving.
I tried to do this by creating and showing the alert view in the viewWillDisappear: method:
- (void)viewWillDisappear:(BOOL)animated
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Exit", #"")
message:NSLocalizedString(#"Are you sure you want to leave? Changes will be discarded", #"")
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", #"")
otherButtonTitles:NSLocalizedString(#"Accept", #""), nil];
[alertView show];
[super viewWillDisappear:animated];
}
But the view is pop anyway, and the alert view is shown after that and app crashes since its delegate is the view controller that has been already pop from the navigation stack... I don't find the way to solve this scenario, can anybody help me?
Thanks!
Showing the alert view when viewWillDissapear won't work, because the view is already dissapearing, it's on its way to be removed.
What you can do, is add yourself a custom action when the back button is pressed, then you decide what to do when the back button is pressed, you can show the alert view, and then in one of the buttons procedd to dismiss the view, something like this:
- (id)init {
if (self = [super init]) {
self.navigationItem.backBarButtonItem.target = self;
self.navigationItem.backBarButtonItem.action = #selector(backButtonPressed:);
}
return self;
}
Then show the alert view when the back button is pressed:
-(void)backButtonPressed:(id)sender
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Exit", #"") message:NSLocalizedString(#"Are you sure you want to leave? Changes will be discarded", #"") delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel", #"") otherButtonTitles:NSLocalizedString(#"Accept", #""), nil];
[alertView show];
}
Now, when the confirmation button in the alert view is pressed, just call:
[self.navigationController popViewControllerAnimated:YES];
Or do nothing if the user cancels
I would be tempted to move the data manipulation you're trying to protect into a modal view controller and handle the validation on whatever action you choose to have dismiss the modal presentation. To me, that's the point of modal: something that has to be completed before interacting with the rest of the app.