ios, code-reuse issue, reuse viewController? - iphone

I have a view controller(A) which shows a list of items download from server.
I have another view controller(B) with segmented control.
When second index of segmentedControl is selected, the view controller(B) shows a list of items in the same format as in the controller(A).
When first index of segmentedControl is selected, the view controller(B) shows the items in different format.
I want A and B share a code for the common stuff.
I first thought ok I could move all the view-related code to "common_view class" and use the view class from both controllers.
But, it turns out that there are codes which are not related to view(such as downloading stuff from server, which is controller-part of mvc pattern).
Now I'm perplexed, "is it a good idea to share a controller-part code? is it even possible?"
I could factor out a common code into a commonViewController, and instantiate it from controller(A) and controller(B), and add commonViewController.view as a subview.
But is this really desirable? or do you suggest any better way to do this?

Yes definitely..you can.Use two NIBS in this case.The first NIB wil be used by ViewController A and first segment of ViewController B.On selection of second segment in ViewController B addSubView of viewControllerA which initsWithSecondNiB name.Once you have added them ,control their visibility by show/hide when toggling between between the two segments of viewcontrollerB

Related

"Reuse" UITableViewController in UITabBarController

So I started to work on an app, it has 5 tabs of which 3 shows UITableViewController with custom cells, but with different data.
Is it possible to link 3 tabs to same UITableViewController, but check which tab is selected, and according to that, load right data?
I tried putting in UITableViewController:
if (self.tabBarController.selectedIndex == 0)
//Load array1
else if (self.tabBarController.selectedIndex == 1)
//Load array2
else
//Load array3
But the indexes are not always the same (it depends in which order you select tabs?, and sometimes I get index that is very high number).
How would you do this?
Assuming you mean "Is it possible to link 3 tabs to same UITableViewController instance":
It is possible, but it's not a great idea. I believe (but couldn't point you at chapter and verse) that the assumption by Apple is that different tabs will have unique, different view controller instances in them. When you change tabs, the view controller instance about to hide has various 'about to be hidden' lifecycle methods called on it. Likewise, the view controller instance about to appear has various 'about to be shown' lifecycle methods shown on it. It's possible your single view controller instance may fall foul of the order of these lifecycle methods being shown.
The above point aside, it's also a waste of effort reloading a table each time just because the user changed a tab. Be kind and do things the way you're meant to do them. There's no reason you can't have a single view controller class that handles everything, but you create a different instance of it for each tab.
Note: your UIViewController being aware of which tab it is in counts as a 'code smell'. This usually means you're doing something in an undesirable fashion and hurting the re-usability of this code. That applies in this case.
To avoid this 'code smell', your view controller should be able to be given the data it is to show, without any knowledge of even being shown in a UITabBarController.
Leverage the UITabBarControllerDelegate to detect when the user switches tabs.
Upon tab switch:
assign the new array contents to a member variable used to display
the data in the table view (with a similar if/else block as you posted), and
call reloadData on your UITableView
to trigger a redraw with the data

Storyboard done, Do I need to create .h and .m View Controller file for each View created?

I have created Storyboard with several views calling each other, now I need to create the code
I notice that XCode didn't created .h and .m controller files for each View from storyboard.
Should I create them manually?
Should I keep only one controller? (or few depending of separation of concerns on MVC)
Is there a pattern for developing this?
thanks
The usual approach is one view controller pr. screen full of content. You can imagine having one view controller for a tableview, with any sort of content, and then another view controller that presents that content in a new screen full of content if a row is pressed.
Normally when you have subviews inside of your view controllers, you wire them up in interfacebuilder. Then for instance if you want to populate a view that has a uiimageview and a uiactivityindicatorview inside it, you can control their behavior and how their populated from the view controllers code. You could also if you want something very generic and you feel that one view will probably take up a lot of code in your view controller, create a uiview subclass for it, and then set the class in interface builder.
Did this help? Please let me know if you need more clarification.
It's entirely up to you whether you have a ViewController for each view. If you have many views I would recommend it. Even if you have 2 or 3 views you probably still should. Things can get really confusing when each view has a different task but all have similar IBOutlets.
TLDR; Personally, I would say it was good practice to have a ViewController for each view if each view has a separate task.

Using Storyboard, UISplitViewController and multiple detail views (MultipleDetailViews with storyboard)

All,
It's not this: Science At Hand - Adventures in UISplitViewController. That really goes from the UITabBarController rather than the cells in the Master.
Let's say I want to create a SplitViewController. On the left side I have different types of cells in the same list (It's not going on Apple Store, so I don't care if it's Apple iOk or not). For each of the different cell types I want to have a different DetailView controller. Cell Type A shows DetailView A, Cell Type B shows DetailView B.
How do I update the SplitViewController subviews to shift detailviews?
Can I just put the navigation controller under the detail and then add viewControllers to that? Using the get based on seque name or get view from storyboard?
Some other, really obvious way that I'm just missing.
For bonus points, I would like a way to detect that I'm leaving one detailview (for saving) and moving to a different detailview (A different cell type button was pressed)
It turns out it was simple.
If you do the obvious (who would have thought?) it works just fine. I created my different cellviews. Each cell view has details link that I just linked to a series of view controllers. I just had to change the seque so that it was a replace and it put the view into the detail view.
I'm make this sample in GitHub
https://github.com/AlfonsoMoreno/MultipleDetailView
MasterViewController is UITableViewController
FirstDetailViewController is UIViewController
SecondDetailViewController is UITableViewController
You can add more views!!!
Please watch MultipleDetailViewManager class!!!

reuse view from storyboard

I have a tableview with custom section headers. The view for the section header is defined in the storyboard and wired to an instance variable. Is there a way to request a new instance of the view from the storyboard?
In the past I have done this by having the section header defined in its own xib file and getting a new instance by using
[[NSBundle mainBundle] loadNibNamed:#"TimerViewSectionHeader" owner:self options:nil];
UIView *newHeaderView = self.sectionHeaderView;
I dont' think there is a way to do that. Best bet is to put the tableview custom header view in a separate nib and load it like you did in your code sample whenever you need to use it.
I tried to do the same thing and ran into the same problem.
I like to work with storyboards a lot and was impressed how fast I could create a working UI. However, as soon as you need to re-use views it makes a lot of sense to put those into a separate nib along with its UIViewController subclass.
You can then place a generic UIView in all the places where your re-used view should go and add the view using your ViewController:
[myReusableViewController loadView];
[myReusableViewController viewDidLoad]; // You have to handle view callbacks yourself.
[self.myReusableViewPlaceholder addSubview:myResusableViewController.view];
[myReusableViewController viewWillAppear:YES];
So to sum it up:
Use storyboard, it's great
Create the scaffold of your application in the storyboard, along with any static view (like About screens etc.)
Create re-used views in a custom nib + UIViewController subclass and add UIView placeholders in your storyboard.
In another answer I thought about some Pros and Cons of Storyboard
The solution I've come up with for this is as follows:
I have a tableview with multiple prototype cells that displays complex data. There is a segue to a detail view, and a transaction process view.
This first tableview has a search button that displays a new tableview with the results. It needs the same functionality as the main tableview that pushes it; including segues to the detail and transaction progress views so:
On storyboard, select and copy your main tableview. Deselect and paste. Create a push segue from your main tableview to your 2nd tableview; or from where ever you want to navigate to it from. Modify the 2nd tableview as you like. IE: If it requires some UI changes no problem.
Create a new viewcontroller class that is a subclass of the viewcontroller running the main tableview.
Override the data delegate in your subclass to serve up the subset of data you want.
Back in the storyboard, select your 2nd tableview controller and in the identity inspector select your subclass as the custom class.
For this solution to work smoothly, your app really needs to be managing data for the views. You could use prepareforsegue to pass data from 1st tableview to the second, but I've found the app data model far more flexible from numerous points of view.
Unless you have buttons that push to the sub views via segue, your subclass will need to override functions that push via segues with identities. NB Segues must have unique identifiers if you id them at all.
It took a lot of trial and error to figure this out, but once you understand the concept, it's a relatively smooth solution that is quite adaptable and not so bad to implement.
I am not sure about just views, but the way that I was able to get view controllers out of my storyboard is as follows.
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"IdentifierName"];
From here, perhaps you might be able to use this similarly to how it was once done with nibs.
I've been able to reuse a view in the storyboard just by connecting a transition from one tableview into the one I want to reuse.
so my tableview that I want to reuse is pointed to twice.
It sort of works but the problem I'm running into it setting a variable (using instantiateViewControllerWithIdentifier) in my app delegate to my table view that is getting reused.
It seems that if I reuse it, the storyboard is creating 2 instances of my tableview and the one I get with instantiateViewControllerWithIdentifier isn't the one I want.
I'm not really sure if this is the proper way to do it. But I assume many others are doing this somehow. With the custom table cells in storyboard I suspect lots of people want to reuse their views.
For example: We want to reuse the view(include subviews) in storyboard shown below.
The best solution I know so far is clip and paste the view related code to the New Singe View file without losing the information.
Detailed steps are as follows
Step 1: Rename the view we want reuse. Just prepare for step 2.
Step 2: Open storyboard as source code in order to clip the XML code we need
Step 3、4: Search and clip the code we need
Step 4.5(Not needed): Open as Interface Builder to see the view removed
Step 5、6: New XXX.xib and paste the code we clipped just now
Step 7: Important. Insert code<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/> to XXX.xib source code.
Warning: Do this before open it as Interface Builder! Otherwise, you will see wrong size and layout waring.
[![step 7][9]][9]
Step 8: New XXX.swift to connect the XXX.xib
[![step 8][10]][10]
Step 9: Add the view anywhere we want
[![step 9][11]][11]
I get warning: "You need at least 10 reputation to post more than 8 links."
Can you support me to upload the remaining 3 screenshots?

Load view with popover?

I have a main view with a UINavigationController and a subview (both added through interface builder). I have a UIBarButtonItem in the navigation toolbar. When that button is clicked, I want a popover view to come up (with a table format) with options for different subviews to choose from. When a user chooses one of the subviews (by clicking a cell), the popover should fade away and the subview should change to the user's chosen view.
Those view options should be loaded from separate xib files.
I know it's a relatively complicated question, but what is the best way to do this?
I don't necessarily need code, but that would be helpful. Thanks guys!
Check this tutorial about the popOvers:
http://mobiforge.com/designing/story/using-popoverview-ipad-app-development
Are you looking for something similar to this?
Checkout the – loadNibNamed:owner:options: of NSBundle(UINibLoadingAdditions). Your view will be the first index object of the returned array commonly.