How to call method from a tabbar rootview controller - objective-c

I have 3 tabbar items, each a view controller. First, Second, and Third viewcontroller. I need First ViewController to call a method that updates the tableview on second view controller view. I'm not sure what's the correct way to handle this. I tried a sharedInstance, but what I think is happening is two instances are being created and that view controller that the first VM is using isn't the same VM that is actually being used in the app, which would explain why my tableview isn't updating.
Basically when I upload a file in First View Controller, I need the Second VM to update the tableview and show the file's upload progress. Kind of like when a song is purchased on iTunes. These are UINavigationViewControllers for tab items.
I tried this:
+ (SecondViewController *)sharedInstance {
// Singleton implementation
static SecondViewController* instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[SecondViewController alloc] init];
});
return instance;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UploadTableView.dataSource = self;
UploadTableView.delegate = self;
[S3UploadClientManager tm].delegate = self;
}

You don't want the controllers to communicate with each other directly. If you are segueing to another view you can use prepareForSegue. If you don't want to use that I suggest you either update a file or a database that both controllers have access to as to avoid direct interaction and keep the mvc architecture.

You could implement the tabBarController delegate method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if([viewController isMemberOfClass[SecondViewController class]]) {
//pass the data here
}
}

Related

Passing data back and forth using AppDelegate

To start I am building an app to learn the basics of Objective-C. If there is anything unclear please let me know and I will edit my question.
The app is supposed to have the next functionality.
Open the camera preview when the app is executed. On the top there is a button to go to a TemplateController where the user can select an array of frames to select from a UICollectionView. User selects the Template and returns to the Camera Preview. User takes a picture and the picture with the frame selected is shown in the PreviewController. If the user doesn't like the frame and wants to switch it for another one. PreviewController has button on top to go to the TemplateController, select the frame and go back again to the PreviewController with the new frame.
I do not want to create an object for the frame everytime. I want the AppDelegate to hold that object. To keep it alive per say?(sorry, English is not my mother tongue).
I was thinking to use NSUserDefaults BUT I really want to do it using the AppDelegate. So at this point NSUserDefaults is not an option.
Now, I am using storyboards with a navigation controller. A screenshot is available here
Right now when I pass from the TemplateController to my PreviewController my code looks like this:
Reaching TemplateController from MainController or PreviewController
- (IBAction)showFrameSelector:(id)sender
{
UIStoryboard *storyboard;
storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
TemplateController *templateController = [storyboard instantiateViewControllerWithIdentifier:#"TemplateController"];
templateController.frameDelegate = self;
[self presentViewController:templateController animated:YES completion:nil];
}
Passing the data from TemplateController to its controller's destiny (Either MainController or PreviewController)
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
_selectedLabelStr = [self.frameImages[indexPath.section] objectAtIndex:indexPath.row];
[self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
[self dismissViewControllerAnimated:YES completion:^{
if ([self.frameDelegate respondsToSelector:#selector(templateControllerLoadFrame:)])
{
[self.frameDelegate performSelector:#selector(templateControllerLoadFrame:) withObject:self];
}
}];
}
This loads the selected frame in PreviewController
- (void)templateControllerLoadFrame:(TemplateController *)sender
{
UIImage *tmp = [UIImage imageNamed:sender.selectedLabelStr];
_frameImageView.image = tmp;
}
My problem is, I don't have very clear what changes I have to do on the AppDelegate(it is untouched right now). What would be the best approach to accomplish this?
Main issue is when Tamplate is chosen before taking the still image. If I select the frame after taking the picture then it displays.
I am not certain that I understand your question. Stuffing an object into the app delegate solution may not be the best way forward. In fact I believe you ought to look at the delegation pattern that is used by Apple to communicate between view controllers. Please note that you appear to be doing half of the delegate pattern already. For example you make your PreviewController a frameDelegate of the TemplateController.
So I would think you'd have something like the following to transfer information from TemplateController back to the PreviewController. Note that I've included prepare for segue as that is a common pattern to push a data object forward (it will be called if you connect a segue from the PreviewController to the TemplateController and in your action method call performSegueWithIdentifier:#"SegueTitle"). Use of the "templateControllerDidFinish" delegation method is a common pattern used to push information back from TemplateController when it closes.
TemplateController.h
#class TemplateController;
#protocol TemplateControllerDelegate <NSObject>
-(void) templateControllerDidFinish :(TemplateController*)controller;
#end
#interface TemplateController : UIViewController
#property (nonatomic, weak) id <TemplateControllerDelegate>delegate;
...
#end
TemplateController.m
//! The internals for this method can also be called from wherever in your code you need to dismiss the TemplateController by copying the internal
-(IBAction)doneButtonAction:(id)sender
{
__weak TemplateController*weakSelf = self;
[self dismissViewControllerAnimated:YES completion:^{
[self.delegate templateControllerDidFinish:weakSelf];
}];
}
PreviewController.h
#import "TemplateController.h"
#interface PreviewController<TemplateControllerDelegate>
...
#end
PreviewController.m
#implementation
...
-(void) templateControllerDidFinish :(TemplateController*)controller
{
self.dataProperty = controller.someImportantData;
...
}
...
-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
if ( [[segue identifier]isEqualToString:#""] )
{
TemplateController *tc = [segue destinationViewController];
tc.delegate = self;
tc.data = [someDataObjectFromPreviewController];
}
}
To fix this situation a bit more:
Add a segue from the PreviewController to the TemplateController
(Ctrl-drag from Preview view controller to the Template Controller
in the document outline mode)
Name the segue identifier in the identity inspector
Change your code that presents the view controller from:
(IBAction)showFrameSelector:(id)sender
{
UIStoryboard *storyboard;
storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
TemplateController *templateController = [storyboard instantiateViewControllerWithIdentifier:#"TemplateController"];
templateController.frameDelegate = self;
[self presentViewController:templateController animated:YES completion:nil];
}
to
- (IBAction)showFrameSelector:(id)sender
{
[self performSegueWithIdentifier:#"SegueTitle"];
}
Add your data object to the target view controller as noted in prepareForSegue and you will be in good shape. Then use the delegate method to catch any data returned from your template (just add the data as properties to the controller and you should be golden)
You can see a better example of this delegation in a utility project template from Xcode (I just keyed this in..) I hope this information helps. You can get more information at these resources and also by searching Google and SO for iOS delegation :
Concepts in Objective C (Delegates and Data Sources)
Cocoa Core Competencies

UITableView isn't displaying data

AppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:#"MainSB" bundle:nil];
tCont = [myStoryboard instantiateViewControllerWithIdentifier:#"Table"];
}
- (void)serviceAdded:(NSNetService *)service moreComing:(BOOL)more {
tCont.server = _server;
[tCont addService:service moreComing:more];
}
TableController
- (void)addService:(NSNetService *)service moreComing:(BOOL)more {
[self.services addObject:service];
if(!more) {
[self.tableView reloadData];
}
}
server is correctly carried over, and service is added to services. But I don't see it in my table view. Is it because I'm trying to instantiate the view before it exists?
Update: I've done a little more debugging and found that TableController is initialized before my delegate calls for it. I've only seen one TableController thread, so my code is most likely the issue.
You need to create an UIWindow, set its rootViewController property to your table view controller and send it the makeKeyAndVisible message. You also have to implement the tableView:cellForRowAtIndexPath: method on your table view controller.

Boolean from class X doesn't persist between change from view controller of class Y to view controller of class Z

In the class GHHaiku I have the BOOL property justComposed.
In mySecondViewControllerClass I have the following methods:
- (void)viewDidLoad
{
[super viewDidLoad];
if (!self.ghhaiku)
{
self.ghhaiku = [[GHHaiku alloc] init];
}
//Lots of other code
}
-(void)saveUserHaiku
{
//Lots of code
self.ghhaiku.justComposed=YES;
NSLog(#"%d",self.ghhaiku.justComposed);
[self.tabBarController setSelectedIndex:0]; //This switches to `myFirstViewController`
}
In myFirstViewController I have the following methods:
-(void)viewDidLoad
{
[super viewDidLoad];
if (!self.ghhaiku)
{
self.ghhaiku = [[GHHaiku alloc] init];
}
}
- (void) viewWillAppear:(BOOL)animated
{
NSLog(#"%d",self.ghhaiku.justComposed);
if (self.ghhaiku.justComposedYes==YES)
{
[super viewWillAppear:animated];
self.displayHaikuTextView.text = [[self.ghhaiku.arrayOfHaiku lastObject] valueForKey:#"quote"];
}
}
The BOOL in saveUserHaiku in mySecondViewController shows 1/yes. But the Boolean in viewWillAppear in myFirstViewController shows 0/no.
What am I leaving out?
EDIT:
This is what I'm trying to accomplish in the end:
myFirstViewController instantiates GHHaiku in viewDidLoad. Then it creates arrayOfHaiku on that instantiation and loads it with x number of haiku.
The method saveUserHaiku in mySecondViewController adds a haiku to that array, sets a Boolean justComposed' toYES, and then programmatically switches view controllers back tomyFirstViewController`.
Once we're back in myFirstViewController, viewWillAppear calls a certain function if justComposed in mySecondViewController is YES.
Both view controllers were created in Interface Builder.
SECOND EDIT: In case it matters, this is a tabbed application. saveUserHaiku changes the tab.
It appears that you are creating separate instances of ghhaiku - one for each of your two view controllers. If you would like to use the same ghhaiku object between both view controllers (thereby remembering the justComposed boolean), you will need to set the ghhaiku property to the existing object when you create the view controller. Do not alloc/init a new one in viewDidLoad.
For instance, if you were to display mySecondViewControllerClass from myFirstViewController, it might look like the following:
mySecondViewControllerClass *secondViewController = [[mySecondViewControllerClass alloc] initWithNibName:nil bundle:nil];
secondViewController.ghhaiku = self.ghhaiku; // pass the ghhaiku object to the other view controller
[self presentModalViewController:secondViewController animated:YES];
Why should it have other value than NO? You have two different classes. In one of them you set value of a class member to YES. And then in another class you check value of a variable which has the same name, but it's different and you never assign anything to it, and it is NO by default

Reload ViewController by clicking on TabBarItem

I'm kinda desperate right now :/
I have a Tab Bar Controller with 4 Items. In the 4. Tab I included a webView which shows a list of pdf's. If I open a PDF in the webView there is no way to go back to the main webView with the links. Is there a way by re-clicking the 4. TabBar to reload the View? If I change from the 3. to the 4. tabbar it works (viewWillAppear).
Someone told me, that the following method should work:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if ([viewController isKindOfClass:[UIColor class]]) {
//Try this if you're pushing the webView in another ViewController
[viewController.navigationController popToRootViewControllerAnimated:YES];
//or access to your webView and call goBack();
}
}
but actually I have no idea in which file I should insert that method. (See print Screen)
Thanks a LOT in advance for your help guys!
Subclass UITabBarController
1.1. Cmd+N and create a new instance of NSObject class, and name it TabBarController
1.2. in TabBarController.h replace NSObject so that it reads #interface TabBarController : UITabBarController <UITabBarControllerDelegate>
1.3. in TabBarController.m add this:
- (id) init
{
self = [super init];
if (self)
{
self.delegate = self;
}
return self;
}
1.4. and this
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
// Is this the view controller type you are interested in?
if ([viewController isKindOfClass:[MehrViewController class]])
{
// call appropriate method on the class, e.g. updateView or reloadView
[(MehrViewController *) viewController updateView];
}
}
1.5. In IB, Inspection, change the class of Tab Bar Controller to your TabBarController (instead of UITabBarController)
1.6. You also need to include MehrViewController.h in TabBarController.m
Edit
in MehrViewController.m (as you posted in your question, assuming it has a webView)
// An example of implementing reloadView
- (void)reloadView {
[self.webView reload];
}

Correct pattern for refreshing data in UIViewController's

I am trying to refresh my view data when the application becomes the active app again. I believe the correct pattern is to have the app delegate tell it's view to reload it's data when applicationDidBecomeActive is called.
However, I am having trouble finding the UIViewController from within the Delegate:
- (void)applicationDidBecomeActive:(UIApplication *)application {
MyFancyViewController* controller = //how do I get my view controller???
[controller refreshData];
}
Also, can I count on the view controller still being allocated, or is there a chance it would go away? I'm using iOS 5 Storyboard's if that makes any difference.
Update:
I think I got it:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
UIViewController* root = _window.rootViewController;
UINavigationController* navController = (UINavigationController*)root;
OctainViewController* mycontroller = (OctainViewController*)[[navController viewControllers] objectAtIndex:0];
[mycontroller refresh:nil];
}
Yeah, this does the trick:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
UIViewController* root = _window.rootViewController;
UINavigationController* navController = (UINavigationController*)root;
OctainViewController* mycontroller = (OctainViewController*)[[navController viewControllers] objectAtIndex:0];
[mycontroller refresh:nil];
}
Why not refresh your data in the respective view controller viewWillAppear method?