Reuse Storyboard viewcontrollers - ios

I am rebuilding an application where I need to show a list of items. This list is retrieved using an API or retrieved from Core Data and shown in a UITableViewController. The problem I am having is that there are already seven different lists where there are small differences. Mostly of them are just different items, but also a search bar included in one list and previously stated one list will not load using an API, but from Core Data.
In my Storyboard I have added a UITableViewController with the class ItemsTableViewController which has a designed UITableViewCell. I have added an identifier to this cell so I can reuse it inside this view controller. There is a segue to this view controller from the home screen.
The idea was to create one parent object (ItemsTableViewController) and add multiple child objects (SavedItemsTableViewController, LocalItemsTableViewController, etc.) which will all use the shared logic of the parent with only some small changes (API and some custom things).
What I currently have is working, but without the child objects:
- (void)offlineButtonPressed {
[self performSegueWithIdentifier:#"openItemsTableViewController" sender:#(ItemListOffline)];
//[[self navigationController] pushViewController:[[OfflineItemsTableViewController alloc] init] animated:YES];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"openItemsTableViewController"]) {
switch((ItemList)[sender unsignedIntegerValue]) {
case ItemListOffline: { [(ItemsTableViewController *)[segue destinationViewController] retrieveOfflineDocuments]; break; }
case ItemListSearch: {
[(ItemsTableViewController *)[segue destinationViewController] retrieveDocumentsWithQuery:#""];
[(ItemsTableViewController *)[segue destinationViewController] addSearchBar];
break;
}
// Loop through all the list...
default: { NSAssert(NO, #"Unhandled type of document list."); break; }
}
}
}
So the application is calling just a function of the parent object which handles the request. But as you probably can feel, the object will be less maintainable. In comments I pushed the child view controller, but since it doesn't include the storyboard view, I need to recreate the cell from scratch, which doesn't feel good. Plus since the reuse identifier, it is not working since it is unable to dequeue a cell with the identifier set in the tableView:cellForRowAtIndexPath: method.
Now I am wondering what would be the best practice for this? It seems I can't use the save UITableViewController from the storyboard for multiple child classes, can I? But creating seven (and probably a even more in the future) controllers in the storyboard where I need to copy the cells to each controller and just give them different classes doesn't seem the way to do it. And add a method to the parent where the list would be retrieved differently and changing some stuff like adding a search bar is working, but also not the nicest way it seems.
Update: The logic in the ItemsTableViewController is pretty simple. In the delegate and datasource I handle the documents almost the same. The method that does the retrieving per list type is something like:
- (void)retrieveOfflineItems {
[self startLoading];
[[APIManager instance] getOfflineItems:^(NSArray<ItemList *> *list, NSError *error) {
[self setDocuments:list];
[[self tableView] reloadData];
}];
}
But there are more things, like the search has to add a search bar (once the view is loaded). So it needs to call multiple methods when performing the segue.

You can have a single UITableViewController, which includes all of your possible table cells, and as part of the prepareForSegue call, you should set an type identifier for the controller and the data - no matter where the data came from.
Within the UITableViewController class, you can hide / display the features that you need for this data type - such as the search bar, and in the tableView methods, choose which data source you need.
This way, the one UITableViewController class that you need to maintain is a little more complex than one dedicated class, but a lot more maintainable than 7 or more!
Assuming you have set up a variable dataType to identify the type of data you need, you could have something like this for the numberOfRowsInSection, and then similar for the other tableView methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
switch self.dataType {
case dataType.MyFirstDataType:
return myFirstDataTypeArray.count
case dataType.MySecondDataType:
return mySecondDataTypeArray.count
case dataType.TheCoreDataType:
return myCoreDataArray.count
default:
break
}
}

Create a parent UITableViewController with all subviews and cells including searchController. Initialize it and add/remove views and cells based on conditions

Related

pass data from UITableCell to UITabView

I am currently working on a project that requires a list of customers to be displayed in a UITableView, the associated cell then segues to a TabView to display a detailed customer record in a tabbed ui.
I have setup the story board with the required TableView and populated fine. The TabViews all setup and I have added a custom class to the main TabView controller which can take the ID (required to interrogate service and return further data) and Customer Name. I have also added a UIViewController for the first tab in which I need to get the ID value.
I can't seem to get hold of the ID or Company Name that is passed. I have tried importing the .h file of the UITabView. I know the UITabView .h file is being populated with the values as in the .m file I am using the Customer Name to update the title of the Navigation Bar. However, whenever I breakpoint on line that gets the ID in the .m file for the individual tab, it always returns nil.
I am using the following code to try and get this value:
companyTabController *headerData = [companyTabController alloc];
_companyName_lbl.text = headerData.companyName;
_companyID_lbl.text = headerData.ID;
I have tried several variations of the above and all to no avail.
You can also use NSUserDefaults to save the data, I think that is the simplest way to save the data throughout the app.
From the code you posted, the headerData is a new instance. So the companyName and the ID will be nil unless you assign some value to them.
Since, you mentioned that you are able update the navigation bar title, try using the same object for fetching the values in this controller as well. (Maybe you can use a singleton object)
If your segueing you have to use the prepareForSegue:sender: method as such:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
companyTabController *companyTC = [segue destinationViewController];
companyTC.companyName_lbl.text = headerData.companyName;
etc
}
if your not segueing you will have to instantiate it as such :
- (void) didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
companyTabController *companyTC = [self.storyboard instantiateViewControllerWithIdentifier:#"CopmanyTabController"];
companyTC.companyName_lbl.text = selectedCell.textLabel.text or = headerData.companyName;
[self.navigationController pushViewController:companyTC animated:YES];
}

Difference between unwind segue and delegation

I wonder if anybody can explain the difference between using a unwind segue and using delegation in the example I have below:
I have a FriendsTableViewController populated by an array of friends and another AddFriendTableViewController with a function to add a friend to the FriendsTableViewController.
This is how I send the data from my AddFriendViewController with a unwind segue:
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Check whether the Done button was tapped.
// If it wasn’t, instead of saving the friend, the method returns without doing anything else.
if (sender != self.doneButton) return;
// See whether there’s text in the text field.
if (self.nameTextField.text.length > 0) {
// If there’s text, create a new friend and give it's properties the input from the text fields.
self.friend = [[Friend alloc] initWithName:self.nameTextField.text
dateOfBirth:self.birthdayDatePicker.date
numberOfGifts:0];
}
}
This is how I add the data from the AddFriendTableViewController to the array in FriendsTableViewController with the unwind segue action:
#pragma mark - Actions
- (IBAction)unwindSegue:(UIStoryboardSegue *)segue
{
// Retrieve the source view controller, the controller that is unwinding from.
AddFriendTableViewController *soruce = [segue sourceViewController];
// Retrieve the soruce view controller’s friend object.
Friend *friend = soruce.friend;
// See whether the item exists.
// If it’s nil, either the Cancel button closed the screen or the text field had no text, so you don’t want to save the item.
if (friend != nil) {
// If it does exist, add the item to the friends array.
[self.friends addObject:friend];
// Reload the data in the table.
[self.tableView reloadData];
}
}
Now this works as I want so I hope I'm not breaking any stackoverflow rules or offending anyone, but I just wanted to know what is the difference between the way my example code is used and if the same scenario was made but with custom delegate methods for the AddFriendViewController. If some can explain it would be great!
Using an unwind segue is very similar to having a delegate, with the following advantages:
You don't need to implement any dismiss logic
You don't need to pass references up and down the navigation stack
You don't need to declare a delegate protocol
It's trivial to unwind back a number of stages in your app
The disadvantages are
Dependent on using storyboards (which may hamper reusability)
If there are a lot of them, it can lead to the same messiness as you can get in prepareForSegue with a lot of branching off identifier names
If you decide to present the view controller via some other method (not a segue) then you can't unwind back from it
The code you have looks good. I'd stick with it.

Core Data - many ways to add an object

i'm doing some testing of Core Data, let's say i have a mainViewController with a navigationBar and addButton.
Clicking on the addButton will open a detailViewController. When i press save to insert a new Object the detailVieController will close and show the table with the new data inserted.
I can think two different way to do that.
FIRST METHOD - Passing the ManagedObjectContext
In the action of the add button i create an instance of the new detailViewController and i pass the managedObjectContext to it. So will be the save button of the detailViewController that will take care of saving the context and then pop the controller.
This is the method called by the addButton in the MainViewController
-(void)addNewObject{
DetailViewController *detVC = [DetailViewController alloc]initWhit:self.managedObjectCOntext];
[self.navigationcontroller pushViewController:detVC animated:YES];
}
This method is called by the save button in the IngredientViewController
-(void)saveObject{
NSError *error;
if (![self.managedObjectContext save:&error]){
NSLog(#"Error");
}
}
SECOND METHOD - Using a delegate
In the action of addButton i create an instance of DetailViewController, i set it as delegate, so when i press the save button in the DetailViewCOntroller will call the delegate that will pass data to the main controller.
This is the method called by the addButton in the MainViewController
(void)addNewObject{
DetailViewController *detVC = [DetailViewController alloc]init];
detVC.delegate = self;
[self.navigationcontroller pushViewController:detVC animated:YES];
}
This method is called by the save button in the IngredientViewController
-(void)saveObject{
[self.delegate detailVCdidSaveObject];
}
This is the delegate implemented in the mainViewController
detailVCdidSaveObject{
NSError *error;
if (![self.managedObjectContext save:&error]){
NSLog(#"Error");
}
}
------------------------------ Passing the object
Is it best to pass raw data to the DetailViewController and create there the object or it's best to pass the instance of the object to DetailViewController that will take care of settin its data?
For Example
This way i link the object instance of the mainVC to the one DetailVC so i can easilly set its value
-(void)addObject{
DetailViewController *detailVC =[[DetailViewController alloc]init];
detailVC.delegate = self;
self.object = [NSEntityDescription insertNewObjectForEntityForName:#"Object" inManagedObjectContext:self.managedObjectContext];
detailVC.object = self.object;
[self.navigationController pushViewController:detailVC animated:YES];
}
this way i pass raw data and let the detailVC create the instance
-(void)addObject{
DetailViewController *detailVC =[[DetailViewController alloc]initWithName:#"objname"];
[self.navigationController pushViewController:detailVC animated:YES];
}
those code are just pseudocode for educational purpose. all ways works, i just want to know which do you think it's the most correct and why. thanks
I have used the first two methods and in my opinion they are both equally valid (though I personally prefer delegation). However, the third method caused problems if you give the user the option to cancel or go back in a navigation controller. If that happens, you will have an object that you never needed to create.
This sounds like a perfect use case for a NSFetchedResultsController. A NSFetchedResultsController is an object makes displaying data from core data in a UITableView a lot easier. It even tells you when the objects in core data matching a predicate change (insert, delete, update, move).
So the way I would do it is that MainViewController would have a NSFetchedResultsController that provides the data to the UITableView. When you press the add button, it would do what you have in the first method. The DetailViewController will create the new instance, set the values on it then save the managedObjectContext.
Since the MainViewController has the NSFetchedResultsController, it will automatically know that a new object have been created and it can update the UITableView to show it.
The NSFetchedResutsController documentation and the NSFetchedResutsControllerDelegate documentation show you exactly how to use it with a UITableView including code you can copy into your view controller that do the majority of the work.
The actual answer depends on your preference. In my project, I have implemented the first two methods. A definite No for the third method from my side because of same reasons as Kevin mentioned. If the user cancels the operation or some error occurs, then you will have to take care of removing the change (Perhaps write the following code in your didMoveToParentViewController method and cancel method):-
[self.managedObjectContext rollback]
Assuming of course that you do not have any other process modifying that managedObjectContext at the same time.
Now, I prefer the first two methods because :-
The first method allows me to write additional code in saveObject method. Lets say that you want to validate some properties before saving the object. These properties are only present in detailViewController. So, you cannot use a delegate in that situation without explicitly passing each and every property back to delegate function (which can get messy).
Now, assume that you are creating a object in your mainViewController and the detailViewController is only used to populate a field of the object that was created in mainViewController. In such a situation, I would use the delegate method and pass the field back to the mainViewController so that when the user saves the object in mainViewController, then the field values are saved along with it. If the user cancels mainViewController, then the field values are also not saved.

passing data to UIView before its loaded onto the navigation controller

I have a very complex situation (well for me) I am trying to resolve but thus far am having trouble with it.
I will outline the structure of the application now and then explain the problem I am having.
The names I am using are made up due to sensitivity of the data I am using.
secondToLastViewController // is a UITableView on the navigation stack
lastViewController // is just a normal UIView that i want to push onto the navigation stack
RequestClass // this class dose requests to my database and passed the data back to correct classes
getInfoClass // class is used for this specific request stores the information correctly and passes it back to secondToLastViewController
So as the user initiates didSelectRowAtIndexPath inside secondToLastViewController I make a request for the data using the RequestClass
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//..
[RequestClass Getinfo:storedInfoPram];
}
now the thread shoots off to my RequestClass, which in turn queries the DB for some data which is then received and this data is passed off to my getInfoClass the reason I have done this is because there are dozens and dozens of different calls in RequestClass all doing different things, this particular request brings back alot of data I have to sort into correct object types so have created this class to do that for me.
anyway inside getInfoClass I sort everything into their correct types etc and pass this data back to secondToLastViewController in a method called recivedData, this is also where I think things are going wrong... as I create a new instance of secondToLastViewController the thing is I dont know how to pass the data back to the same secondToLastViewController that is already on the stack and was where the original request came from.
- (void) recivedData {
// do some stuff then pass data back to secondToLastViewController
SecondToLastViewController *sec = [[SecondToLastViewController alloc] init];
[sec sendGetSeriesArrays:pram1 Pram2:pram2 Pram3:pram3 Pram4:pram4 Pram5:pram5];
}
Now going back into SecondToLastViewController the thread lands in this method
- (void)sendGetSeriesArrays:pram1 Pram2:pram2 Pram3:pram3 Pram4:pram4 Pram5:pram5{
// call detailed view onto the stack
lastViewController *last = [[lastViewController alloc] initWithNibName:#"lastViewController" bundle:nil];
[self.navigationController pushViewController:last animated:YES];
}
after the thread reaches this point nothing happens... all the data is there and ready to be sent but the new view is never pushed to the controller stack.. and I think it is due to me declaring another version of secondToLastViewController when I am inside getInfoClass
what I would like to know firstly is how do I pass the recived data in sendGetSeriesArrays to the final view and secondly how do i even load the lastview onto the navigation stack?
Your observation is correct you are creating the secondToLastViewController instance again inside the getInfoClass. Dont do like that you have to use delegate/protocol approach for passing the data back to the secondToLastViewController.
Do like this
Define a protocol in getInfo class
getInfoClass.h
#protocol GetInfoClassProtocol <NSObject>
//delegate method calling after getting data
// I dont know the argument types give it properly
- (void)sendGetSeriesArrays:pram1 Pram2:pram2 Pram3:pram3 Pram4:pram4 Pram5:pram5;
#end
// declare the delegate property
#property (assign, nonatomic)id<GetInfoClassProtocol>delegate;
getInfoClass.m
- (void) recivedData {
// do some stuff then pass data back to secondToLastViewController
if ([self.delegate respondsToSelector:#selector(sendGetSeriesArrays: param2:)])
{
[self.delegate sendGetSeriesArrays:pram1 Pram2:pram2 Pram3:pram3 Pram4:pram4 Pram5:pram5];
}
}
secondToLastViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//..
RequestClass.delegate = self;
[RequestClass Getinfo:storedInfoPram];
}
Your secondToLastViewController should conform to the GetInfoClassProtocol
There are lots of ways you can accomplish this. In your revivedData function, instead of creating a new instance, you could:
1) Maintain a pointer to the navigation controller in getInfoClass, then you can get the last view controller from the view controllers on the navigation stack and use that. This will be the active instance of the view controller. There are ways to recover this from the window object, but those seem fragile and I would not recommend that approach.
2) You can pass a pointer to self from secondToLastViewController to your RequestClass getInfo call, then hold on to that and pass it back. This is probably a pain depending on the amount of code you have already.
3) You can maintain a static instance of the class if you will never have more than one secondToLastViewController. See How do I declare class-level properties in Objective-C?

Generate new UITableViewController and show it

I'm working with tableviews showing a hierarchy data structure. I take the first node and show the sons in the tableview and repeat it until the end of the tree. I'm doing it in this way:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([[actualNode getSonAtIndex:0] sonsCount]>0) {
NSLog(#"New level");
actualNode = [actualNode getSonAtIndex:indexPath.row];
[self.tableView reloadData];
} else {
NSLog(#"Service");
[self performSegueWithIdentifier:#"Service" sender:NULL];
}
It is not the correct way but the problem is that I don't know how many levels has the tree so I can't create them in the Storyboard. The idea is create only one TableView for showing each level but doing it in that way I can't go back to the previous level on the NavBar like I could do if I was working with some controllers in the storyboard and I don't have animations. So, is there any way to do it? something like:
MyNextLevel *nextlevel = [MyNextLevel alloc];
[nextlevel setNode: actualNode]
Myactualtableview = nextlevel; (Here the next level is showed in the screen with animations and with the possibility to go back to the previous level)
Thanks.
This is why I don't like storyboards. It has always been possible to do what you want to do (if I'm understanding it correctly), but storyboards detract from it; they don't have the flexibility of doing things in code, which is what we always had to do back in iOS 3 and iOS 4 anyway.
Anyhow, you want to do something like this:
UITableViewController* tvc = [[MyTableViewController alloc] initWithNibName:#"MyNib" bundle:nil];
[self.navigationController pushViewController:tvc animated:YES];
Your business logic can just pick the class for MyTableViewController. Alternatively, MyTableViewController could be something flexible, where between those two lines you give it some configuration info that causes it to show the right data! (I have to admit, though, that you could do that same thing using a storyboard, configuring the table view controller in performSegue:.)

Resources