【问题标题】:Custom UIView label and animation not showing up in view controller自定义 UIView 标签和动画未显示在视图控制器中
【发布时间】:2016-10-08 07:24:16
【问题描述】:

我实际上有一个可以工作的自定义 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() - 我只是想让它工作。即使标题的变量确实发生了变化,标题也不会改变 - 我已经检查过了,这是有效的。标签和变量之间存在连接问题,或者视图控制器没有收到更新信号来更改视图控制器。我不知道是哪一个——我也不知道如何确定它是哪一个。任何有关如何解决此问题的想法将不胜感激。这是 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 的想法或建议都将受到欢迎。谢谢。

此致
肖恩

【问题讨论】:

    标签: ios swift uiview swift3


    【解决方案1】:

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

    【讨论】:

    • 我试过了,但没有帮助。事实证明,视图控制器以某种方式损坏了——我删除了它并重新创建了它,并且它起作用了。我不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多