gpt4 book ai didi

ios - 自定义 UIView 标签和动画未显示在 View Controller 中

转载 作者:行者123 更新时间:2023-11-30 13:00:38 25 4
gpt4 key购买 nike

我实际上有一个可以工作的自定义 uiview,并且我认为 uiview 中不工作的所有内容的设置都与工作的相同。这是代码:

protocol SessionDisplayViewDelegate: class
{
func homeButtonTapped()
}

class SessionDisplayView: UIView
{

@IBOutlet var view: UIView!


@IBOutlet weak var accountImage: UIButton!

@IBOutlet weak var mySKView: SKView!
@IBOutlet weak var sessionTitle: UILabel!

weak var delegate: SessionDisplayViewDelegate?
required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)!
commonInitialization()

}

override init(frame: CGRect) {
super.init(frame: frame)
commonInitialization()
}

func commonInitialization()
{
Bundle.main.loadNibNamed("SessionDisplayView", owner: self, options: nil)
self.addSubview(self.view)


//accountImage = UIImage(data: ShareData.sharedInstance.accounts[ShareData.sharedInstance.indexOfCurrentAccount].picture!, scale: 1.0)

}

func onView()
{
let curScene = MyScene(size: mySKView.bounds.size)
curScene.scaleMode = SKSceneScaleMode.aspectFill
mySKView.presentScene(curScene)
let myImage = UIImage(data: (ShareData.sharedInstance.accounts[ShareData.sharedInstance.indexOfCurrentAccount]?.picture!)! as Data, scale: 0.5)

accountImage.setImage(myImage, for: UIControlState())
sessionTitle.text = ShareData.sharedInstance.accounts[ShareData.sharedInstance.indexOfCurrentAccount]?.name
var myTest = sessionTitle.text
self.view.setNeedsDisplay()

}

@IBAction func homeButtonTouched(_ sender: UIButton)
{
delegate?.homeButtonTapped()
}
}

我不知道需要调用 self.view.setNeedsDisplay() - 我只是想让它工作。即使标题的变量确实发生了变化,标题也不会改变 - 我已经检查过,并且这是有效的。标签和变量之间存在连接问题,或者 View Controller 没有收到更新信号来更改 View Controller 。我不知道是哪一个——我也不知道如何确定它是哪一个。任何有关如何解决此问题的想法将不胜感激。这是 ViewController 代码:

class SessionDisplayViewController: UIViewController, SessionDisplayViewDelegate
{

@IBOutlet weak var mySessionView: SessionDisplayView!
func homeButtonTapped()
{
self.performSegue(withIdentifier: "ReturnHome", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
mySessionView.onView()
mySessionView.sessionTitle.text = "Test"

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override var shouldAutorotate : Bool {
if (UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft) || (UIDevice.current.orientation == UIDeviceOrientation.landscapeRight) || (UIDevice.current.orientation == UIDeviceOrientation.unknown)
{
return true
}
else
{
return false
}
}

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.portrait, UIInterfaceOrientationMask.portraitUpsideDown]
}
override var preferredStatusBarStyle : UIStatusBarStyle
{
return UIStatusBarStyle.lightContent
}
}

任何有关如何更新 UIView 的想法或建议都将受到欢迎。谢谢。

此致,
肖恩

最佳答案

如何将自定义 View 添加到 Root View ?来自代码还是来自 xib/storyboard?如果您通过 xib/storyboard 完成此操作,则必须重写自定义 View 类中的 awakeFromNib 方法,并在其中调用 commonInitialization 。仅当您通过代码创建 View 时才会调用 init() 方法。

关于ios - 自定义 UIView 标签和动画未显示在 View Controller 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39929834/

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