Swift:如何从另一个控制器设置IBOutlet?

时间:2021-01-30 16:54:01

I want to hide my IBOutlet. However, setting text to empy string works perfectly fine, but when I acces directly my IBOutlet I keep getting error that "unexpectedly found nil while unwrapping optional value".

我想隐藏我的IBOutlet。但是,将文本设置为empy字符串的工作完全正常,但是当我直接访问我的IBOutlet时,我不断收到“在解开可选值时意外发现nil”的错误。

I tried setting my IBOutlet to local varialble, but it gives error as well. I prepared some code snippets with irrelevant lines deleted just to better expose my problem without providing too much code:

我尝试将我的IBOutlet设置为局部变量,但它也会出错。我准备了一些删除了不相关行的代码片段,只是为了更好地揭示我的问题而不提供太多代码:

PageContentViewController

@IBOutlet weak var LabelTop: UILabel!
var Test:UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    self.LabelTop = self.Test   
}

PageViewController

func viewControllerAtIndex(index: Int)-> PageContentViewController?{

    if((self.pageTitles.count == 0) || (index >= self.pageTitles.count))
    {
        return nil
    }

    var pageContentViewController: PageContentViewController = self.storyboard!.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController
    pageContentViewController.imageFile = self.pageImages[index]
    pageContentViewController.titleText = self.pageTitles[index]
    pageContentViewController.pageIndex = index
    if(index == 2){

    pageContentViewController.Test.hidden = true
    }
    else {
        pageContentViewController.Test.hidden = false
    }

    return pageContentViewController

}

The program crashes when trying to access the UIlabel in if statement. Obviously, as I said before, I can easily access text property and set it to an empty string, but I wonder why IBOutlet is unreachable this way.

尝试访问if语句中的UIlabel时程序崩溃。显然,正如我之前所说,我可以轻松访问text属性并将其设置为空字符串,但我想知道为什么IBOutlet无法通过这种方式访问​​。

Thanks in advance!

提前致谢!

2 个解决方案

#1


The problem you getting is that in viewControllerAtIndex method you trying to set the outlet but the view controller (PageContentViewController) was not loaded to the view hierarchy so the outlet is still nil.

您遇到的问题是,在viewControllerAtIndex方法中,您尝试设置插座但视图控制器(PageContentViewController)未加载到视图层次结构,因此插座仍为零。

One of the solution you can use is add a property to the PageContentViewController for example isHidden and in viewDidAppear check that property and hide the view or show it:

您可以使用的解决方案之一是向PageContentViewController添加一个属性,例如isHidden,并在viewDidAppear中检查该属性并隐藏视图或显示它:

var isHidden:Bool = true 
override func viewDidAppear(animated: Bool) {
    superviewDidAppear(animated)

    self.Test.hidden = isHidden
}

in the viewControllerAtIndex method change the lines which set up hidden property of the Test control to isHidden:

在viewControllerAtIndex方法中,将设置Test控件的隐藏属性的行更改为isHidden:

if(index == 2){

    pageContentViewController.isHidden = true
    }
    else {
        pageContentViewController.isHidden = false
    }

#2


You have declared " var Test:UILabel! " which is not optional, so in this case, first you have to initialize it. You are directly accessing its properties without initializing, this is not possible.

你已声明“var Test:UILabel!”这不是可选的,所以在这种情况下,首先你必须初始化它。您无需初始化即直接访问其属性,这是不可能的。

Either you need to create Test as " var Test:UILabel? " and then create Test programmatically and set its all other properties like text and all.

您是否需要将Test创建为“var Test:UILabel?”,然后以编程方式创建Test并设置其所有其他属性,如text和all。

OR you can create Boolean variable in your next class PageContentViewController, set Boolean variable value instead of setting " pageContentViewController.Test.hidden " value, then after in PageContentViewController hide/show your label.

或者您可以在下一个类PageContentViewController中创建布尔变量,设置布尔变量值而不是设置“pageContentViewController.Test.hidden”值,然后在PageContentViewController中隐藏/显示您的标签。

#1


The problem you getting is that in viewControllerAtIndex method you trying to set the outlet but the view controller (PageContentViewController) was not loaded to the view hierarchy so the outlet is still nil.

您遇到的问题是,在viewControllerAtIndex方法中,您尝试设置插座但视图控制器(PageContentViewController)未加载到视图层次结构,因此插座仍为零。

One of the solution you can use is add a property to the PageContentViewController for example isHidden and in viewDidAppear check that property and hide the view or show it:

您可以使用的解决方案之一是向PageContentViewController添加一个属性,例如isHidden,并在viewDidAppear中检查该属性并隐藏视图或显示它:

var isHidden:Bool = true 
override func viewDidAppear(animated: Bool) {
    superviewDidAppear(animated)

    self.Test.hidden = isHidden
}

in the viewControllerAtIndex method change the lines which set up hidden property of the Test control to isHidden:

在viewControllerAtIndex方法中,将设置Test控件的隐藏属性的行更改为isHidden:

if(index == 2){

    pageContentViewController.isHidden = true
    }
    else {
        pageContentViewController.isHidden = false
    }

#2


You have declared " var Test:UILabel! " which is not optional, so in this case, first you have to initialize it. You are directly accessing its properties without initializing, this is not possible.

你已声明“var Test:UILabel!”这不是可选的,所以在这种情况下,首先你必须初始化它。您无需初始化即直接访问其属性,这是不可能的。

Either you need to create Test as " var Test:UILabel? " and then create Test programmatically and set its all other properties like text and all.

您是否需要将Test创建为“var Test:UILabel?”,然后以编程方式创建Test并设置其所有其他属性,如text和all。

OR you can create Boolean variable in your next class PageContentViewController, set Boolean variable value instead of setting " pageContentViewController.Test.hidden " value, then after in PageContentViewController hide/show your label.

或者您可以在下一个类PageContentViewController中创建布尔变量,设置布尔变量值而不是设置“pageContentViewController.Test.hidden”值,然后在PageContentViewController中隐藏/显示您的标签。