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 246719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:11:46+00:00 2026-05-11T21:11:46+00:00

I am creating a view controller that will be used to enter text information.

  • 0

I am creating a view controller that will be used to enter text information.

The view itself consists of a label, text field and two buttons in the navigation bar: “cancel” and “OK”.

When user presses “cancel” I am just popping back to the root view controller.

But when he presses OK, I want first to call a function from the root view controller and only after that to pop back.

I tried to implement it in the following way:

The header:

@interface UserInputViewController : UIViewController {
    UILabel *textLabel;

    UITextField *textField;

    SEL OKButtonAction;
}

-(NSString*) getEnteredText;

-(UserInputViewController*) initWithTitle: (NSString*)title Text: (NSString*)text andOKButtonSelector: (SEL) OKButtonSelector;

@end

Implementation:

@implementation UserInputViewController

-(UserInputViewController*) initWithTitle: (NSString*)title Text: (NSString*)text andOKButtonSelector: (SEL) OKButtonSelector
{
    self = [self init];
    self.title = title;

    OKButtonAction = OKButtonSelector;

    textLabel = [ [UILabel alloc] initWithFrame: CGRectMake(20, 20, 280, 50)];
    [textLabel setText: text];
    [ [self view] addSubview: textLabel];

    textField = [ [UITextField alloc] initWithFrame: CGRectMake(20, 100, 280, 50)];
    [ [self view] addSubview: textField];

    return self;
}

-(NSString*) getEnteredText
{
    return [textField text];
}

-(void) popToRootViewController
{
    [ [self navigationController] popToRootViewControllerAnimated: YES ];
}

-(void) popToRootWithOKAction
{
    [ [self navigationController] popToRootViewControllerAnimated: YES ];
    [self performSelector: OKButtonAction];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    //Cancel button
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"cancel button", @"") style: UIBarButtonSystemItemCancel target: self action: @selector(popToRootViewController) ];


    [ [self navigationItem] setLeftBarButtonItem: cancelButton animated: NO];
    [cancelButton release];

    //OK button
    UIBarButtonItem *OKButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"ok button", @"") style: UIBarButtonSystemItemSave target: self action: @selector(popToRootWithOKAction) ];

    [ [self navigationItem] setRightBarButtonItem: OKButton animated: NO];
    [OKButton release];

}

And here is the root view controller methods:

-(void) OKButtonAction
{
    NSLog(@"text: %@", [newProfileDialog getEnteredText]);
    [newProfileDialog release];
}

-(void) add_clicked {
    newProfileDialog = [ [UserInputViewController alloc] initWithTitle: @"User name" Text: @"Please enter a new user name:" andOKButtonSelector: @selector(OKButtonAction)];

    [ [self navigationController] pushViewController: newProfileDialog animated: YES];
}

But when I compile it and press the OK button from the pushed view, I get an exception.

I am not yet familiar with selectors programming, so I’ve got hard time trying to figure out what am I doing wrong.

How can I achieve this goal?

Thanks.

  • 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-05-11T21:11:46+00:00Added an answer on May 11, 2026 at 9:11 pm

    What you’re trying to do is called “delegation” — the action of the OK button is delegated to another object, in this case the root controller. Try changing your view controller’s initializer to this:

    -(UserInputViewController*) initWithTitle:(NSString*)title Text:(NSString*)text delegate:(id)delegate;
    

    Store the delegate in an instance variable (no need to retain it) and in popToRootWithOKAction call:

    if([delegate respondsToSelector:@selector(OKButtonAction)])
        [delegate performSelector:@selector(OKButtonAction)];
    

    Delegate methods don’t even need to be declared in your root controller’s .h file. In UserInputViewController‘s header file, just add a second @interface:

    @interface NSObject (UserInputViewControllerDelegate)
        -(void)OKButtonAction;
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Note: Please see my other answer below as well. Seeing… May 11, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer A framework is almost certainly the way to go. One… May 11, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer Check out [[UIDevice currentDevice] systemVersion]. This will contain the version… May 11, 2026 at 11:47 pm

Related Questions

Let's say I have a Size class which has height and width properties (in
I am creating a website in CakePHP and I am kind of new on
I need to create a photo gallery service that is managed by users. I've
I am creating a directory of sorts with members and their profiles. I'm using

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.