所有IBOutlets都是零 [英] All IBOutlets are nil

查看:71
本文介绍了所有IBOutlets都是零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用以下代码初始化viewController:

Hello I am initializing viewController using this code:

 var aboutUsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("AboutUsViewController") as AboutUsViewController

之后我转到 aboutViewController 。但是 aboutViewController 上的所有 IBOutlets 都是 nil !为什么?
我正在使用MMDrawer库以获得侧栏菜单。

After this I move to aboutViewController. However all my IBOutlets on aboutViewController are nil! Why is that so? I am using MMDrawer library in order to have side bar menu.

 var aboutUsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("AboutUsViewController") as AboutUsViewController


        var aboutUsNavController = UINavigationController(rootViewController: aboutUsViewController)

        var appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate

        appDelegate.centerContainer!.centerViewController = aboutUsNavController
        appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)

在侧栏上,我选择了关于我们的菜单项,在我创建AboutViewController并将其向左移动后...问题是,当我初始化我的控制器时,出口是零

On the side bar I am choosing about us menu item and after I am creating AboutViewController and moving it to the left... The problem is that when I initialize my controller , outlets are nil

我在使用pageViewController时也遇到这样的问题:

I AM also having such kind of problem when using pageViewController:

class ViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {


let pageTitles = ["Title 1", "Title 2", "Title 3", "Title 4"]
var images = ["long3","long4","long1","long2"]
var count = 0


var pageViewController : UIPageViewController!

override func viewDidLoad() {
    super.viewDidLoad()
    reset()
    // 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.
}

func reset() {
    /* Getting the page View controller */
    pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as UIPageViewController
    self.pageViewController.dataSource = self

    let pageContentViewController = self.viewControllerAtIndex(0)
    self.pageViewController.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)

    /* We are substracting 30 because we have a start again button whose height is 30*/
    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height - 30)
    self.addChildViewController(pageViewController)
    self.view.addSubview(pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)
}



func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    var index = (viewController as PageContentViewController).pageIndex!
    index++
    if (index >= self.images.count){
        return nil
    }

    return self.viewControllerAtIndex(index)
}


func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    var index = (viewController as PageContentViewController).pageIndex!

    if index<=0 {
        return nil
    }
    index--
    return self.viewControllerAtIndex(index)
}

func viewControllerAtIndex(index : Int) -> UIViewController? {
    if((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
        return nil
    }
    let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageContentViewController") as PageContentViewController

    pageContentViewController.imageName = self.images[index]
    pageContentViewController.titleText = self.pageTitles[index]
    pageContentViewController.pageIndex = index
    return pageContentViewController
}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return pageTitles.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0
    }

}



class PageContentViewController: UIPageViewController {


@IBOutlet var imageViewMain: UIImageView!

@IBOutlet var labelMain: UILabel!

var pageIndex: Int!
var titleText: String!
var imageName: String!


override func viewDidLoad() {
    super.viewDidLoad()

    self.imageViewMain.image = UIImage(named:imageName)
    self.labelMain.text = self.titleText
    self.labelMain.alpha = 0.1
    UIView.animateWithDuration(1.0, animations:{ ()-> Void in
        self.labelMain.alpha = 1.0
    })
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation



  // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

I我有标签和图像插座是零的问题!我使用MMDrawer库时遇到了同样的问题。同样的情况。我不是在使用segues!我认为因为那些出口是零

I am having problem that label and image outlet is nil!!! The same problem I had using MMDrawer library. The same situation. I am not using segues ! I think because of that outlets are nil

推荐答案

我假设你在实例化场景后立即查看出口(你'如果使用segues并尝试使用 prepareForSegue 中的插座,也会看到此行为。但是直到你出现/推送到那个场景之后才连接出口,加载视图,并且调用 viewDidLoad

I assume you're looking at the outlets immediately after instantiating the scene (you'll also see this behavior if using segues and trying to use the outlets in prepareForSegue). But the outlets are not hooked up until after you present/push to that scene, the view is loaded, and viewDidLoad is called.

底线,不要尝试在 viewDidLoad 被调用之前使用插座(即在实例化场景后不要立即使用插座)。而是将所需数据传递到目标场景(例如 String 属性),并且目标的 viewDidLoad 应该然后填充插座。

Bottom line, do not try to use the outlets prior to viewDidLoad being called (i.e. do not use outlets immediately after instantiating the scene). Instead, pass the needed data to the destination scene (e.g. a String property), and the viewDidLoad of the destination should then populate the outlet.

例如,您可能在中有一个属性AboutUsViewController

var message: String!

当你实例化 AboutUsViewController 时,你会设置该属性:

When you instantiate AboutUsViewController, you'd set that property:

let aboutUsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("AboutUsViewController") as AboutUsViewController // use `as!` in Swift 1.2

aboutUsViewController.message = "some message"

presentViewController(aboutUsViewController, animated: true, completion: nil)

然后 viewDidLoad AboutUsViewController 将使用该属性相应地填充插座:

And then the viewDidLoad of AboutUsViewController would use that property to populate the outlet accordingly:

override func viewDidLoad() {
    super.viewDidLoad()

    label1.text = message
}

但你会从来没有原始视图控制器尝试设置/调整目标场景的出口。这是 AboutUsViewController 的责任。

But you would never have the originating view controller try to set/adjust the outlets of the destination scene. That's the responsibility of AboutUsViewController.

待确认,是你的网点连接得好吗?下面显示了两种方法来确认它们是否正确连接。

Just to confirm, are your outlets hooked up properly? The below shows two ways to confirm that they are hooked up properly.

当您在Interface Builder中选择视图控制器然后查看连接检查器时,您是否看到了网点连接在那里?在以下快照中, label1 已连接,但 label2 不是:

When you select the view controller in Interface Builder and then look in the connections inspector, do you see your outlets hooked up there? In the following snapshot, label1 is hooked up, but label2 is not:

或者当您查看代码并查看插座时,您是否看到左边距中的实心点(意味着插座已连接)或空点(意味着插座未连接)?

Or when you look at your code, and look at the outlets, do you see solid dots in the left margin (meaning the outlet is hooked up) or empty dots (meaning the outlet is not hooked up)?

这篇关于所有IBOutlets都是零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆