Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Swift 4中按需在多页UI iOS应用程序(水平旋转木马)中生成其他视图控制器?_Ios_Swift_Xcode - Fatal编程技术网

如何在Swift 4中按需在多页UI iOS应用程序(水平旋转木马)中生成其他视图控制器?

如何在Swift 4中按需在多页UI iOS应用程序(水平旋转木马)中生成其他视图控制器?,ios,swift,xcode,Ios,Swift,Xcode,我制作了一个简单的天气应用程序作为辅助项目(我不是软件工程师),它可以同时显示华氏温度和摄氏温度。用户可以在城市之间水平滑动: 我通过使用Xcode中提供的多页应用程序模板作为新的应用程序模板实现了这一点,直到Xcode 9。在Xcode 10之前构建应用程序时,问题是当用户将城市添加到城市阵列中时,如果用户在添加新城市时位于旋转木马的最后一页,则不会呈现新添加城市的页面。即便如此,当用户向左再向右滑动时,新添加的城市也会出现。现在,在Xcode 11中,除非用户重新启动应用程序,否则新添加城

我制作了一个简单的天气应用程序作为辅助项目(我不是软件工程师),它可以同时显示华氏温度和摄氏温度。用户可以在城市之间水平滑动:

我通过使用Xcode中提供的多页应用程序模板作为新的应用程序模板实现了这一点,直到Xcode 9。在Xcode 10之前构建应用程序时,问题是当用户将城市添加到城市阵列中时,如果用户在添加新城市时位于旋转木马的最后一页,则不会呈现新添加城市的页面。即便如此,当用户向左再向右滑动时,新添加的城市也会出现。现在,在Xcode 11中,除非用户重新启动应用程序,否则新添加城市的新视图控制器永远不会呈现。这是我想解决的一个相当大的问题

我需要帮助,了解在调用
addCity
后,如何根据需要将新的视图控制器呈现到现有的多页旋转木马中,即使用户当前处于最后一页并且不需要重新启动应用程序

下面是驱动多页旋转木马的完整代码。为里面的一些骇人的东西道歉。就上下文而言,我是一名非技术产品经理,试图将一些iOS应用程序作为辅助项目

导入UIKit
进口存储套件
/*
管理简单模型的控制器对象
控制器用作页面视图控制器的数据源;因此,它实现了pageViewController:ViewControllerBeforReviewController:和pageViewController:viewControllerAfterViewController:。
它还实现了一个自定义方法viewControllerAtIndex:它在数据源方法的实现和应用程序的初始配置中非常有用。
实际上,不需要提前为每个页面创建视图控制器——实际上这样做会产生不必要的开销。给定数据模型,这些方法根据需要创建、配置和返回新的视图控制器。
*/
类ModelController:NSObject,UIPageViewControllerDataSource{
var rootViewController=rootViewController()
var cities=[“”]
让defaults=UserDefaults.standard
让currentCount=UserDefaults.standard.integer(forKey:“launchCount”)
var pageViewController:UIPageViewController?
重写init(){
super.init()
self.cities=self.defaults.stringArray(forKey:“SavedStringArray”)??[String]()
如果self.cities=[“”]| | self.cities.count==0{
self.cities=[“当前位置”、“旧金山”、“纽约”]
}
}
func addCity(名称:字符串){
self.cities.append(名称)
self.defaults.set(self.cities,forKey:“SavedStringArray”)
}
func viewControllerAtIndex(index:Int,情节提要:UIStoryboard)->DataViewController{
//返回给定索引的数据视图控制器。
if(self.cities.count==0)| |(index>=self.cities.count){
归零
}
//创建新的视图控制器并传递适当的数据。
让dataViewController=storyboard.InstanceEviewController(标识符为:“dataViewController”)作为!dataViewController
//获取城市名称
dataViewController.dataObject=self.cities[索引]
返回数据视图控制器
}
func indexOfViewController(uviewcontroller:DataViewController)->Int{
//返回给定数据视图控制器的索引。
//为简单起见,此实现使用模型对象的静态数组,视图控制器存储模型对象;因此,您可以使用模型对象来标识索引。
返回self.cities.firstIndex(of:viewController.dataObject)??NSNotFound
}
//标记:-页视图控制器数据源
func pageViewController(uPageViewController:UIPageViewController,viewController在viewController:UIViewController之前)->UIViewController{
var index=self.indexOfViewController(viewController为!DataViewController)
如果(索引==0)| |(索引==NSNotFound){
归零
}
索引-=1
返回self.viewControllerAtIndex(索引,情节提要:viewController.storyboard!)
}
func pageViewController(pageViewController:UIPageViewController,viewController后的viewController:UIViewController)->UIViewController{
var index=self.indexOfViewController(viewController为!DataViewController)
如果索引==NSNotFound{
归零
}
指数+=1
如果索引==self.cities.count{
归零
}
如果索引>1&¤tCount>2{
打印(“请求审核”)
SKStoreReviewController.requestReview()
}
返回self.viewControllerAtIndex(索引,情节提要:viewController.storyboard!)
}
}