How to execute button press action before prepareForSegue in iOS?

假装没事ソ 提交于 2019-12-20 04:15:17

问题


I am creating an iOS application in which I want to perform an assignment action in button press before running the method prepareForSegue.

I created all controls using Main Story board.

The order of execution for some buttons is button press action -> prepareForSegue

but for some buttons it is prepareForSegue-> button press action

How to change the order for second set of buttons?

Here is the code I am using:

import UIKit

class SummaryViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    var button_prsd = "none"

    @IBAction func people_insights(sender: AnyObject) {
        button_prsd = "people_insights"
    }

    @IBAction func industry_research(sender: AnyObject) {
        button_prsd = "industry_research"
    }

    @IBAction func holidays_events(sender: AnyObject) {
        button_prsd = "holidays_events"
    }

    @IBAction func monthly_spotlights(sender: AnyObject) {
        button_prsd = "monthly_spotlights"
    }

    @IBAction func blog(sender: AnyObject) {
        button_prsd = "blog"
    }


    @IBAction func about_us(sender: AnyObject) {
        button_prsd = "about_us"
        print("button press about us executed")
    }


    @IBAction func settings(sender: AnyObject) {
        button_prsd="settings"
        print("button press settings executed")

    }


    @IBAction func quiz(sender: AnyObject) {
        button_prsd="quiz"
        print("button press quiz executed")
    }

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.

        let next_view = segue.destinationViewController

        if(next_view is DetailViewController)
        {
            let det_view = next_view as! DetailViewController
            det_view.link = button_prsd
            print("segue executed")
        } else if(next_view is DetailTableViewController)
        {
            let det_view = next_view as! DetailTableViewController

            print("segue executed")
        }
    }
}

回答1:


I figured out the problem, when give then code in this way, the execution is just alphabetical order, all the other methods were getting executed before prepareForSegue because the methods come above in alphabetical order

When I renamed the quiz and settings methods as a_quiz and a_settings, it worked



来源:https://stackoverflow.com/questions/33466275/how-to-execute-button-press-action-before-prepareforsegue-in-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!