【问题标题】:Linking View Controllers through button通过按钮链接视图控制器
【发布时间】:2015-08-03 15:25:40
【问题描述】:

我是所有这一切的初学者...话虽如此,我在我的应用程序中遇到了一个问题,我已经停滞不前,不知道下一步该做什么或修复。因此,任何答案将不胜感激!

所以在我的主视图控制器中,我有四个不同类别的按钮。 这些类别中的每一个都有自己的问题列表,但它们有一个共同的“一般问题”列表。一般问题列表有自己的视图控制器。 当您单击四个按钮中的任何一个时,它会将您带到一般问题视图。在这个视图的底部,我有一个“下一步”按钮。

目标:根据最初在主视图控制器中按下的内容,配置“下一步”按钮以继续到类别的问题列表之一。

我已经通过视图控制器中的插座和操作连接了按钮。 但是,当我控制 + 拖动到视图控制器时,下一步按钮将无法连接。我不确定我需要把代码放在哪里......

我在想“下一步”按钮的代码可能需要某种条件语句,但由于它无法连接,我什至无法做到这一点。

帮助!

(这就是我所拥有的)示例代码: 导入 UIKit 导入地址簿UI 导入通讯录 进口基金会 导入核心数据 导入核心图形 导入事件套件 导入 EventKitUI 导入核心基础

类视图控制器:UIViewController {

@IBOutlet var ColorButton: UIButton!

@IBOutlet var StyleButton: UIButton!

@IBOutlet var CutButton: UIButton!

@IBOutlet var MakeupButton: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

var eventstore: EKEventStore!
var event: EKEvent!
weak var editViewDelegate: EKEventEditViewDelegate!




@IBAction func ColorButtonPressed(sender: UIButton) {

}

@IBAction func StyleButtonPressed(sender: UIButton) {

}

@IBAction func HaircutButtonPressed(sender: UIButton) {

}

@IBAction func MakeupButtonPressed(sender: UIButton) {

}

}

【问题讨论】:

  • 为了最好地回答您的问题,您应该包含相关的示例代码和/或您的问题的屏幕截图。请查看提出好问题并进行修改的指南。 stackoverflow.com/help/how-to-ask
  • @Erik,我刚试过,但显然我还没有足够的积分来发布照片 - 但我刚刚添加了我的代码

标签: swift button xcode6 viewcontroller


【解决方案1】:

为简洁起见,这是一个建议的方法,如下面的代码所示,用于 2 个控制器(而不是 4 个)。对来自公共处理控制器的每个“下一个处理”控制器使用适当的命名segue,并建立一个链。这是项目文件的链接:Project file

        import UIKit



    class ViewController: UIViewController {

    var nextVcId = 0 // defines the button that is pressed



    @IBAction func unwindFromOtherControllers(segue: UIStoryboardSegue) {

    // In case you want to get back to the main VC

    }

    @IBAction func btn2Action(sender: UIButton) {

    nextVcId = 0

    self.performSegueWithIdentifier("commonSegue", sender: sender)

    }

    @IBAction func btn1Action(sender: UIButton) {

    nextVcId = 1

    self.performSegueWithIdentifier("commonSegue", sender: sender)

    }

    override func viewDidLoad() {

    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

    }



    override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }



    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {



    let  vc = segue.destinationViewController as! CommonViewController

    vc.nextControllerId = nextVcId



    }



    }

    import UIKit



    class CommonViewController: UIViewController {

    var nextControllerId = 0



    @IBOutlet weak var StatusLabel: UILabel!

    override func viewDidLoad() {

    super.viewDidLoad()

    self.StatusLabel.text = "Common"

    commonProcessing()

    // Do any additional setup after loading the view.

    }



    override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }



    func commonProcessing() {

    // do your common processing

    if nextControllerId == 0 {

    performSegueWithIdentifier("next1Segue", sender: self)



    } else {

    performSegueWithIdentifier("next2Segue", sender: self)

    }

    }



    }

    import UIKit



    class Next1ViewController: UIViewController {



    @IBOutlet weak var statusLabel: UILabel!

    override func viewDidLoad() {

    super.viewDidLoad()

    self.statusLabel.text = "Next1"

    next1Processing()



    // Do any additional setup after loading the view.

    }



    override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }



    func next1Processing() {

    println("Next 1 Processing")

    }





    }

    import UIKit



    class Next2ViewController: UIViewController {



    @IBOutlet weak var statusLabel: UILabel!

    override func viewDidLoad() {

    super.viewDidLoad()

    statusLabel.text = "Next 2"

    next2Processing()



    // Do any additional setup after loading the view.

    }



    override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }



    func next2Processing() {

    println("Next 2 Processing")

    }





    }

处理

【讨论】:

    猜你喜欢
    • 2013-05-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2012-06-18
    • 2014-11-14
    • 1970-01-01
    相关资源
    最近更新 更多