Question

I am trying to develop a demo apps for iPhone displaying a label title and button. When clicking the button, the alert message box is displayed.

When it comes to the implementation,it comes with the following exception. I have tried to make viewController and window to be global variables but to no avail.

Would you please tell me if there are alternatives or not to try to create the event handling for the UIButton

'-[UIViewController OnClick:]: unrecognized selector sent to instance 0x14d7e1b0'

The below is my code

ViewController.m

#import "ViewController.h"
#import "AppDelegate.h"

@interface ViewController ()

@end

@implementation ViewController


@synthesize titleLabel;
@synthesize clickButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

    [self OnClick:clickButton];
    // Dispose of any resources that can be recreated.
}

- (IBAction)OnClick:(UIButton *)sender
{
    UIAlertView *helloWorldAlert = [[UIAlertView alloc]
                                    initWithTitle:@"My First App" message:@"Hello, World!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    // Display the Hello World Message
    [helloWorldAlert show];
}


- (void)viewDidUnload {
    self.clickButton = nil;
}
@end

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    UILabel *titleLabel;
    UIButton *clickButton;

@public
    double frequency;
    double sampleRate;
    double theta;
}

@property (nonatomic , retain) IBOutlet UILabel *titleLabel;
@property (nonatomic , retain) IBOutlet UIButton *clickButton;

- (IBAction)OnClick:(UIButton *)clickButton;

@end

XIB.

Only having Object App Delegate UI Items and View

AppDelegate.h

#import <UIKit/UIKit.h>


@class ViewController;

@interface AppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    ViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ViewController *viewController;

@end

AppDelegate.m

@implementation AppDelegate


@synthesize window;
@synthesize viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    window.backgroundColor = [UIColor whiteColor];
    UIViewController *viewController = [[UIViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = viewController;


    [self.window makeKeyAndVisible];
    return YES;
}

// ... UIApplication Delegate methods
@end
Was it helpful?

Solution

The problem is in your didFinishLaunching. Where you allocate your view controller -

 UIViewController *viewController = [[UIViewController alloc] initWithNibName:@"ViewController" bundle:nil];

You are allocating the base UIViewController, which doesn't have your clickButton method - so you get the exception. What you want is -

ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

By omitting the UI you get an instance of your UIViewController subclass, which implements the clickButton method.

OTHER TIPS

You need to create that object of controller class in which you define the method of button in didFinishLaunching method of appDelegate not UIViewController.

I guess you did not associate the UIButton in xib. code

@interface ViewController : UIViewController
{
    IBOutlet UILabel *titleLabel;
    IBOutlet UIButton *clickButton;
}

And associate clickButton to your respective button in xib.

Did you link the action with your UIButton ?

if not, place this in viewDidLoad

[clickButton addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];

and this of code should not be there

[self OnClick:clickButton];


- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  CGRect frame = CGRectMake(100, 100, 100, 100);
  UIButton *button = [[UIButton alloc] initWithFrame:frame];
  [button setTitle:@"my button" forState:UIControlStateNormal];
  [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:button];

}

You need to implement the OnClick: method in your implementation file. Like so:

In ViewController.m, add the following:

- (IBAction)OnClick:(UIButton *)clickButton
{
    // logic here, what to do when button was clicked!
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top