1

How can I connect my button to another view controller class programmatically

here is code for my programmatically button

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


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

    //I don't know what should I write here to connect my button to UIViewController**
    //when I added this line my process is terminated** 

    [self performSegueWithIdentifier:@"YearView" sender:self]; 
 }

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString: @"YearView"]){
       [segue.destinationViewController setTitle:@"YearView"];
    }
}

Here you can see my view in storyboard: ![enter image description here][1]

Edit: *My process is Terminated when I used this method*

-(void)year:(id)sender{

// [self performSegueWithIdentifier:@"YearView" sender:self]; 
NSLog(@"Year button clicked");
YearView *yearVC = [[YearView alloc] initWithNibName:@"YearView" bundle:nil];

[[self navigationController] pushViewController:yearVC animated:YES];   // [yearVC release];

}
  • self.navigationItem.rightBarButtonItem = yearButton; – Rinju Jain Jul 23 '12 at 09:32
  • @RinjuJain where should I put it ? behind this -(void) year:(id)sender{ NSLog(@"Year button clicked"); ??? –  Jul 23 '12 at 09:33
  • if you want to add a button that you created programmatically to a `UIView` you can add with `addSubView`. but if the button is not showing on your `UIView` where it will show and what is the point to connect to another `ViewController`? – nfarshchi Jul 23 '12 at 09:34
  • @nfarshchi I have my button but my problem is I don't know how should I connect my UIBarButton to the UIViewController programetically –  Jul 23 '12 at 09:36
  • I think in this case you just need to push `yearView` to your `navigationController` – nfarshchi Jul 23 '12 at 09:50
  • Check this link:- http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers this will help you.. – Leena Jul 23 '12 at 09:52
  • 1
    @justin Just asking for code because you are new to Objective-C isn't a good way of using this site. – Abizern Jul 23 '12 at 10:16

3 Answers3

3

Try this one :

YearView *year = [[YearView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:year animated:YES];

It's good to read the tutorial first: Presenting View Controllers from Other View Controllers http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

Elnaz
  • 1,095
  • 1
  • 12
  • 30
  • @Terminator but with this code my buttons works and loaded the views –  Jul 23 '12 at 10:50
1
UIBarButtonItem *yearButton = [[UIBarButtonItem alloc] initWithTitle:@"Year"style:UIBarButtonItemStyleBordered target:self action:@selector(year:)];
self.navigationItem.rightBarButtonItem = yearButton;

-(void) year:(id)sender {
    NSLog(@"Year button clicked");
    YearViewcontroller *yearVC = [[YearViewcontroller alloc] initWithNibName:@"YearViewcontroller" bundle:nil];
    [self.navigationController  pushViewController:yearVC  animated:YES];
    [yearVC release];
}
Rinju Jain
  • 1,694
  • 1
  • 14
  • 23
  • jain but what should I write inside of -(void) year:(id)sender{ my problem is how connect my button to UIViewController –  Jul 23 '12 at 09:40
  • Jain yes everything works but after NSLOg what should I write to connect my year button to yearview UIVIewController class as you can see in picture??? –  Jul 23 '12 at 09:46
  • @justin you mean after you push year button you get navigated to `yearView`. it is different to connect to a `viewController` – nfarshchi Jul 23 '12 at 09:52
  • thansk but I got error since I'm using storyboard should I have initWithNibName:@"YearViewcontroller" ?? @RinjuJain –  Jul 23 '12 at 10:00
  • 1
    Going through the trouble of loading and pushing a view manually when there is already a storyboard is wasted effort. – Abizern Jul 23 '12 at 10:15
  • @Abizern yes but what is the problem here, I what to know it, how can I connect it manually –  Jul 23 '12 at 10:17
  • What is the segue identifier given in the storyboard that connects to the year view controller? Is that segue identifier the one connected to the year view controller or to the navigation controller of the year view controller? –  Jul 23 '12 at 10:21
  • @Terminator It's works thanks for reply the segue was year thanks –  Jul 23 '12 at 10:28
0

To link your button with the method in the viewController, you need to set an IBOutlet in the interface designer (this is the simplest way). If you still want that in another view controller, you could set a property of the another viewController for example in the viewDidLoad method, so that the other viewController also has a reference to the button, altough that is far from ideal. Only one viewController should manage the button.

Jorge Aguirre
  • 2,787
  • 3
  • 20
  • 27
  • Thanks for reply would you please show me with code I'm really new to iOS –  Jul 23 '12 at 09:31