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

  • Home
  • SEARCH
  • 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 7039189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:46:01+00:00 2026-05-28T01:46:01+00:00

Just learning to add actions with code in my .m file and my first

  • 0

Just learning to add actions with code in my .m file and my first attempt throws an exception and crashes the app.

This is an authentication script that I hope will communicate with my web server. Here is the relevant code (this is all in the .m file, very simple .h file):

.h (header) file:

#import <UIKit/UIKit.h>

@interface Login_ViewController : UIViewController <UITextFieldDelegate>

@end

.m (implementation) file:

- (void)viewDidLoad {
    UILabel *emailLabel = [[UILabel alloc] initWithFrame:CGRectMake(31.0f, 75.0f, 256.0f, 20.0f)];
    emailLabel.text = @"Email Address";
    emailLabel.backgroundColor = [UIColor grayColor];
    emailLabel.textColor = [UIColor whiteColor];
    UITextField *emailField = [[UITextField alloc] initWithFrame:CGRectMake(31.0f, 100.0f, 256.0f, 32.0f)];
    emailField.delegate = self;
    emailField.placeholder = @"Enter email address";
    emailField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:emailLabel];
    [self.view addSubview:emailField];
    UILabel *passLabel = [[UILabel alloc] initWithFrame:CGRectMake(31.0f, 175.0f, 256.0f, 20.0f)];
    passLabel.text = @"Password";
    passLabel.backgroundColor = [UIColor grayColor];
    passLabel.textColor = [UIColor whiteColor];
    UITextField *passField = [[UITextField alloc] initWithFrame:CGRectMake(31.0f, 200.0f, 256.0f, 32.0f)];
    passField.delegate = self;
    passField.placeholder = @"Enter Password";
    passField.borderStyle = UITextBorderStyleRoundedRect;
    passField.secureTextEntry = YES;
    UIButton *loginButton = [[UIButton alloc] initWithFrame:CGRectMake(31.0f, 275.0f, 256.0f, 32.0f)];
    [loginButton setTitle:@"Login" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(authenticate) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:passLabel];
    [self.view addSubview:passField];
    [self.view addSubview:loginButton];
    [self.view setBackgroundColor: [UIColor grayColor]];
    UIAlertView *noAccount = [[UIAlertView alloc] initWithTitle:@"Welcome to T-of-U" message:@"Please enter your TofU credentials." delegate:self cancelButtonTitle:@"continue" otherButtonTitles: nil];
    [noAccount show];
    [super viewDidLoad];
}

also in the .m file (IBAction for the button):

-(IBAction)authenticate:(id)sender{
    // This will be the validation code
    // if we validate we will set the default Setting with accountID here.
    UIAlertView *auth1 = [[UIAlertView alloc] initWithTitle:@"You clicked a button" message:@"Oh, do you want to login do you?" delegate:self cancelButtonTitle:@"Sure.. Why not!" otherButtonTitles: nil];
    [auth1 show];
}

I am using a storyboard with a View that has a custom class of login_ViewController and it loads successfully based on a check for a local setting for account ID. This window is only pulled up if they have not logged in before, so I can authenticate and get that account ID to store locally. I am still experimenting with programmatically creating all these items as you can see in the viewDidLoad section.

Is this failing because I don’t have the button/IBAction reference in the .h file , or is it ok to use the addTarget method where its all in the .m file?

Crash info from the log:

2012-01-08 04:54:32.868 TofU-5[10452:10103] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Login_ViewController authenticate]: unrecognized selector sent to instance 0x6c94780'
  • 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-28T01:46:01+00:00Added an answer on May 28, 2026 at 1:46 am

    Your fix may be as easy as:

    [loginButton addTarget:self action:@selector(authenticate:) forControlEvents:UIControlEventTouchUpInside];
    

    (add a COLON after the authenticate name in your @selector). If a method takes any parameter, there will be a colon after it and for the run time environment to be able to send a message to it, that colon needs to be included in the selector of the action target.

    I hope this makes sense and helps you out!

    p.s. it wouldn’t hurt to add

    -(IBAction)authenticate:(id)sender
    

    to the .h file.

    IBAction here is an abbreviation for InterfaceBuilder-Action so this actually helps a lot more if you’re building the interface in a XIB file. Because you’re setting the target programatically, you could just use -(void) authenticate (no colon, because your method uses or needs no parameters), but don’t do this yet until you get more overall experience with Objective C coding and actions.

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

Sidebar

Related Questions

First of all, this is my code (just started learning java): Queue<String> qe =
This is just an experienting/learning example. I'm an extreme functional noob. F# code to
I'm just learning wcf and currently got this far. CS File: using System; using
I am just learning about app.config in respect of creating custom sections. I have
I am learning cakephp, and I was just doing a simple Todo list app
Ok, I'm just now learning how to add libraries to visual studio projects, but
I've just started learning how to use pygame yesterday. I was read this one
I have just started learning wordpress plugin development and got this error when I
I just started learning some ruby, and I want to do something like this:
So I just started learning haskell and I'm trying to use this if statement:

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.