Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9014911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:37:32+00:00 2026-06-16T03:37:32+00:00

I am trying to learn how to get a button (in my case, a

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:37:33+00:00Added an answer on June 16, 2026 at 3:37 am

    You could solve it like this:

    - (void)addCancelButton{
    
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                                                         style:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
    
    
        self.navigationItem.leftBarButtonItem = cancelButton;
    
    }
    
    
    
    - (void)cancel:(id)sender{
        //If presented
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    
        //If pushed
        [self.navigationController popViewControllerAnimated:NO];
        }
    

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I copied this from the jquery site. I'm trying to learn how to get
I'm trying to learn GUI programming using python2 and GTKBuilder, but I get a
I've been trying to learn about metaclasses in Python. I get the main idea,
Hello I'm trying to learn ruby blocks. But I have a trouble to get
Trying to learn a bit about PDO and is going through this tutorial .
I'm trying to learn Prototype, so I threw together a simple Log-in page, with
I've been trying to learn delegates.I just created a button,label and checkbox. If I
I'm trying to learn jQuery's ajax functions. I've got it working, but jQuery doesn't
I'm trying to learn mouse/keyboard emulation using win api. I found out that it
I am trying to learn backbone.js through the following example. Then I got stuck

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.