, 당신이 다른 당신의 "탭 표시 줄"각 버튼에서 SEGUE을 만든 것이 분명하다 뷰 컨트롤러. unwind segue을 제외하고, segues는 항상 전환 할보기 컨트롤러의 새 인스턴스를 만듭니다. 따라서 설정을 사용하여보기 컨트롤러 1에서보기 컨트롤러 2로 전환 한 다음 다시보기 컨트롤러 1로 돌아 가면 원래보기 컨트롤러로 돌아 가지 않고 완전히 새로운보기 컨트롤러 1을 생성하게됩니다.
메모리 소비가 많은 이유입니다. 앱이 충돌 할 때까지 View Controller를 계속 생성합니다.
탭바 컨트롤러를 사용하는 것이 좋습니다. 뷰 컨트롤러를 한 번 앞쪽으로 할당 한 다음 두 뷰 사이를 전환하도록 설계되었습니다. 또한, 그들은 이유에 대한 표준보기를 가지고, 그것은 당신의 애플 리케이션 사용자가 즉시 그들과 상호 작용하는 방법을 알고 도움이됩니다.
탭간에 데이터를 전달하려면 탭을 전환 할 때 끊김이 일어나지 않으므로 섹그를 사용하지 마십시오. 이렇게 할 수있는 방법에는 여러 가지가 있지만, 모든 탭이 모든 탭에서 액세스 할 수있는 모델 데이터를 저장하는 것으로 종결됩니다. 더 큰 응용 프로그램에서 CoreData으로이 작업을 수행 할 수 있습니다. 간단한 응용 프로그램의 경우 다음을 수행 할 수 있습니다.
UITabBarController
의 사용자 정의 하위 클래스를 만듭니다. 그것을 CustomTabBarController
이라고 부르 자. 해당 클래스에서 각 탭에서 액세스 할 모델 데이터를 만들고 보유하게하십시오.
CustomTabBarController.swift : 스토리 보드에서
import UIKit
// This class holds the data for my model.
class ModelData {
var name = "Fred"
var age = 50
}
class CustomTabBarController: UITabBarController {
// Instantiate the one copy of the model data that will be accessed
// by all of the tabs.
var model = ModelData()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
은 Identity Inspector에서이 CustomTabBarController
에 UITabBarController
의 클래스를 변경합니다. 각 탭에 viewWillAppear
에서
, 모델 데이터에 대한 참조를 가져 그리고 당신은 그것을 사용할 수 있습니다. FirstViewController.swift :
import UIKit
class FirstViewController: UIViewController {
override func viewWillAppear(animated: Bool) {
// Get a reference to the model data from the custom tab bar controller.
let model = (self.tabBarController as CustomTabBarController).model
// Show the we can access and update the model data from the first tab.
// Let's just increase the age each time this tab appears and assign
// a random name.
model.age++
let names = ["Larry", "Curly", "Moe"]
model.name = names[Int(arc4random_uniform(UInt32(names.count)))]
}
}
SecondViewController.SWIFT : 당신이 그 코드와 함께 일을하려고 무엇
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var ageLabel: UILabel!
override func viewWillAppear(animated: Bool) {
// Get a reference to the model data from the custom tab bar controller.
let model = (self.tabBarController as CustomTabBarController).model
// This tab will simply access the data and display it when the view
// appears.
nameLabel.text = model.name
ageLabel.text = "\(model.age)"
}
}
? (현재로서는 실제로 아무 것도하지 않습니다 ...) –
나는 "두번째"segue를 호출했습니다 –
그 코드에서 세그개를 호출하지 않고 있습니다 ... secondVC를 로컬 상수로 설정하고 아무 것도하지 않는 것. 그 전체적인 방법을 제거하면 segue가 여전히 수행되는 것을 볼 수 있습니다. –