所有IBOutlets都是零

时间:2015-04-25 13:24:22

标签: ios swift null iboutlet

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

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl as vm">
  
  <label for="radType1"><input type="radio" name="radType" id="radType1" ng-model="vm.listType" value="1">Ducks</label>
  <label for="radType2"><input type="radio" name="radType" id="radType2" ng-model="vm.listType" value="2">Mice</label>
  
  <br>
  <select ng-options="o.name for o in vm.getListOptions()" ng-model="vm.selectedThing">
  </select>
</div>

在此之后我转到 var aboutUsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("AboutUsViewController") as AboutUsViewController 。但是aboutViewController IBOutlets aboutViewController nil都是 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) !为什么会这样? 我正在使用MMDrawer库以获得侧栏菜单。

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.
    }
    */

}

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

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

    </div>
    <ul class="nav navbar-nav">
        <li><a href="#portfolio"><span class="glyphicon glyphicon-briefcase"></span><br/> Portfolio</a>
        </li>
        <li><a href="#skills"><span class="glyphicon glyphicon-signal"></span><br/> Skills</a>
        </li>
        <li><a href="#blog"><span class="glyphicon glyphicon-menu-right"></span><br/> Blog</a>
        </li>
    </ul>
    <ul class="nav navbar-nav navbar-right">
        <li style="border-left: 1px solid rgb(215,215,215);"><a href="#contact"><span class="glyphicon glyphicon-envelope"></span><br/> Contact</a>
        </li>
        <li style="padding-right: 0.8em;"><a href="#about"><span class="glyphicon glyphicon-user"></span><br/> CV</a>
        </li>
    </ul>
</nav>
<section class="section-bg intro-bg">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">Mooses are cool and big and stuff.</div>
        </div>
    </div>
</section>

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

2 个答案:

答案 0 :(得分:16)

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

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

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

var message: String!

当您实例化AboutUsViewController时,您需要设置该属性:

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的{​​{1}}将使用该属性相应地填充出口:

AboutUsViewController

但是你永远不会让原始视图控制器尝试设置/调整目标场景的出口。这是override func viewDidLoad() { super.viewDidLoad() label1.text = message } 的责任。

只是为了确认,你的网点是否正确连接?下面显示了两种确认它们正确连接的方法。

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

enter image description here

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

enter image description here

答案 1 :(得分:2)

您可以在演示之前使用此功能,这在iOS9及更高版本中可用:

    aboutUsViewController.loadViewIfNeeded()