0

I have a Segmented Control that I'm using to switch between three view controllers. My first tableview is embedded in a navigation controller where the segmented control is in the navigation controller. I also have two custom segues in the storyboard (with animations coming from the left and right). In my "main" controller class - the middle of the segmented control - I use this code

- (IBAction)indexChanged:(id)sender {
    if ([sender selectedSegmentIndex] == 0) {
        [self performSegueWithIdentifier:@"leftSegue" sender:self];
    }
    else if ([sender selectedSegmentIndex] == 1){
    }
    else {
        [self performSegueWithIdentifier:@"rightSegue" sender:self];
    }
}

This works great for those two controllers. When those view controllers are put on the screen, I can still move between the far left and right controllers, but going back to the main controllers is crashing under each attempt I've tried. I think it has to do with me not understanding either the self that I'm sending the performSegueWithIdentifier message or self that I'm passing to sender:.

Example: If in the else if ([sender selectedSegmentIndex] == 1){} I create a new segue, with a unique identifier coming from either of the two new controllers, the application crashes saying "Receiver has no segue with identifier" when it is spelled correctly. Who is the receiver in this example?

Edit: I've added my current storyboard pictures here. The top two are the custom segues, and the middle two are modal and push, specific for the requirements.

Edit2: Using methods like self.navigationController.viewControllers[0] finds the correct view, but it doesn't allow me to switch back to the initial controller- it redraws it an loses the navigation bar.

Jared
  • 3,651
  • 11
  • 39
  • 64

3 Answers3

1

In Xcode, add an "Exception Breakpoint" in the "Breakpoint Navigator" view. Launch your app, and try to reduce the crash. Xcode should now stops on the instruction causing the crash. So now when you're stopped, try to type po self in the Debug Area, to get which classes is the receiver, it might help you understand what's happening...

cdescours
  • 6,004
  • 3
  • 24
  • 30
1

self in objective-C is the equivalent to this in most other languages (java, php etc ..).

It means this current class.

In MainViewController Say if you do:

[self performSegueWithIdentifier:@"leftSegue" sender:self];

It is the same as

[MainViewController performSegueWithIdentifier:@"leftSegue" sender:self];
meda
  • 45,103
  • 14
  • 92
  • 122
  • Thanks. So using this method I don't think it'll be possible to present the middle view controller again, because I can't have a segue to 'self' - it needs to be presenting something else? – Jared Sep 10 '14 at 16:31
  • As in - I don't think there's a method segueToSelf or one that would perform similarly. – Jared Sep 10 '14 at 16:34
  • 1
    Can you elaborate on what you are trying to achieve? I dont understand why your viewController need to segue to itself – meda Sep 10 '14 at 16:34
  • My primary window is embedded in a navigation controller. In the navigation bar there is a segmented control flipping between three views. It can change between the first and third views fine, but the second view to be changed is the primary view displayed. How can I signal that I want to go back to the primary view? – Jared Sep 10 '14 at 16:41
  • I've added a picture of my storyboard in the question - maybe it'll make it clear if this is the way to do this or not. – Jared Sep 10 '14 at 16:48
  • 1
    ok I see what you mean, only use segue from the segmented controller, use `[self.navigationController popViewControllerAnimated:TRUE];` to pop back to the main controller – meda Sep 10 '14 at 16:50
  • This will show the proper view, but is there no way to do this with custom animation? – Jared Sep 10 '14 at 17:22
0

Your segue to the main view controller from the view controller you are currently on may not have been properly labeled as "rightSegue" (or whatever it's called).

On the right panel of your Xcode while the Storyboard segue is selected, check that the "Identifier" field is labeled correctly.

Koh
  • 1,570
  • 1
  • 8
  • 6
  • It's properly labeled. The error must be coming from the inside the IBAction- `self` is the view controller I want to show. Should I not segue and use a popover or some similar method? – Jared Sep 10 '14 at 16:06
  • What I understand is that you start your view on the middle segment (let's call it VC2). Then you move to either VC1 or VC3. When you move back to VC2 through the segmented control, that's where the error occurs? – Koh Sep 10 '14 at 17:11
  • Yes, exactly. I now know that the error is occuring because the segmented control action is taking place within VC2 - the main view of sorts. There's not an easy way to segue to "self". Maybe I'll put a clear view in between and use that to navigate. Ideally, I'd like to show VC2 for 0.5 seconds and immediately continue to VC1 if I press VC1 while in VC3. – Jared Sep 10 '14 at 17:21
  • Looks like @meda answered your original question. Regarding custom animation, you might want to look at the accepted answer in the following [link](http://stackoverflow.com/questions/2215672/how-to-change-the-push-and-pop-animations-in-a-navigation-based-app) for sample code on customizing the animation through animation blocks. – Koh Sep 10 '14 at 19:08