gpt4 book ai didi

ios - Swift、协议(protocol)和变量值未存储...?

转载 作者:可可西里 更新时间:2023-11-01 00:52:46 26 4
gpt4 key购买 nike

我是协议(protocol)和页面浏览 Controller 的新手,我尝试了一些我希望很简单的事情。将变量值从内容 View Controller 传递到页面 ListView Controller ,但它不起作用,所以我做错了什么......

在我的 Storyboard中,我有一个 ViewController(作为页面 ListView Controller )并嵌入在导航 Controller 中。导航栏上有一个按钮。

有一个带有标签的 PageContentViewController。

我使用的代码如下(使用@Tom Elliott 提供的公认解决方案进行编辑):

protocol setRoomOnScreenProtocol {
func updateRoomOnScreen(myRoom: String)
}

class ViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, setRoomOnScreenProtocol {

@IBAction func press1(sender: AnyObject) {

println("roomOnScreen: \(roomOnScreen)")
println("pageTitles: \(pageTitles)")
}

@IBOutlet weak var btn1: UIBarButtonItem!
let pageTitles = ["Room 1", "Room 2", "Room 3", "Room 4"]
var count = 0

var pageViewController : UIPageViewController!

var roomOnScreen: String = ""

override func viewDidLoad() {
super.viewDidLoad()
reset()
}

func updateRoomOnScreen(myRoom: String) {

roomOnScreen = myRoom
println("roomOnScreen (PGL): \(roomOnScreen)")
}

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)

self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
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++

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.delegate = self

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
}

和 PageContentViewController:

class PageContentViewController: UIViewController {

var pageIndex: Int?
var titleText : String!

var delegate: setRoomOnScreenProtocol!

@IBOutlet weak var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()

self.label1.text = self.titleText

}

override func viewDidAppear(animated: Bool) {

if let actualDelegate = self.delegate {
actualDelegate.updateRoomOnScreen(titleText)
} else {
println("error")
}

doMore()
}

func doMore() {

println("showing...:\(titleText)")
}


}

当我浏览页面时,我正确地得到了输出:

roomOnScreen (PGL): Room 1
showing...:Room 1
roomOnScreen (PGL): Room 2
showing...:Room 2

但是当我点击导航栏按钮时,我没有得到任何值...

roomOnScreen:
pageTitles: [Room 1, Room 2, Room 3, Room 4]

我做错了什么......?

谢谢!

最佳答案

行:

self.delegate = ViewController()

实际上是在每次调用时创建一个全新的 ViewController 实例,因此可见的原始 ViewController 没有收到消息。

我建议您在最初创建页面时设置委托(delegate):

let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController
pageContentViewController.delegate = self

关于ios - Swift、协议(protocol)和变量值未存储...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31723325/

26 4 0
文章推荐: java - JAXB 编码 Set