0

I have created a new Monotouch iOS storyboard project. I have added two screens, one with a button on. I have linked the button to the second screen with a push segue. This all works as expected. What I have now done is add an touch up inside event method to the button, where I am trying to add some logic to say if the segue should be performed or not.

    partial void LoginButton_click(UIButton sender)
    {
        Random r = new Random();
        if (r.Next(1,2000000) == 33432)
        {
            this.PerformSegue("Push", this);
        } else {
            //Do not perform segue
        }
    }

I have seen lots of reference online that explain how to use the PerformSegue method, but I can't find how to stop the segue in the storyboard if the logic is not correct. Should I not create a segue in the storyboard and then just do it programatically? This seems to remove a huge benefit of using storyboards though to see how everything fits together.

Joseph
  • 2,706
  • 6
  • 42
  • 80

2 Answers2

1

You can implement the UIViewController method shouldPerformSegue:withIdentifier:

This method is invoked when the segue is triggered and you can return a boolean to indicate whether the segue should be performed or not.

Using this approach you can keep the segue connected to the button and you don't need a touchUpInside action handler.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
0

I'm not familiar with monotouch or xamarin, but make sure the segue is hooked up via the view controller and not the button.

As far as I know, you can't stop a started segue. All you can do is catch events within the segue. The right thing to do is make sure you're either calling it or not calling it.

If the following code snippet performs the segue in every scenario, then the segue is tied to the button's touch event and fires every time the button is touched.

This question & answer addresses this exact same issue in Xcode/Objective-C.

Community
  • 1
  • 1
nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Ah, thats my issue - thanks! In what instances would you want to segue from the button directly? – Joseph Jun 11 '14 at 11:46
  • When the segue has no conditional logic. But for me, it's annoying to change later so I hook up all segues like this even if the button event just performs the segue unconditionally. – nhgrif Jun 11 '14 at 12:08