0

I have searched all of the internet, it seems that every post on this topic leaves out one vital piece of info that i need. no matter what people tell me to do, i cannot get rid of a "undeclared identifier" error with everything/anything regarding my method for my button's action. I have picked at the code so much that it is totally jacked up. Can anyone tell me what exactly i need to to do make a proper interactive button? Everything that goes in the .m file and .h file for viewcontroller?
please don't assume that I already have a certain vital piece of code, because I might not. I am new to objective-c.
Thank you!!!!!!!! ( it is for ipad by the way, and i am avoiding using the storyboard )thanks!! i am very frustrated at this point.

here is my code so far

    UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[add setTitle:@"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[objects addSubview:add];
- (void)aMethod:(UIButton*)button
{
    NSLog(@"Button  clicked.");
}

And the problem isn't that it runs and then crashes, the problem is that i can't run it to begin with as long as I have this error "Use of undeclared identifier 'aMethod'"

Andrew Savage
  • 103
  • 1
  • 7
  • Please post whatever code you have so that we can have a look. – Ken Toh Feb 19 '14 at 05:22
  • If you're very new in iOS, I suggest you to start with Paul Hegarty lections. https://itunes.apple.com/ru/itunes-u/ipad-iphone-application-development/id473757255?mt=10 – Valentin Shamardin Feb 19 '14 at 05:22
  • possible duplicate of [How do I create a basic UIButton programmatically?](http://stackoverflow.com/questions/1378765/how-do-i-create-a-basic-uibutton-programmatically) – Jignesh B Feb 19 '14 at 05:29

4 Answers4

5

Please try this code

  UIButton *submitButton=[[UIButton alloc]initWithFrame:CGRectMake(120, 300, 70, 50)];
[submitButton.layer setCornerRadius:10.0]; 
[submitButton setTitle:@"Submit" forState:UIControlStateNormal];
[submitButton addTarget:self action:@selector(submitAction:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:submitButton];
Albin Joseph
  • 1,133
  • 15
  • 27
5
 .what is object in this line     [object addSubview:add];
    try this

    UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [add addTarget:self action:@selector(aMethod) forControlEvents:UIControlEventTouchDown];
    [add setTitle:@"add new" forState:UIControlStateNormal];
    add.frame = CGRectMake(100, 100, 100, 100);
     [object addSubview:add];


    - (void)aMethod
    {
        NSLog(@"Button  clicked.");
    }
Sport
  • 8,570
  • 6
  • 46
  • 65
  • object is the name of a scrollview i am trying to add it to. I will try that code – Andrew Savage Feb 19 '14 at 05:49
  • again, i am getting the "undeclared identifier" error for "aMethod" – Andrew Savage Feb 19 '14 at 05:51
  • - (void)aMethod; pest this line in .h file and then .m file use this - (void)aMethod { NSLog(@"Button clicked."); } – Sport Feb 19 '14 at 05:55
  • (aMethod) remove : change this line – Sport Feb 19 '14 at 05:57
  • thanks for helping me, i am still getting the same error though. and to make sure were still on the same page this what i have in .h `- (void)aMethod;` no errors there... and here is what i have in .m ` UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [add addTarget:self action:@selector(aMethod) forControlEvents:UIControlEventTouchDown]; [add setTitle:@"add new" forState:UIControlStateNormal]; add.frame = CGRectMake(100, 100, 100, 100); [self.view addSubview:add]; - (void)aMethod { NSLog(@"Button clicked."); }` – Andrew Savage Feb 19 '14 at 06:02
  • if it makes any difference, I am also getting the same " undeclared identifier" as a warning instead of error on the "addtarget" line – Andrew Savage Feb 19 '14 at 06:04
  • (aMethod) : remove this – Sport Feb 19 '14 at 06:04
  • remove it from the "add target" line? or the "void" line? – Andrew Savage Feb 19 '14 at 06:05
  • - (void)aMethod { NSLog(@"Button clicked."); } have you used this it is working for me why not for you – Sport Feb 19 '14 at 06:06
  • that's what I have, it's not working. Could there be some other factor involved? could it be an issue of global/non global variables? – Andrew Savage Feb 19 '14 at 06:09
  • and also I just noticed that at the top of my .m file i have a warning for my `@implementation ViewController` line that sais "Method definition of aMethod not found" – Andrew Savage Feb 19 '14 at 06:10
  • but you have - (void)aMethod { NSLog(@"Button clicked."); } this in .m – Sport Feb 19 '14 at 06:12
  • hmm.. i dont know. I will look through and make sure nothing is being excluded on a global level. thanks for all the help. I will consider this answered because I now know a little more than i did before – Andrew Savage Feb 19 '14 at 06:16
2

try this.

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
Vinoth
  • 81
  • 5
  • thank you! now where/how to I add my custom piece of code that runs after pressing the button. I have gotten this far before, it is when i try to make the button do something, that is when everything crumbles. – Andrew Savage Feb 19 '14 at 05:32
0
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

Once you press the button it will look for buttonClicked: action event automatically

-(void) buttonClicked:(UIButton *)clicked{ //Your action goes here...}
Vinoth
  • 81
  • 5