问题
I am on the beginning stages of my project. It is a UITabbedApplication with three tabs. The tab at [0] position is the "Location picker", the [1] tab is the "view profiles" from the location picked in the Location picker view. Depending on the location that is first chosen I want the view profiles tab to show profiles from that specific location. Would I be able to do this with container views or is the way I want this set up not going to work? Any advice would be helpful.
//tab [0] choose location
class ChooseLocationVC: UIViewController {
@IBAction func chooseAction(_ sender: Any) {
if pageControl.currentPage == 0{
print("LA")
}
else if pageControl.currentPage == 1{
print("SF")
}else if pageControl.currentPage == 2{
print("NY")
}else if pageControl.currentPage == 3{
print("Miami")
}else if pageControl.currentPage == 4{
print("LasVegas")
}else if pageControl.currentPage == 5{
print("Chicago")
}
}
@IBOutlet weak var collectionView: UICollectionView!
var thisWidth:CGFloat = 0
@IBOutlet weak var pageControl: UIPageControl!
var imageArray = [UIImage(named: "LA"),UIImage(named: "SF"), UIImage(named: "NY"), UIImage(named: "Miami"), UIImage(named: "LasVegas"), UIImage(named: "Chicago")]
override func viewDidLoad() {
super.viewDidLoad()
setLabels()
// thisWidth = CGFloat(self.frame.width)
collectionView.delegate = self
collectionView.dataSource = self
// 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 collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell
cell.locationImage.image = imageArray[indexPath.row]
return cell
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
pageControl.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
setLabels()
print(pageControl.currentPage)
}
}
回答1:
My StoryBoard :
A tab Bar Controller - >
-> VC2 tab[0]
-> VC3 tab[1]
My Struct Class that Will take data from VC2 To VC3
struct Global
{
static var Location : String!
}
My VC2 Class :
import UIKit
class VC2: UIViewController {
@IBOutlet weak var textTF: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool)
{
print("Called of vC2")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func saveValue(_ sender: Any)
{
Global.Location = self.textTF.text
}
}
My VC3 Class:
import UIKit
class VC3: UIViewController {
@IBOutlet weak var resultTf: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
print("Called of vC3")
if Global.Location != ""
{
self.resultTf.text = Global.Location
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
来源:https://stackoverflow.com/questions/48575507/uitabcontroller-app-setup