0

My app contains social feed page. feed page table is getting blank when i am calling feed viewController from another storyboard scene.

Code is mention below which is calling feed viewController from another storyboard scene.

let feedPage: UIViewController =  UIStoryboard(name:"ActivityFeed",bundle:nil).instantiateInitialViewController()!
let window :UIWindow = UIApplication.shared.keyWindow!
window.rootViewController = feedPage

window.makeKeyAndVisible()

What i observe from calling feed page from another storyboard scene is feed page is being recreated so table as well being recreated. When i am switching through different scene in same storyboard this problem is not happening. I am new to iOS please help if anyone getting any clue.

Feed page viewDidLoad method:

override func viewDidLoad() {
    super.viewDidLoad()
    //createTable()
    print("viewDidLoad:ActivityPage")
    facebookLoginHandler = FacebookLogin(controller: self)

    screenWidth = UIScreen.main.bounds.width
    screenHeight = UIScreen.main.bounds.height
    screenScale = UIScreen.main.scale
    if screenScale == 3{
        screenScale = 2
    }else if screenScale == 1{
        screenScale = 1.23
    }else if screenScale == 2{
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad){
            screenScale = 1
        }else{
            screenScale = 2
        }
    }

    DispatchQueue.main.async {
        ActvityFeedNetwork.getActivityFeedFromServer(viewController: self)
    }
    tableView.delegate = self
    tableView.dataSource = self

    if userLoggedIn {
        statusBarNotSignedIn.removeFromSuperview()
        navigationBar.topItem!.title = "Activity Feed"
    }
    else {
        statusBarSignedIn.removeFromSuperview()
    }
    effect = visualEffectView.effect
    visualEffectView.effect = nil
    visualEffectView.alpha = 0
    visualEffectView.backgroundColor = UIColor.black

}
Mukesh Gami
  • 1,070
  • 1
  • 9
  • 15

2 Answers2

1

If you are trying to set the RootViewController the use :

Update your code in didFinishLaunchingWithOptions as

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let window = UIWindow(frame: UIScreen.main.bounds)
    let feedPage: UIViewController =  UIStoryboard(name:"ActivityFeed",bundle:nil).instantiateInitialViewController()!
    window.rootViewController = feedPage

    window.makeKeyAndVisible()

    return true
}

whats the change I did then ?

let window = UIWindow(frame: UIScreen.main.bounds)

setting the frame of window :) which u missed

Simply loading one VC from another VC:

If you are trying to load a ActivityFeed VC from some other VC u don't need to set the rootView Controller of app all u need is

If you are modally presenting a VC then,

    if let feedPage =  UIStoryboard(name:"ActivityFeed",bundle:nil).instantiateInitialViewController() {
        self.present(vc, animated: true, completion: nil)
    }

if you are pushing a VC then

if let feedPage =  UIStoryboard(name:"ActivityFeed",bundle:nil).instantiateInitialViewController() {
      self.navigationController?.pushViewController(vc, animated: true)
}

place any of the above code as per ur need and remove

let window :UIWindow = UIApplication.shared.keyWindow! window.rootViewController = feedPage

window.makeKeyAndVisible()

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • @Mukesh Gami: Please have a look at the answer – Sandeep Bhandari Sep 13 '17 at 07:39
  • where i have to add this function "func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool" – Mukesh Gami Sep 13 '17 at 07:48
  • @mukesh-gami : This is to set the rootViewController of the app rootViewController is the initial View controller that loads when u launch app in case u are trying to load a viewController from another VC then u don't need to set the rootViewController – Sandeep Bhandari Sep 13 '17 at 07:54
  • @mukesh-gami : Please consider accepting answer if it helped – Sandeep Bhandari Sep 13 '17 at 09:19
  • Thanks Sandeep. but my problem is table view in ActivityFeed is getting re-created. Because of that its being blanked for some time. – Mukesh Gami Sep 13 '17 at 09:38
  • @mukesh-gami : Thats how it is supposed to behave buddy :) Every time u call instantiate a view controller its a fresh instance all the data held by it previously will be gone. If its already pushed in Navigation stack and if u are going back by posing current VC only then it won't be re created. You cant push/present VC which is already pushed or presented. Hence it will always be a fresh instance – Sandeep Bhandari Sep 13 '17 at 10:31
  • yes Sandeep thats the point, how can i refer to old instance or is there any other way to overcome this problem. – Mukesh Gami Sep 13 '17 at 10:50
  • @mukesh-gami : It is not a problem to overcome buddy. If u have pushed a ViewController in Navigation Stack already u don't have to do anything when u pop out the viewController on top of it it will load automatically. If its somewhere down the stack n u wanna pot to that specific VC you can use this https://stackoverflow.com/questions/5753256/poptoviewcontroller to pop to specific VC u are interested in. But if its not in stack u have to create a new one and push it or present it. Thats how its supposed to be n not a bug – Sandeep Bhandari Sep 13 '17 at 11:03
0

you are calling activity feed from another storyboard scene suppose main view scene, so make that main view storyboard scene as initial view controller from x-code x-code->click on main view controller ->side console/class inspector->tick on is initial view controller.

No need to add code for make root view controller

and then use segue or present view controller for switch from initial view controller to any view controller it works.