use prepareForSegue for button

24,689

Solution 1

There are really two ways to push a view controller onto the navigation controller.

  1. The old way, all in code. That is what you did. You can completely forget about segues at this point.

  2. The new way: establish a Segue in Storyboard (push segue from one view controller to a new view controller, which you also define in Storyboard), give it an Identifier name and then use this code to push the view controller.

.

[self performSegueWithIdentifier:myIdentifyer sender:self];

where self is the view controller. Now you can do the customizations in prepareForSegue:.

Solution 2

the sample code in this case is just telling you the type of thing you can do in prepareForSegue:sender: .

it is suggesting that the viewController to which you are attempting to segue would have a method setMyObjectHere: and it would be expecting you to pass that object.

as an example, your class might have a setTitle: method, and in the prepareForSegue:sender: code, you could pre-set the title based on information in the controller you are performing the segue from.

to get rid of the warning, you need an identifier, as in the picture below:

enter image description here

Share:
24,689
Admin
Author by

Admin

Updated on May 26, 2020

Comments

  • Admin
    Admin almost 4 years

    Hi I create button programmatically and I connect my button to another view, but I got segue problem that I should use prepareForSegue method for storyboard but I don't know how there ar esome sample to the internet but I will get error when I used that sample, would you please help me

    Thanks in advance!

    here is my code

    Creating Button

     UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
            button.backgroundColor=[UIColor colorWithRed: 201.0/255.0 green: 201.0/255.0 blue:201.0/255.0 alpha: 1.0];
            button.tag = currentTag;
            currentTag++;
            [button.layer setBorderColor: [[UIColor blackColor] CGColor]];
            [button.layer setBorderWidth: 1.0];
            [button setTitle:[NSString stringWithFormat:@"%d",currentTag] forState:UIControlStateNormal];
            button.frame = CGRectMake(80*x, 32*y, 80, 32); 
            [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [buttonView addSubview: button];
    

    Action for button

    -(void)buttonPressed:(UIButton *)button
    {
    NSLog(@"button %u -- frame: %@", button.tag, NSStringFromCGRect(button.frame));
    WeekView *crtObj=[[WeekView alloc]initWithNibName:@"WeekView" bundle:nil];
    [self.navigationController pushViewController:crtObj animated:YES];
    [crtObj release];
    
    
    }
    

    Prepare for segue

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if ([[segue identifier] isEqualToString:@"WeekView"]) {
    
            // Get reference to the destination view controller
           WeekView *vc = [segue destinationViewController];
    
            // Pass any objects to the view controller here, like...
            [vc setMyObjectHere:object];  //// I don't know what should I write instead of object
        }}
    

    error

    enter image description here

    Edit 2:**** I have a warning :

    Segues initiated directly from view controllers must have an identifier for use with -[UIViewController performSegueWithIdentifier:sender:]

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if ([[segue identifier] isEqualToString:@"WeekView"]) {
    
            // Get reference to the destination view controller
         //  WeekView *vc = [segue destinationViewController];
    
            // Pass any objects to the view controller here, like...
           // [vc setMyObjectHere:object];
        [segue.destinationViewController setTitle:@"WeekView"];
        }}
    
  • john.k.doe
    john.k.doe almost 12 years
    added a picture to show you where to add the id to get rid of the warning.