How to open a embedded segue controller from another controller? - ios

I am making an app following the same design approach of this app https://github.com/Bailig/SurveyApp .
In the above GitHub project the controllers are designed in such a way like a common ViewController with ContainerView inside.And also this common ViewController is Embed Segue with the First Controller or very first page. The container view is getting replaced for all the next screens.
In the above project there is a thank you page in the last, in my app I placed a button on the thank you page. I want to open the very first page again on click of the button from thank you page. How can I do that from last page controller/FourthController?
ViewController
class ViewController: UIViewController {
var currentController: UIViewController?
var pageIndex = 1
var survey = Survey()
#IBOutlet weak var backButton: UIButton!
#IBOutlet weak var nextButton: UIButton!
#IBOutlet weak var containerView: UIView!
#IBOutlet weak var progressLabel: UILabel!
#IBAction func nextTapped(_ sender: Any) {
switch pageIndex {
case 1:
let nextController = storyboard?.instantiateViewController(withIdentifier: "SecondController")
if let fromController = currentController, let toController = nextController as? SecondController {
toController.survey = survey
moveAndSizeChildControllers(fromController: fromController, toController: toController)
pageIndex += 1
setButtonAndProgressLabel()
}
case 2:
let nextController = storyboard?.instantiateViewController(withIdentifier: "ThirdController")
if let fromController = currentController, let toController = nextController as? ThirdController {
toController.survey = survey
moveAndSizeChildControllers(fromController: fromController, toController: toController)
pageIndex += 1
setButtonAndProgressLabel()
}
case 3:
let nextController = storyboard?.instantiateViewController(withIdentifier: "FourthController")
if let fromController = currentController, let toController = nextController as? FourthController {
toController.survey = survey
moveAndSizeChildControllers(fromController: fromController, toController: toController)
pageIndex += 1
setButtonAndProgressLabel()
}
default:
break
}
}
#IBAction func backTapped(_ sender: Any) {
switch pageIndex {
case 2:
let nextController = storyboard?.instantiateViewController(withIdentifier: "FirstController")
if let fromController = currentController, let toController = nextController as? FirstController {
toController.survey = survey
moveAndSizeChildControllers(fromController: fromController, toController: toController)
pageIndex -= 1
setButtonAndProgressLabel()
}
case 3:
let nextController = storyboard?.instantiateViewController(withIdentifier: "SecondController")
if let fromController = currentController, let toController = nextController as? SecondController {
toController.survey = survey
moveAndSizeChildControllers(fromController: fromController, toController: toController)
pageIndex -= 1
setButtonAndProgressLabel()
}
default:
break
}
}
#IBOutlet weak var backTapped: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
currentController = segue.destination
if let firstController = currentController as? FirstController {
firstController.survey = survey
}
setButtonAndProgressLabel()
}
func moveAndSizeChildControllers(fromController: UIViewController, toController: UIViewController) {
fromController.willMove(toParentViewController: nil)
toController.view.frame = containerView.bounds
addChildViewController(toController)
containerView.addSubview(toController.view)
// animatin
toController.view.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
toController.view.alpha = 1
fromController.view.alpha = 0
}) { (completed) in
fromController.view.removeFromSuperview()
fromController.removeFromParentViewController()
toController.didMove(toParentViewController: self)
self.currentController = toController
}
}
}

You can manage it by using Notification
Add in ViewController (main)
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(navigateToFirstPage), name:NSNotification.Name(rawValue:"NavigateToFirstPage") , object: nil)
}
#objc private func navigateToFirstPage() {
pageIndex = 2
self.backTapped(UIButton())
}
Add in you fourthViewController
// This is your button from where you need to perform action
#IBAction func thanksButtonAction(_ sender: Any) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue:"NavigateToFirstPage"), object: nil)
}

I would better use delegation pattern. It's simple to manage and easily to understand.
On the top of FourthController after imports you need to add
protocol FourthControllerThankDelegate {
func didTapYourButton()
}
and then add the variable inside that class
class FourthController: UIViewController {
weak var delegate: FourthControllerThankDelegate?
...
}
then you need to call didTapYourButton() inside your button action handler
#IBAction func thanksButtonTapped(_ sender: Any) {
delegate?.didTapYourButton()
}
then inside your ViewController function nextTapped: you need to set this view controller as a delegate to FourthController
case 3:
... // I have skipped code above
toController.delegate = self
setButtonAndProgressLabel()
}
and finally you need to handle delegate action
extension ViewController: FourthControllerThankDelegate {
func didTapYoutButton {
pageIndex = 2
backTapped(self)
}
}
As you do that, backTapped handler will move you to first page. This is not the best way to move back. To improve that you can create a function "displayMeFirstPage" and move there part of the logic from backTapped() case: 2 and call it inside that case and inside delegate handler. There are a lot of ways to improve navigation. if you have any questions please let me know.

Related

Pass data between childViewControllers Swift

I put a container inside my MainViewController. Then added FirstViewController inside that container
let containerView = UIView()
view.addSubview(containerView)
let controller = FirstViewController()
addChildViewController(controller)
containerView.addSubview(controller.view)
controller.didMove(toParentViewController: self)
There are a UITextField and a UIButton inside FirstViewController. There is a label inside SecondViewController and var passedText = "" to update the UILabel text. I just want to set the UILabel text according to the UITextField text from FirstViewController when I press UIButton and it should leads to SecondViewController as well. This is the UIButton action method from FirstViewController. I'm not using Storyboard
#IBAction func btnPressed(_ sender: Any) {
let secondVC = SecondViewController()
addChildViewController(secondVC)
view.addSubview(secondVC.view)
secondVC.view.frame = view.bounds
secondVC.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
secondVC.didMove(toParentViewController: self)
secondVC.passedText = inputTextField.text!
}
This is FirstViewController
class FirstViewController: UIViewController {
#IBOutlet weak var inputTextField: UITextField!
let secondVC = SecondViewController()
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btnPressed(_ sender: Any) {
addChildViewController(secondVC)
view.addSubview(secondVC.view)
secondVC.view.frame = view.bounds
secondVC.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
secondVC.didMove(toParentViewController: self)
secondVC.passedText = inputTextField.text!
}
}
This is SecondViewController
class SecondViewController: UIViewController {
var passedText = ""
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = passedText
}
}
Closure
class ParentViewController: UIViewController {
var embeddedViewController: UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
let firstViewController = FirstViewController()
embedViewController(firstViewController)
firstViewController.passDataClosure = { [weak self] text in
let secondViewController = SecondViewController()
self?.embedViewController(secondViewController)
secondViewController.passedText = text
}
}
func embedViewController(_ viewController: UIViewController) {
removeEmbedded()
viewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
addChildViewController(viewController)
view.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
embeddedViewController = viewController
}
func removeEmbedded() {
embeddedViewController?.view.removeFromSuperview()
embeddedViewController?.didMove(toParentViewController: nil)
embeddedViewController?.removeFromParentViewController()
}
}
-
class FirstViewController: UIViewController {
var passDataClosure: ((String?) -> Void)?
#IBOutlet weak var inputTextField: UITextField!
#IBAction func btnPressed(_ sender: Any) {
passDataClosure?(inputTextField.text)
}
}
-
class SecondViewController: UIViewController {
var passedText: String? {
didSet {
label.text = passedText
}
}
#IBOutlet weak var label: UILabel!
}
Delegate
class ParentViewController: UIViewController, FirstViewControllerDelegate {
var embeddedViewController: UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
let firstViewController = FirstViewController()
embedViewController(firstViewController)
firstViewController.delegate = self
}
func embedViewController(_ viewController: UIViewController) {
removeEmbedded()
viewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
addChildViewController(viewController)
view.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
embeddedViewController = viewController
}
func removeEmbedded() {
embeddedViewController?.view.removeFromSuperview()
embeddedViewController?.didMove(toParentViewController: nil)
embeddedViewController?.removeFromParentViewController()
}
// MARK: FirstViewControllerDelegate
func firstViewController(_ firstViewController: FirstViewController, pass text: String?) {
let secondViewController = SecondViewController()
embedViewController(secondViewController)
secondViewController.passedText = text
}
}
-
class SecondViewController: UIViewController {
var passedText: String? {
didSet {
label.text = passedText
}
}
#IBOutlet weak var label: UILabel!
}
-
protocol FirstViewControllerDelegate: class {
func firstViewController(_ firstViewController: FirstViewController, pass text: String?)
}
class FirstViewController: UIViewController {
weak var delegate: FirstViewControllerDelegate?
#IBOutlet weak var inputTextField: UITextField!
#IBAction func btnPressed(_ sender: Any) {
delegate?.firstViewController(self, pass: inputTextField.text)
}
}
As far as I can see, this should work:
secondVC.label.text = inputTextField.text
You currently have secondVC.label = inputTextField.text which shouldn't even compile.
you can create a singleton class for your scenario. so that you can access all of your important data from anywhere either from your firstViewController or SecondViewController.
class YourDataClass : NSObject{
var textFieldData = String()
var lblSeconddata = String()
struct Static
{
public static var instance: YourDataClass?
}
class var singleton: YourDataClass
{
if Static.instance == nil
{
Static.instance = YourDataClass()
}
return Static.instance!
}
}
now somewhere in your controller
YourDataClass.Singleton.textFieldData = txtFld.Text
YourDataClass.Singleton. lblSeconddata = lblText.Text
in smame way you can get it anywhere

Multiple Array Passing with SpriteKit - UIColor and String

I have three data view controllers: settingsVC.swift, menuVC.swift and GameScene.swift
I am attempting to use a prepareforsegue method for the transfer of [UIColor] and [String] array's from settings to menuVC and then another transfer from menuVC to GameScene.swift using a instantiateViewController. I have declared empty arrays in each and just want to pass data from one page to another and am receiving 'use of unresolved identifier' on both of my arrays. Any help is appreciated.
class MenuVC: UIViewController {
var colourSettingsMenu = [UIColor]()
var otherSettingsMenu = [String]()
//action function for button to transfer to a 1 player game
#IBAction func Player1Game(_ sender: MenuVC) {
moveToGame(game: .player1, colourSettings: colourSettingsMenu, otherSettings: otherSettingsMenu)
}
//action function for button to transfer to a 2 player game
#IBAction func Player2Game(_ sender: MenuVC) {
moveToGame(game: .player2, colourSettings: colourSettingsMenu, otherSettings: otherSettingsMenu)
}
func moveToGame(game : gameType, colourSettings: [UIColor], otherSettings: [String]) {
let GameVC = self.storyboard?.instantiateViewController(withIdentifier: "GameVC") as! GameViewController
currentGameType = game
colourSettingsGame.GameVC = colourSettings;
otherSettingsGame.GameVC = otherSettings;
self.navigationController?.pushViewController(GameVC, animated: true)
}
#IBAction func moveToSettings(_ sender: Any) {
performSegue(withIdentifier: "settings", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender:Any?) {
if (segue.identifier == "settings") {
let SettingsVC = segue.destination as! SettingsVC
SettingsVC.colourSettings = colourSettingsMenu
SettingsVC.otherSettings = otherSettingsMenu
}
}
}
class SettingsVC : UIViewController {
//Colours for segue transfer
var player1Colour: UIColor?
var player2Colour: UIColor?
var ballColour: UIColor?
// other settings to allow segue transfer
// do we need these
var AIDiff: String?
var paddleSi: String?
var ballSi: String?
//arrays for segue transfer
var colourSettings = [UIColor]()
var otherSettings = [String]()
override func prepare(for segue: UIStoryboardSegue, sender:Any?) {
let colourSettings = [player1Colour, player2Colour, ballColour]
let otherSettings = [AIDiff, paddleSi, ballSi]
if (segue.identifier == "back") {
let MenuVC = segue.destination as! MenuVC
MenuVC.colourSettingsMenu = colourSettings as! [UIColor]
MenuVC.otherSettingsMenu = otherSettings as! [String]
}
}
class GameScene: SKScene {
// init colour settings array
var colourSettingsGame = [UIColor]()
// init other settings array
var otherSettingsGame = [String]()
var currentGameType = gameType?.self
}
You should not name a variable like a class name , here
override func prepare(for segue: UIStoryboardSegue, sender:Any?) {
if (segue.identifier == "settings") {
let nextSettings = segue.destination as! SettingsVC
nextSettings.colourSettings = colourSettingsMenu
nextSettings.otherSettings = otherSettingsMenu
}
}
and here
override func prepare(for segue: UIStoryboardSegue, sender:Any?) {
let colourSettings = [player1Colour, player2Colour, ballColour]
let otherSettings = [AIDiff, paddleSi, ballSi]
if (segue.identifier == "back") {
let nexrMenu = segue.destination as! MenuVC
nexrMenu.colourSettingsMenu = colourSettings as! [UIColor]
nexrMenu.otherSettingsMenu = otherSettings as! [String]
}

Swift - Delegate through Nav Controller

I'm going to include my full code in this but I will try to give pointers to where the relevant bits are. Basically I am returning to a view controller from an Unwind Segue with some new data. I am using that data successfully in the 'NBARotoHome' VC but I additionally need to pass some of that data through an embedded Nav controller to 'NBARotoTabPager' vc.
I am trying to do this using the 'UpdateChildView' delegate (at the top of the first block of code) and calling its method in 'loadViewData() (in the 'if statement' near the bottom of the first block).
protocol UpdateChildView : class {
func updateView()
func test()
var playerSelected: Player? { get set }
}
class RotoViewRoundCell: UITableViewCell{
#IBOutlet weak var categoryLabel: UILabel!
}
class RotoViewRoundHeader: UITableViewCell{
}
class NBARotoHome: UIViewController{
#IBOutlet weak var posPaidLabel: UILabel!
#IBOutlet weak var progressIndicator: UIProgressView!
#IBOutlet weak var vsLabel: UILabel!
#IBOutlet weak var fantasyFundsAmountLabel: UILabel!
#IBOutlet weak var fantasyFundsLabel: UILabel!
#IBOutlet weak var playerName: UILabel!
#IBOutlet weak var roundIndicator: UILabel!
var selectedPlayer: Player!
var firstNavController: UINavigationController!
weak var updateChildView : UpdateChildView?
override func viewDidLoad() {
super.viewDidLoad()
loadViewData()
firstNavController = self.navigationController
let rightBarButton = UIBarButtonItem(title: "Select", style: UIBarButtonItemStyle.plain, target: self, action: #selector(myRightSideBarButtonItemTapped(_:)))
self.navigationItem.rightBarButtonItem = rightBarButton
self.title = "NBA Roto"
}
func myRightSideBarButtonItemTapped(_ sender:UIBarButtonItem!){
performSegue(withIdentifier: "ShowDraft", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowDraft" {
let navVC = segue.destination as? UINavigationController
let nbaDraftList = navVC?.viewControllers.first as! NBADraftList
nbaDraftList.mainNavController = firstNavController
}
if (segue.identifier == "buyNavControllerChild"){
// let navVC = segue.destination as? UINavigationController
// let buyVC = navVC?.viewControllers.first as! NBARotoTabPager
// buyVC.delegate = self
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
#IBAction func prepareForUnwind(segue: UIStoryboardSegue) {
}
func loadViewData(){
if((selectedPlayer) != nil){
roundIndicator.text = "Upcoming: " + selectedPlayer.game_time
playerName.text = selectedPlayer.Name
vsLabel.text = selectedPlayer.visiting + " # " + selectedPlayer.home
fantasyFundsLabel.text = ""
fantasyFundsAmountLabel.text = ""
updateChildView?.test()
// updateChildView?.playerSelected = selectedPlayer
// updateChildView?.updateView()
}else{
roundIndicator.text = "Select a Player"
playerName.text = "No Player Selected"
vsLabel.text = "--"
fantasyFundsLabel.text = "Fantasy Funds"
fantasyFundsAmountLabel.text = "$10,000"
}
}
}
Because I haven't been able to get the delegate to work, I have been playing around with setting its delegate property in the above 'prepare' method -'buyVC.delegate = self' - but I'm getting 'buyVC has no member delegate' so that has been a dead end.
The next bit of code is the NBARotoTabPager vc which is embedded in the navigation controller. For reasons I'm no longer sure about I decided to make it a subclass of NBARotoHome, but its basically a custom tab pager that uses a segmented control to switch between two additional vcs.
The most important step at this point is just getting the 'test' function to work (which just prints 'test'. Its implemented in the below block of code second from the bottom above updateView).
class NBARotoTabPager: NBARotoHome, UpdateChildView{
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBOutlet weak var scoreKey: UIBarButtonItem!
#IBOutlet weak var standings: UIBarButtonItem!
var playerSelected: Player?
override func viewDidLoad() {
navigationController?.navigationBar.barTintColor = UIColor(red: 27/255, green: 27/255, blue: 27/255, alpha: 1)
scoreKey.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Helvetica", size: 13.0)!], for: UIControlState.normal)
scoreKey.tintColor = UIColor.blue
standings.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Helvetica", size: 13.0)!], for: UIControlState.normal)
standings.tintColor = UIColor.blue
setupView()
}
private func setupView() {
setupSegmentedControl()
updateView()
}
private func setupSegmentedControl() {
// Configure Segmented Control
segmentedControl.removeAllSegments()
segmentedControl.insertSegment(withTitle: "Live", at: 0, animated: false)
segmentedControl.insertSegment(withTitle: "Avg / +", at: 1, animated: false)
segmentedControl.addTarget(self, action: #selector(selectionDidChange(_:)), for: .valueChanged)
segmentedControl.selectedSegmentIndex = 0
}
func selectionDidChange(_ sender: UISegmentedControl) {
updateView()
}
private lazy var viewLiveTab: NBARotoLive = {
// Load Storyboard
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
// Instantiate View Controller
var viewController = storyboard.instantiateViewController(withIdentifier: "NBARotoLive") as! NBARotoLive
if((self.playerSelected) != nil){
viewController.selectedPlayer = self.playerSelected
}
// Add View Controller as Child View Controller
self.add(asChildViewController: viewController)
return viewController
}()
private lazy var viewAvgsTab: NBARotoAvgs = {
// Load Storyboard
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
// Instantiate View Controller
var viewController = storyboard.instantiateViewController(withIdentifier: "NBARotoAvgs") as! NBARotoAvgs
if((self.playerSelected) != nil){
viewController.selectedPlayer = self.playerSelected
}
// Add View Controller as Child View Controller
self.add(asChildViewController: viewController)
return viewController
}()
private func add(asChildViewController viewController: UIViewController) {
// Add Child View Controller
addChildViewController(viewController)
// Add Child View as Subview
view.addSubview(viewController.view)
// Configure Child View
viewController.view.frame = view.bounds
viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Notify Child View Controller
viewController.didMove(toParentViewController: self)
}
private func remove(asChildViewController viewController: UIViewController) {
// Notify Child View Controller
viewController.willMove(toParentViewController: nil)
// Remove Child View From Superview
viewController.view.removeFromSuperview()
// Notify Child View Controller
viewController.removeFromParentViewController()
}
internal func test(){
print("test")
}
internal func updateView() {
if segmentedControl.selectedSegmentIndex == 0 {
let position = viewAvgsTab.tableView.contentOffset.y;
viewLiveTab.tableView.contentOffset = CGPoint(x:0, y:position);
remove(asChildViewController: viewAvgsTab)
add(asChildViewController: viewLiveTab)
} else {
let position = viewLiveTab.tableView.contentOffset.y;
viewAvgsTab.tableView.contentOffset = CGPoint(x:0, y:position);
remove(asChildViewController: viewLiveTab)
add(asChildViewController: viewAvgsTab)
}
}
}
I've looked at a lot of examples but I don't understand the whole 'setting the delegate' thing i.e. theSecondViewController.delegate = self. Sometimes I see examples where you don't need to do this. And other times it seems like my VCs don't even have a delegate property. So I'm not sure if that's my specific problem or not but any direction would be greatly appreciated. Thanks
There are three steps to implement a delegate.
create a protocol.. (you've already done this by creating a updateChildView protocol)
you need to implement this protocol in the class you wish to receive and process this data.. (you've not done this step and thats why you cant set buyVC.delegate = self)
you need to add a property in ViewController2 called "delegate" and make it as a type of your protocol in step 1 (you've not done this step and there is no property called "delegate" in vc2 .. that's why you get this error 'buyVC has no member delegate')
Here's a quick example:
Protocol:
protocol UpdateChildView{ //removed :class
func updateView()
func test()
var playerSelected: Player? { get set }
}
Viewcontroller A:
class NBARotoHome: UIViewController, UpdateChildView { //added conformance to the protocol
//add the methods for conforming to protocol and add your implementation
func updateView() {
//add your implementation
}
func test(){
//add your implementation
}
var playerSelected: Player? {
//add your implementation
}
prepare(for: Segue) {
/** code for passing data **/
let navVC = segue.destination as? UINavigationController
let buyVC = navVC?.viewControllers.first as! NBARotoTabPager
buyVC.delegate = self
//sets the delegate in the new viewcontroller
//remember.. delegate is a property in the next vc
// and the property only accepts any viewcontroller that is implements updatechildview protocol
present(vc2)
}
}
viewcontroller2 :
class viewControllerB: UIViewController {
var delegate: UpdateChildView? //add this
viewdidload {
delegate?.test() //call the func in the previous vc
}
}

Segue is not being transferred in (swift 3)

I am making a game that when a button is pressed twice, it will transfer to another view controller. Right now the users reaction time is timed in view controller a. I would like the time to be segued to view controller b and when in view controller b the time be subtracted by 1. Right now the time is not being segued. When the button is pressed twice the view controller changes but the text is not being transferred.
import UIKit
class ViewController: UIViewController {
#IBOutlet var labelx: UILabel!
#IBOutlet var startx: UIButton!
#IBOutlet var pressSoccerBall: UIButton!
var level = 0
var timer: Timer?
var isRunning: Bool {
get {
return timer != nil
}
}
var counter = 0.0
override func viewDidLoad() {
super.viewDidLoad()
labelx.text = String(format: "%.1f", counter)
startx.isEnabled = true
}
#IBAction func startTimer(_ sender: Any) {
if isRunning {
return
}
refreshTimer()
}
#IBAction func PressSoccerBall(_ sender: Any) {
level += 1
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let DestViewController : winViewController = segue.destination as! winViewController
DestViewController.LebelText = labelx.text!
}
func refreshTimer() {
if let timer: Timer = timer {
timer.invalidate()
}
timer = Timer.scheduledTimer(timeInterval: 0.1,target: self,selector: #selector(updateTimer),userInfo: nil, repeats: true)
startx.isEnabled = false
}
func updateTimer() {
counter += 0.1
labelx.text = String(format: "%.1f", counter)
if counter < 9.9 && level == 2 {
let nextc = self.storyboard?.instantiateViewController(withIdentifier: "winViewController") as? winViewController
self.present(nextc!, animated: true, completion: nil)
}
}
}
VIEW CONTROLLER B
import UIKit
class winViewController: UIViewController {
#IBOutlet var winningLabel: UILabel!
public var LebelText: String?
override func viewDidLoad() {
super.viewDidLoad()
steve()
}
func steve(){
guard let unwrapedText = self.LebelText else {
return
}
if let myInt = Double(unwrapedText){
let myInt = myInt - 1
self.winningLabel.text = String(myInt)
} else {
return
}
}
}
You're not using segues. You are creating the winViewController and then calling present to show it. So, prepareForSegue will never be called.
Simply set the LebelText property on nextc before presenting it:
func updateTimer() {
counter += 0.1
labelx.text = String(format: "%.1f", counter)
if counter < 9.9 && level == 2 {
if let nextc = self.storyboard?.instantiateViewController(withIdentifier: "winViewController") as? winViewController {
nextc.LebelText = labelx.text
self.present(nextc, animated: true, completion: nil)
}
}
}

How can I do this with one segue?

I want to pass movies's data to another controller also pass another controller when progress is finished.Can I do this with one segue ?
class LoadingScreenViewController: UIViewController {
var movies = [Movie]()
#IBOutlet weak var progress: UIProgressView!
#IBOutlet weak var countLabel: UILabel!
override func viewDidLoad()
{
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(LoadingScreenViewController.updateProgress), userInfo: nil, repeats: true)
progress.setProgress(0, animated: true)
}
func updateProgress () {
if progress.progress != 1 {
self.progress.progress += 2 / 10
} else {
UIView.animateWithDuration(0.4, animations: { () -> Void in
})
performSegueWithIdentifier("segue", sender:self)
progress.hidden = true
self.countLabel.hidden = true
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let controller : SearchViewController = segue.destinationViewController as! SearchViewController
if segue.identifier == "segue"{
controller.model = movies
}
}
}
Do following:-
extension UIViewController {
func addSearchController() {
let searchController = UISearchController(searchResultsController: nil)
self.view.addSubview(searchController.searchBar)
}
}
Just call self.addSearchController() method in viewDidLoad method of required VC. No need to pass SearchController.

Resources