开发者

XCode 4.2 + iOS 5 Storyboard : Can't distinguish between segmented control sections as segue initiators?

开发者 https://www.devze.com 2023-04-12 03:32 出处:网络
Using Xcode 4.2 Storyboard, I\'ve just created a segmented control in a view with two segments. I then ctrl-drag from each segment to a separate view to create two segues. It seems the developers forg

Using Xcode 4.2 Storyboard, I've just created a segmented control in a view with two segments. I then ctrl-drag from each segment to a separate view to create two segues. It seems the developers forgot to distinguish between the segments though, since onl开发者_运维问答y one segue can be created; attempting to create a second to the 'other' control segment cause the first segue to be replaced by the second. Does anyone have a Storyboard workaround for this, or must I write the code manually?

Thank you.


4.2 has been publicly release now.

The problem can be solved with Storyboard using a custom segue. In the custom segue the segmented control can be tested to determine which controller to call. I have tested the following custom segue:

#import "FlipTopPop.h"

@interface UIViewController (Extension)

@property (strong, nonatomic) IBOutlet UISegmentedControl *tabControl;

@end


@implementation FlipTopPop

- (void) perform {

UIViewController *src = (UIViewController *) self.sourceViewController;
switch (src.tabControl.selectedSegmentIndex) {
    case 0:
        // go to settings
        src.tabControl.selectedSegmentIndex = 1; //not yet implemented - for now don't segway - reset to index 1 for now
        break;
    case 1:
        // this controller is called with index 1 - do nothing - should not get here
        break;
    case 2: {
        [UIView transitionWithView:src.navigationController.view duration:0.5
                           options:UIViewAnimationOptionTransitionFlipFromTop
                        animations:^{
                            [src.navigationController popViewControllerAnimated:NO];
                        }
                        completion:NULL];
        }
            break;
        default:
            break;
    }
}

@end

Note that I have not yet implemented the transition in case 0 (segment 0) which will transition to another controller with code similar to what is implemented in case 2.


I solved this situation in the following way. I added the segmented control to my toolbar in storyboard. This must be done first by adding a bar button and then adding the segmented control to that. My segment control has 3 segments each of which takes you to a different view. The current view represents on of those leaving a requirement for two segues to the two other views. I then created two button out of the view to the right on the toolbar. I control clicked from those to the two destination controllers to create the two required segues. I also attached the "value changed" sent action to the IBAction in the code below. The rest is implemented in code as follows:

- (IBAction)segmentChanged:(id)sender {
    switch (self.segmentedControl.selectedSegmentIndex) {
        case 0:
            [self performSegueWithIdentifier: @"goToSettings" sender: self];
            break;
        case 1:
            // aready here - do nothing
            break;
        case 2:
            [self performSegueWithIdentifier: @"returnToNotes" 
                                      sender: self];
            break;
        default:
            break;
    }
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"goToSettings"]) {
        [[segue destinationViewController] setManagedObjectContext:self.managedObjectContext];
        // do nothing special
    }
    if ([[segue identifier] isEqualToString:@"returnToNotes"]) {
        // do nothing special
    }
}

It is also necessary to keep the segment index selected to show what view cone is currently in. This is done with the following statement in the viewWillAppear method:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.segmentedControl.selectedSegmentIndex = 1; // the index for the current view
}

Similar code is implemented in each of the three view controllers selected by the segmented control. This solution is not much different than doing it all in code, but has the advantage the the storyboard reflects the views and view transitions (segues).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号