Ios7, Segue and Storyboards - How to Create Without a Button

IOS7, Segue and storyboards - How to create without a button?

Delete the current segue.

Attach the segue from the origin view controller to the destination (and then name it).

Now, your button press method should look something like this:

Objective-C:

- (IBAction)validateLogin:(id)sender {
// validate login
if (validLogin) {
[self performSegueWithIdentifier:@"mySegue" sender:sender];
}
}

Swift:

@IBAction func validateLogin(sender: UIButton) {
// validate login
if validLogin {
self.performSegueWithIdentifier("mySegue", sender:sender)
}
}

The key here is that the origin view controller should be the one you're dragging from to create the segue, not the button or any other UI element.

Sample Image

Personally, I hook ALL of my segues up this way, even the ones that should trigger on simple button pushes without any validation or logic behind them. It's easy enough to call it from the button's method.

And, I usually declare all of my segue names as constant strings somewhere in the project.

Perform segue without use of a button?

You can do it using performSegueWithIdentifier:sender: method:

if (condition) {
[self performSegueWithIdentifier:@"mySegue" sender:self];
}

The snippet above assumes the context of an instance method in a class that defines a segue with identifier "mySegue".

This segue should take place once the application loads for the firs time.

You do not need a segue for situations when the application loads. Instead, you need to set the initial view controller programmatically.

Storyboards - how to perform segues automatically without animation and showing the screen

Here is one way of doing it;

In your StoryBoard, assign an identifier to the second view controller (this is done on the identity inspector, setting the Storyboard ID field). In the code below, i have named mine secondVC;

Then in the viewDidLoad for your first controller (the one you want to skip but come back to) do something like this;

- (void)viewDidLoad{
[super viewDidLoad];
/// validate viewController one being displayed
if(dontDisplayFirstController){

UIStoryboard *storyBoard = self.storyboard;
UIViewController *targetViewController = [storyBoard instantiateViewControllerWithIdentifier:@"secondVC"];
UINavigationController *navController = self.navigationController;


if (navController) {
[navController pushViewController:targetViewController animated:NO];
}
}
}

This will efectivly push to the second viewController whilst still maintaining viewController one in the navigation tree.

Stop segue in storyboard when button logic fails

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.

Using Segue Manually

You'd have to trigger the segue manually. Hook up the button to a method, then make two segues, one from each view controller to the other in your storyboard, then give it an identifier in IB, then in your method you can call "performSegueWithIdentifier:".

Additional Info

To make a manual segue, control-click from the view controller object in IB to another view controller and the box will pop up as "Manual Segue". Just make sure it has an identifier.

Segue to a UINavigation Controller programmatically without storyboards

First of all segue can be used only with if you have a UINavigationController that will handle the navigation (push, pop, etc).

So if you have a UINavigationController and you want to push another UIViewController on the stack without using segue then you can use pushViewController:animated: method which also has a reverse popViewControllerAnimated:. Also UINavigationController provides other methods for adding/removing UIVIewControllers, for more info check UINavigationController class reference.

Checking if button is pressed to segue correct data

You need to add segue from view controller to viewcontroller refer this link : IOS7, Segue and storyboards - How to create without a button?

set tag property to each button i.e. 1 and 2

on button click pass button

   @IBAction func choiceOneButton(sender: AnyObject) 
{
character?.strength += 3
character?.intelligence -= 2
self.performSegueWithIdentifier("identifer", sender: sender)
}


@IBAction func choiceTwoButton(sender: AnyObject)
{
character?.agility += 2
character?.intelligence += 2
character?.courage -= 2
self.performSegueWithIdentifier("identifer", sender: sender)
}

In prepare segue method check

 override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) 
{
var btn:UIButton=sender as UIButton

if btn.tag==1
{
// First button clicked
}

if btn.tag==2
{
// Second button clicked
}

}


Related Topics



Leave a reply



Submit