2

I sending a String from a UIButton to my next UITtableviewcontroller, the string is the UIButton Title (Monday ... Sunday) the string is used by the NSPredicate to filter my table information by day.

@property (nonatomic, weak) NSString *dayOTW;

- (IBAction)selectDay:(UIButton *)sender {
UIButton *dayW = sender;
dayOTW = dayW.titleLabel.text;
NSLog(@"button press = %@", dayOTW);
}

2012-05-01 06:23:21.731 passingdata[99957:fb03] button press = Monday

And segue to my next screen

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"SendWeekDay"])
{
    // Get reference to the destination view controller
    ViewController *vc = [segue destinationViewController];

    // Pass any objects to the view controller here, like...
    vc.dayOTW = dayOTW;
   }
}

the first time I select the button no information is show in my table , if I go back and select the button again my table show with the correct information for that day .

what i am doing wrong ?

one more question related to the above . I have 7 UIButtons one for each day on the week, how can segue from all the UIButtons with one segue ?

Thanks

HernandoZ
  • 742
  • 2
  • 12
  • 24

1 Answers1

4

The segue is being called before your IBAction method, so the variable is not set the first time you click it.

If the segue is connected to your button, then it will be the sender in prepareForSegue, so you don't really need the action method. Just get the title in prepareForSegue:

UIButton *dayButton = (UIButton*)sender
vc.dayOTW = sender.titleLabel.text;

To connect the same segue to all buttons, you have to ctrl-drag from each button, but give all segues the same identifier. This looks messy on the storyboard, so an alternative is to create a single segue directly from the source view controller, and in the action method (which you would need in this case, and would be connected to all of your buttons) call performSegueWithIdentifier:sender::

-(IBAction)dayButtonPressed:(UIButton*)sender
{
    [self performSegueWithIdentifier:@"SendWeekDay" sender:sender];    
}

This would then pass the button to the performSegue method as the sender, so you can use the code outlined above.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • how can i check if the segue is been call first ? or do i need to initialized the string first? I'll try that later . – HernandoZ May 01 '12 at 08:38
  • You can put a log in each method if you like, but the point of my answer is that you don't need the string variable - the button is there to be used as the sender in prepareForSegue, so just get the text from it at that point. – jrturton May 01 '12 at 08:40
  • I getting this error now when i click on the Button . '2012-05-01 11:41:33.097 passingdata[23451:fb03] nested push animation can result in corrupted navigation bar 2012-05-01 11:41:33.455 passingdata[23451:fb03] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted. 2012-05-01 11:41:33.457 passingdata[23451:fb03] Unbalanced calls to begin/end appearance transitions for .' it is passing the information but creating a extra view I need to click back twice to get to my rootviewController, any Ideas. Thanks – HernandoZ May 01 '12 at 10:45
  • You don't still have a segue connected to the button, do you (so it is performing the segue twice)? Other than that, I'm not sure - you should ask a new question. – jrturton May 01 '12 at 11:00
  • +1 Sorry my bad .. it was Connected .. thanks a lot for your help – HernandoZ May 01 '12 at 11:09