通过按钮链接视图控制器
Linking View Controllers through button
我是所有这方面的初学者...话虽如此,我在我的应用程序中遇到了一个问题,我已经停滞不前,不知道下一步该做什么或修复。所以任何答案将不胜感激!
所以在我的主视图控制器中,我有四个按钮,它们具有四个不同的类别。
这些类别中的每一个都有自己的问题列表,但它们有一个共同的 "General Question" 列表。一般问题列表有自己的视图控制器。
当您单击四个按钮中的任何一个时,它会将您带到“一般问题”视图。在此视图的底部,我有一个 "Next" 按钮。
目标:配置“下一步”按钮以根据 Home View Controller 中最初按下的内容继续到类别的问题列表之一。
我已经通过 View Controller 中的 outlet 和 action 连接了按钮。
但是,当我控制并拖动到视图控制器中时,下一步按钮将不会连接。我不确定我需要将代码放在哪里...
我在想“下一步”按钮的代码可能需要某种条件语句,但由于它不会连接,所以我什至无法做到这一点。
求助!
(这是我的)示例代码:
导入 UIKit
导入 AddressBookUI
导入地址簿
导入基金会
导入核心数据
导入核心图形
导入 EventKit
导入 EventKitUI
导入 CoreFoundation
class ViewController: 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) {
}
}
为简洁起见,这里有一个建议的方法,如以下代码所示,用于 2 个控制器(而不是 4 个)。使用适当的命名 segues 到公共处理控制器中的每个 "next processing" 控制器并设置链。这是项目文件的 link: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")
}
}
处理中
我是所有这方面的初学者...话虽如此,我在我的应用程序中遇到了一个问题,我已经停滞不前,不知道下一步该做什么或修复。所以任何答案将不胜感激!
所以在我的主视图控制器中,我有四个按钮,它们具有四个不同的类别。 这些类别中的每一个都有自己的问题列表,但它们有一个共同的 "General Question" 列表。一般问题列表有自己的视图控制器。 当您单击四个按钮中的任何一个时,它会将您带到“一般问题”视图。在此视图的底部,我有一个 "Next" 按钮。
目标:配置“下一步”按钮以根据 Home View Controller 中最初按下的内容继续到类别的问题列表之一。
我已经通过 View Controller 中的 outlet 和 action 连接了按钮。 但是,当我控制并拖动到视图控制器中时,下一步按钮将不会连接。我不确定我需要将代码放在哪里...
我在想“下一步”按钮的代码可能需要某种条件语句,但由于它不会连接,所以我什至无法做到这一点。
求助!
(这是我的)示例代码: 导入 UIKit 导入 AddressBookUI 导入地址簿 导入基金会 导入核心数据 导入核心图形 导入 EventKit 导入 EventKitUI 导入 CoreFoundation
class ViewController: 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) {
}
}
为简洁起见,这里有一个建议的方法,如以下代码所示,用于 2 个控制器(而不是 4 个)。使用适当的命名 segues 到公共处理控制器中的每个 "next processing" 控制器并设置链。这是项目文件的 link: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")
}
}
处理中