IBOutlet elements appear when I try to set them in viewDidLoad

I create a carousel in which there will be 3-4 dispatchers. I created a viewcontroller in a storyboard, and I set IBOutlets inside viewDidLoad (). My plan is to instantiate these ViewControllers and dynamically change their iboutlets. Since their viewDidLoad functions will be called when they are loaded, here I set the iboutlet elements. There is custom controller code here:

import UIKit

class CarouselViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var label: UILabel!


var imageName: String?
var bottomText: String?

convenience init(imageName: String, bottomText: String){
    self.init()
    self.imageName = imageName
    self.bottomText = bottomText
}

override func viewDidLoad() {
    if(self.imageView==nil){
        println("nil")
    }
    self.imageView.image = UIImage(named: self.imageName!)
    self.label.text = self.bottomText
}
}

And this is how I create it:

var vc1 = CarouselViewController(imageName: "tour1", bottomText: "This is view1")

However, in viewDidload (), self.imageView gives a nil.Exact error message:

fatal error: unexpectedly found nil while unwrapping an Optional value

. , . , -, . - ?

: super.viewDidLoad() viewDidLoad(),

Edit2: viewcontrollers:

var vc1 = CarouselViewController(imageName: "tour1", bottomText: "This is view1")
var vc2 = CarouselViewController(imageName: "tour2", bottomText: "This is view2")


    let pages = PagesController([vc1, vc2])
    self.presentViewController(pages, animated: true) { () -> Void in
        pages.goTo(0)
    }

Edit3: , , @Roux, . UIViewControllers . : UIViewController nib ( UIViewController) init

self.init(nibname:"NibName", bundle: nil)
+4
3

iOS7 .

, viewDidLayoutSubviews, . , viewDidLayoutSubviews .

var first = false

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews()
    if self.first {
        //Update your views
        self.first = false
    }
}

view.layoutSubviews(), ;;

, ! , IBOutlets ;)

var vc1 = storyboard?.instantiateViewControllerWithIdentifier("YourControllerStoryboardId") as! CarouselViewController

, !

+3

Super-classes viewDidLoad ?

override func viewDidLoad() {
    super.viewDidLoad()
    if(self.imageView==nil){
        println("nil")
    }
    self.imageView.image = UIImage(named: self.imageName!)
    self.label.text = self.bottomText
}    
0

( ) - awakeFromNib() viewDidLoad(). IBOutlets, awakeFromNib().

0
source

Source: https://habr.com/ru/post/1599070/


All Articles