0

Possible Duplicate:
connect button to TableViewController in xcode

How can I connect my button to another view controller class programmatically thanks in advance , I need the code I 'm really beginner thanks, here is my button code: my vie controller class name is year.m

-(void) year:(id)sender{
    NSLog(@"Year button clicked");
 }

Edit:

here is code for my programmatically button

UIBarButtonItem *yearButton= [[UIBarButtonItem alloc] initWithTitle:@"Year" style:UIBarButtonItemStyleBordered    
 target:self action:@selector(year:)];
Community
  • 1
  • 1

3 Answers3

0

To add target/action pairs programatically (as opposed to through Interface Builder), look into this method on UIButton:

addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
Amy Worrall
  • 16,250
  • 3
  • 42
  • 65
  • I edit my question can you check it –  Jul 20 '12 at 12:41
  • That's a pretty broad question. Take a look at this Apple guide about [Navigation Controllers](http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html#//apple_ref/doc/uid/TP40011313-CH2). There's a section in there for creating them programmatically. Essentially you have to make sure that the first view controller (that contains your button) is already inside a navigation controller, then in your action method you create the new VC and push it onto the navigation controller's stack. – Amy Worrall Jul 20 '12 at 12:50
  • Well, nope -- that's true for UIConrols, but UIBarButtonItem is not an UIControl subclass. –  Jul 20 '12 at 13:01
  • Ah, when the question was posted it didn't originally mention a UIBarButtonItem. It also seems that it's more about how to push a new view controller (hence my linking to that guide) rather than how to set up target/action (what I originally thought it was about). – Amy Worrall Jul 20 '12 at 14:34
0

Create yourButton in viewDidLoad method as following way...

UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[yourButton setTitle:@"YearButton" forState:UIControlStateNormal];
yourButton.frame = CGRectMake(240, 40, 75, 30);     
[yourButton addTarget:self action:@selector(year:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:yourButton];

This is your method code....

-(void) year:(id)sender{
    NSLog(@"Year button clicked");
 }

I think it will be helpful to you.

Prasad G
  • 6,702
  • 7
  • 42
  • 65
  • thanks for reply but I create my button I have my button I don't know how should I connect my button to one viewcontroller class , I mean after NSLog(@"Year button clicked"); what should I writ that when I press my button it goes to another class –  Jul 20 '12 at 13:00
  • See this [link](http://stackoverflow.com/questions/10296573/connect-button-to-tableviewcontroller-in-xcode/10297146#10297146) for your answer – Prasad G Jul 20 '12 at 13:03
  • but it's Item bar button it's not worked that way –  Jul 20 '12 at 13:44
0

You can simply add the addTarget method (see here: Bind Action to UI Button). In your case it would be:

[yearButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

//the method that gets called
-(IBAction) buttonPressed: (id) sender {
  NSLog(@"Button clicked %@", sender);
  // do something here
}

But if you want to check wether the button was pressed in another view controller you have to look for NSNotifications (see here: Cocoa Notification Example). With these you can send messages from one to another class and react to the users input.

Community
  • 1
  • 1
palme
  • 2,499
  • 2
  • 21
  • 38
  • inside (IBAction) buttonPressed: (id) sender { NSLog(@"Button clicked %@", sender); // do something here } I should write I want to press my button then goes to test.m view that's it –  Jul 20 '12 at 13:02
  • so you want to make a transition from one view to the other i guess: `//push it to the navigationController [[self navigationController] pushViewController:detailedViewController animated:YES];` In this case `detailedViewController` is the name of your new view controller you want to switch to in your case it should be `year`. – palme Jul 20 '12 at 13:06