gpt4 book ai didi

ios - 所有 IBOutlets 均为零

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:18 25 4
gpt4 key购买 nike

您好,我正在使用这段代码初始化 viewController:

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

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

 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 并将其移动到左侧之后......问题是当我初始化我的 Controller 时,导出为零

我在使用 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.
}
*/

}

我遇到标签和图像导出为零的问题!!!我在使用 MMDrawer 库时遇到了同样的问题。同样的情况。我没有使用 segues !我想因为那个网点是零

最佳答案

我假设您在实例化场景后立即查看 socket (如果使用 segues 并尝试使用 prepareForSegue 中的 socket ,您也会看到此行为)。但是在您呈现/推送到该场景、加载 View 并调用 viewDidLoad 之前, socket 不会连接。

最重要的是,在调用 viewDidLoad 之前不要尝试使用 socket (即不要在实例化场景后立即使用 socket )。相反,将所需的数据传递到目标场景(例如 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)

然后 AboutUsViewControllerviewDidLoad 将使用该属性相应地填充 socket :

override func viewDidLoad() {
super.viewDidLoad()

label1.text = message
}

但是您永远不会让原始 View Controller 尝试设置/调整目标场景的导出。这是 AboutUsViewController 的职责。


只是为了确认一下,您的 socket 是否已正确连接?下面显示了两种确认它们是否正确连接的方法。

当您在 Interface Builder 中选择 View Controller 然后查看连接检查器时,您是否看到您的 socket 连接在那里?在以下快照中,连接了 label1,但未连接 label2:

enter image description here

或者,当您查看代码并查看 socket 时,您是否在左边看到实心点(表示 socket 已连接)或空点(表示 socket 未连接)?

enter image description here

关于ios - 所有 IBOutlets 均为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29865775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com