gpt4 book ai didi

ios - 从另一个 UIViewController 推送 ViewController - Swift

转载 作者:行者123 更新时间:2023-11-30 13:41:06 24 4
gpt4 key购买 nike

我有一堂这样的课,

import UIkit

class One {

let btn = UIButton()

override func viewDidLoad(){
super.viewDidLoad()

btn.frame = CGRectMake(10, 20, 30, 30)
btn.setTitle("Go", forState: UIControlState.Normal)
btn.addTarget(self, action: "goToClassTwo", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
}

func goToClassTwo(){
if(AppGlobals().getIsFromDiffView()){
let difView = UINavigationController(rootViewController: DiffView())
difView.pushViewController(Two(), animated: true)
}else{
self.navigationController?.pushViewController(Two(), animated: true)
}
}
}

像这样的setter/getter类,

class AppGlobals: NSObject {

var isFromDiffView = false

func setIsFromDiffView(val: Bool){
isFromDiffView = val
}

func getIsFromDiffView() -> Bool {
return isFromDiffView
}

}

我还有一个这样的类(class),

class DiffView {

let btn = UIButton()

override func viewDidLoad(){
super.viewDidLoad()

btn.frame = CGRectMake(10, 20, 30, 30)
btn.setTitle("Push", forState: UIControlState.Normal)
btn.addTarget(self, action: "btnAction", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
}

func btnAction(){
AppGlobals().setIsFromDiffView(true)
One().goToClassTwo()
}
}

我在这里遇到了一个问题。当点击“One”类中的“Go”按钮时,将显示“Two” View Controller 。但是,当我点击“DiffView”类中的“推送”按钮时,不会显示“两个” View Controller 。

我已经检查了设置断点。控件确实到达“One”类中的 goToClassTwo 函数,并且 if 路径正在执行。但未显示“两个” View Controller 。 difView.pushViewController 被调用。但它并没有插入到下一个 View 。

注意:我没有使用 Storyboard

如有任何帮助,我们将不胜感激!

最佳答案

这是更新后的代码。

“One”类的代码:

import UIKit

class One {

let btn = UIButton()

override func viewDidLoad(){
super.viewDidLoad()

btn.frame = CGRectMake(10, 20, 30, 30)
btn.setTitle("Go", forState: UIControlState.Normal)
btn.addTarget(self, action: "goToClassTwo", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
}

func goToClassTwo(){
if(AppGlobals().getIsFromDiffView()){
//Using the navigation controller of DiffView
AppGlobals().getController().pushViewController(Two(), animated: true)
}else{
self.navigationController?.pushViewController(Two(), animated: true)
}
}
}

setter/getter 类:

class AppGlobals: NSObject {

var isFromDiffView = false
var cntrlr: UINavigationController!

func setIsFromDiffView(val: Bool){
isFromDiffView = val
}

func getIsFromDiffView() -> Bool {
return isFromDiffView
}

//Setting and getting DiffView Navigation controller
func setController(cntrl: UINavigationController){
cntrlr = cntrl
}

func getController() -> UINavigationController {
return cntrlr
}

}

DiffView 类:

class DiffView {

let btn = UIButton()

override func viewDidLoad(){
super.viewDidLoad()

btn.frame = CGRectMake(10, 20, 30, 30)
btn.setTitle("Push", forState: UIControlState.Normal)
btn.addTarget(self, action: "btnAction", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
}

func btnAction(){
AppGlobals().setIsFromDiffView(true)
//Setting the navigation controller
AppGlobals().setController(self.navigationController!)
One().goToClassTwo()
}
}

通过此更新的代码,将显示类“Two” View Controller 。感谢@zp_x 的帮助。

关于ios - 从另一个 UIViewController 推送 ViewController - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35552163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com