Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

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

I currently have a login View and an Application view, I have successfully implemented validation on the login view and I need to programmatically shift to the application view on successful validation.

I understand I can add a segue to the login button and then call it programmatically like so...

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

But this will obviously be triggered whenever the button is clicked (As the segue was created attached to the button). I've just read that I should create a button (And hide it) and then make a programmatic call to the segue - this seems a bit 'wrong'.

How can a create a segue that isn't attached to any particular UI event?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...