Apologies
You must be signed in to watch this lesson.
iOS Create Posts and Refresh UI
Fullstack Social iOS NodeJS REST
Now that we have access to a UITabBarController menu system, let's go ahead and implement the feature that creates a post object using the plus button at the bottom of the screen. I'll show you how to configure the button to only activate for the middle tab bar button.

Comments (4)
mreaybeaton
4 years ago
Hi Brian, It looks like when we create homeController it creates a new instance. Below I am printing self.table and posts.count in the HomeController. When the app initially runs, it works as expected, all posts are loaded, but once I run the homeController.fetchPosts, it creates a new instance of the tableview, runs numberOfRowsInSection, but doesn't execute cellForRowAtIndex, even though the data has pulled down the extra post. What I can't work out is why if we have created a new instance, cellForRowAtIndex doesn't get called but numberOfRowsInSection does. Optional(<UITableView: 0x7fb64c02e600; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001fc3810>; layer = <CALayer: 0x600001190220>; contentOffset: {0, -88}; contentSize: {0, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x600001fb1320>>) 0 Optional(<UITableView: 0x7fb64c02e600; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001fc3810>; layer = <CALayer: 0x600001190220>; contentOffset: {0, -88}; contentSize: {0, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x600001fb1320>>) 0 Optional(<UITableView: 0x7fb64c02e600; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001fc3810>; layer = <CALayer: 0x600001190220>; contentOffset: {0, -88}; contentSize: {375, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x600001fb1320>>) 0 Optional(<UITableView: 0x7fb64c02e600; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001fc3810>; layer = <CALayer: 0x600001190220>; contentOffset: {0, -88}; contentSize: {375, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x600001fb1320>>) 40 Successfully created post, here is the response: refreshing posts Optional(<UITableView: 0x7fb64d035e00; frame = (0 44; 375 768); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001f63420>; layer = <CALayer: 0x6000011126c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <_UIFilteredDataSource: 0x600001fb1fb0>>) 41 Optional(<UITableView: 0x7fb64d035e00; frame = (0 44; 375 768); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001f63420>; layer = <CALayer: 0x6000011126c0>; contentOffset: {0, 0}; contentSize: {375, 1804}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <_UIFilteredDataSource: 0x600001fb1fb0>>) 41
Brian Voong
4 years ago
mreaybeaton
4 years ago
Thanks Brian, so I run your version downloaded from here, and break point on viewDidLoad and your stack trace is: run application: 0 HomeController.viewDidLoad() 21 UIApplication 22 Main 23 Start 24 Start print self.table from numberOfRowsInSection Optional(<UITableView: 0x7fce3500ea00; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001c36010>; layer = <CALayer: 0x6000012334e0>; contentOffset: {0, -88}; contentSize: {0, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x600001c6d620>>) Optional(<UITableView: 0x7fce3500ea00; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001c36010>; layer = <CALayer: 0x6000012334e0>; contentOffset: {0, -88}; contentSize: {0, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x600001c6d620>>) Optional(<UITableView: 0x7fce3500ea00; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001c36010>; layer = <CALayer: 0x6000012334e0>; contentOffset: {0, -88}; contentSize: {375, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x600001c6d620>>) Optional(<UITableView: 0x7fce3500ea00; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001c36010>; layer = <CALayer: 0x6000012334e0>; contentOffset: {0, -88}; contentSize: {375, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x600001c6d620>>) Now if I create a post, viewDidLoad doesn't fire, but the post updates and the output is: Optional(<UITableView: 0x7fce3500ea00; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001c36010>; layer = <CALayer: 0x6000012334e0>; contentOffset: {0, -88}; contentSize: {375, 9972.6666666666642}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x600001c6d620>>) So same pointer address, makes sense. Run application: stack trace is the same, with the output: Optional(<UITableView: 0x7f8200812c00; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001414270>; layer = <CALayer: 0x600001aae8e0>; contentOffset: {0, -88}; contentSize: {0, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x6000014961f0>>) Optional(<UITableView: 0x7f8200812c00; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001414270>; layer = <CALayer: 0x600001aae8e0>; contentOffset: {0, -88}; contentSize: {0, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x6000014961f0>>) Optional(<UITableView: 0x7f8200812c00; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001414270>; layer = <CALayer: 0x600001aae8e0>; contentOffset: {0, -88}; contentSize: {375, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x6000014961f0>>) Optional(<UITableView: 0x7f8200812c00; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001414270>; layer = <CALayer: 0x600001aae8e0>; contentOffset: {0, -88}; contentSize: {375, 0}; adjustedContentInset: {88, 0, 83, 0}; dataSource: <_UIFilteredDataSource: 0x6000014961f0>>) now when I create a post, my viewDidLoad IS fired, with a stack trace of: 0 HomeController.viewDidLoad() 5 [UITableViewController TableView] 6 closure #1 in HomeController.fetchPosts() 7 closure #1 in Service.fetchPosts(completion:) 8 closure #1 in closure #1 in DataRequest.response<A>(queue:responseSerializer:completionHandler:) 17 UIApplicationMain 18 main 19 start Enqued from (Thread 19) 0 dispatch_async 1 OS_dispatch_queue.async(group:qos:flags:execute:) () 2 closure #1 in DataRequest.response<A>(queue:responseSerializer:completionHandler:) Any ideas on that stack trace, where it's coming from? Thanks
Brian Voong
4 years ago
mreaybeaton
4 years ago
Ahhh, thank you, that is indeed the issue! I missed the step at 14:32 in your video where you replace HomeConroller() with homeController createNavController(viewController: homeController, tabBarImage: #imageLiteral(resourceName: "home")) createNavController(viewController: HomeController(), tabBarImage: #imageLiteral(resourceName: "home")) So it was going off spawning a new version, doh! Thank you for replying, I can continue on now. Good tip with the stack trace, I've never used that before.
gerkov77
3 years ago
Hey Brian, amazing stuff, I am definitely enjoying this course a lot! One general question: -Do you recommend going back and forth between the iOS project and the web app project, when developing a full stack project for production? Or it is just for the course, to keep it interesting all the time, and basically you finish the web project first before start with the iOS app? Or maybe it is just question of personal preference?n What are the best practices? Thanks!
Brian Voong
3 years ago
gerkov77
3 years ago
Thank you! Greg
Lazaro Ambrosio
3 years ago
I got a warning stating 'keyWindow' was deprecated in iOS 13.0: Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes. Would this method still work?
BrianVoongLBTA
3 years ago
Christopher J. Roura
3 years ago
Hey Brian, unfortunately this code below is no longer executing and i am only receiving the print line of error. I have tried google searching this issue but haven't been able to find a solution. If you have any ideas or can point me in the right direction that would be great. thank you fileprivate func refreshApplicationWithPosts() { guard let mainTabBarController = (UIApplication.shared.keyWindow?.rootViewController) as? MainTabBarController else { print("ERROR") return } mainTabBarController.refreshPosts() }
BrianVoongLBTA
3 years ago
Brian Voong
3 years ago
Christopher J. Roura
3 years ago
SwiftUI App. I have followed your other tutorials and have set the view controllers with uiviewcontrollerrepresentable.
Brian Voong
3 years ago
HELP & SUPPORT