当viewController从iOS中的另一个故事板场景调用时,表视图变为空白(正在加载或重建)

时间:2023-01-22 22:30:48

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

我的应用包含社交Feed页面。当我从另一个故事板场景调用feed viewController时,feed页面表变得空白。

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

下面提到的代码是从另一个故事板场景调用feed viewController。

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页面时观察到的是正在重新创建feed页面以便重新创建表格。当我在同一故事板中切换不同的场景时,这个问题不会发生。我是iOS新手请帮助,如果有人得到任何线索。

Feed page viewDidLoad method:

Feed页面viewDidLoad方法:

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

}

2 个解决方案

#1


1  

If you are trying to set the RootViewController the use :

如果您尝试设置RootViewController使用:

Update your code in didFinishLaunchingWithOptions as

在didFinishLaunchingWithOptions中更新您的代码

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:

只需从另一个VC加载一个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

如果你试图从其他VC加载一个ActivityFeed VC,你不需要设置app的rootView控制器,你需要的是

If you are modally presenting a VC then,

如果你是模态呈现VC那么,

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

if you are pushing a VC then

如果你正在推动VC

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

让窗口:UIWindow = UIApplication.shared.keyWindow! window.rootViewController = feedPage

window.makeKeyAndVisible()

window.makeKeyAndVisible()

#2


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.

你从另一个故事板场景调用活动源假设主视图场景,所以使主视图故事板场景作为初始视图控制器从x-code x-code->单击主视图控制器 - >侧控制台/类检查器 - >打勾是初始视图控制器。

No need to add code for make root view controller

无需为make root view controller添加代码

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

然后使用segue或present视图控制器从初始视图控制器切换到它工作的任何视图控制器。

#1


1  

If you are trying to set the RootViewController the use :

如果您尝试设置RootViewController使用:

Update your code in didFinishLaunchingWithOptions as

在didFinishLaunchingWithOptions中更新您的代码

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:

只需从另一个VC加载一个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

如果你试图从其他VC加载一个ActivityFeed VC,你不需要设置app的rootView控制器,你需要的是

If you are modally presenting a VC then,

如果你是模态呈现VC那么,

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

if you are pushing a VC then

如果你正在推动VC

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

让窗口:UIWindow = UIApplication.shared.keyWindow! window.rootViewController = feedPage

window.makeKeyAndVisible()

window.makeKeyAndVisible()

#2


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.

你从另一个故事板场景调用活动源假设主视图场景,所以使主视图故事板场景作为初始视图控制器从x-code x-code->单击主视图控制器 - >侧控制台/类检查器 - >打勾是初始视图控制器。

No need to add code for make root view controller

无需为make root view controller添加代码

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

然后使用segue或present视图控制器从初始视图控制器切换到它工作的任何视图控制器。