I am trying to learn how to get a button (in my case, a ‘Cancel’ button) to direct me back to the previous page it was on. I’ve got a button on my previous page that directs me to this page, and i want this button to take me back to the previous page and discard anything i had written on the page. My button on my first page pushes the view to this page through a modal connection. I know it would make sense to just use the simple modal connection for the Cancel button to get back to my original page, but I was hoping for a more elegant way to do this. Creating a custom view is a good idea i’m sure but I don’t really know what to do there so.. haha but any suggestions would be greatly appreciated!
#import "AddEventViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface AddEventViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textField2;
@property (weak, nonatomic) IBOutlet UITextField *textField3;
@property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (weak, nonatomic) IBOutlet UINavigationBar *addEventTitleBar;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *cancelButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@property (weak, nonatomic) IBOutlet UITextView *myTextView;
- (IBAction)textFieldReturn:(id)sender;
@end
@implementation AddEventViewController
@synthesize textField1, textField2, textField3, myTextView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (IBAction)textFieldReturn:(id)sender;
{
[sender resignFirstResponder];
}
- (void)viewDidLoad
{ // Do any additional setup after loading the view.
[super viewDidLoad];
self.textField1.delegate = self;
textField1.delegate = self;
self.textField2.delegate = self;
textField2.delegate = self;
self.textField3.delegate = self;
textField3.delegate = self;
[myTextView.layer setCornerRadius:10.0f];
[myTextView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[myTextView.layer setBorderWidth:1.5f];
[myTextView.layer setShadowColor:[UIColor blackColor].CGColor];
[myTextView.layer setShadowOpacity:0.002f];
[myTextView.layer setShadowRadius:3.0f];
[myTextView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan: (NSSet *) touches withEvent: (UIEvent *)event
{
if (textField1)
{
if ([textField1 canResignFirstResponder]) [textField1 resignFirstResponder];
}
[super touchesBegan: touches withEvent: event];
if (textField2)
{
if ([textField2 canResignFirstResponder]) [textField2 resignFirstResponder];
}
[super touchesBegan: touches withEvent: event];
if (textField3)
{
if ([textField3 canResignFirstResponder]) [textField3 resignFirstResponder];
}
[super touchesBegan: touches withEvent: event];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == textField1)
{
[textField1 resignFirstResponder];
}
else if (textField == textField2)
{
[textField2 resignFirstResponder];
}
else if (textField == textField3)
{
[textField3 resignFirstResponder];
}
return YES;
}
-(void)addCancelButton
{
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"style:UIBarButtonSystemItemAction target:self action:@selector(cancel:)];
self.navigationItem.leftBarButtonItem = cancelButton;
}
-(void)cancel:(id)sender
{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:NO];
}
@end
Fixed the issue!
You could solve it like this:
Where the code in AddCancelButton can be moved to viewDidLoad or somewhere appropriate. And you might want to edit how the view is removed depending on how it was added (pushed, presented etc.)