Re-use tableview from different controller: how to split didSelectRowAtIndexPath behaviour? - objective-c

I'm currently working on my Favorites implementation. In the end, it should work the same as the favorites features in the Phone book on the iPhone.
I've the following set-up (besides other controllers and classes):
TabBarController (named mainTabBarController)
NavigationController with a Tableview (let's call it listNavController)
ViewController with some components for displaying row details (named detailViewController)
NavigationController with a TableView for favorite records (named favoritesNavController)
From the favoritesNavController, I want to select a row (from listNavController) so I can add a new entry to my Favorites tableview.
So, I decided to re-use my listNavController because it has all the functionality I need. Like searching, index, etc.
I've managed to show the listNavController from the mainTabBarController. So no problem here. When I select a row from the listNavController, it displays my detailViewController for that row. Of course, this was expected because that's in didSelectRowAtIndexPath in listNavController.
But, when I launch a listNavController from my favoritesNavController with the help of presentModelViewController, it still shows the detailViewController when selecting a row.
In this case, I want to return the selected row to my favoritesNavController. Then I can add it to my Favorite's list.
So, how do I differentiate this behaviour in code ? Should I use protocols, delegation, etc. ?
Any tips ?
With regards,
Rutger

It turned out that I was looking in the wrong direction.
The solution to the posted question is as follows:
I created a subclass of my listNavController and overrided the didSelectRowAtIndexPath method. Next I presented this new view controller with a navigation controller as a modal view (presentModalViewController).
Finally I set the delegate and a protocol for the subclassed view controller to the initiating class. This way I can present and dismiss the subclassed view controller from the same controller. A much more clean and MVC way to go!

Related

Xcode Unwind Segues [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 8 years ago.
I am having trouble passing data back between 2 viewControllers. They are linked with a segue and I am trying to link the data generated in the 2nd viewController and use it in the 1st viewController.
The way it works is the user starts at the 1st viewController and navigates to the 2nd viewController which contains multiple settings to choose. When they navigate back to the 1st viewController, I want the data to be passed. With prepareForSegue, it is very simple, but what should I do? Is there a method such as prepareForSegue that works in reverse (i.e going back to the main viewController)? I have tried to use unwindSegues but I am not sure if this is the appropriate method to be using. Can anyone tell me the correct way of setting this up? Or is there a different method I should use? Any help regarding what I should do would be greatly appreciated.
The best way to go about this is to use a delegate. See the articles here and here to learn about delegation. This answer and this one may also help.
You don't have to do anything like that. The unwind method looks something like that:
- (IBAction)unwindToPackerView:(UIStoryboardSegue*)sender
You have access to segue and you can have reference to your source view controller (2 view controller)
- (IBAction)unwindToPackerView:(UIStoryboardSegue*)sender {
// you can check is this right segue if you have more than one:
if ([sender.identifier isEqualToString:#"SegueIdentifier"]) {
CustomViewController *vc2 = sender.sourceViewController;
// get the data from source view controller
}
}
Note that you should add this method to the first view controller.
// Extended
If you want to call unwind segue after button (bar button, etc.) clicked just control drag from the button to the unwind icon (exit icon) in storyboard.
But if you want to run unwind segue programatically (after some action happened, like swipe gesture) you should connect your unwind segue to the view controller (File owner). Simple control drag from view controller (file owner) to the unwind icon in storyboard and choose appropriate segue.
In the code when the action (for example swipe) happened call:
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender

How to update UITableView after adding/ updating data

I am newbie for iPhone application. For storing data, I am following this tutorial.
I understand how to save data and retrieve the same.
What I am wanted to do is instead of another UITableViewController (first screen in storyboard, where we have list), I will drag a UITableView and show list of items there instead of showing in another screen.
Any idea/ suggestion how to get this done?
Any hints would be greatfull.
What I feel is, I would need to add UITableView. Add delegate and dataSource method on it and add a method where I will have all data shown in UITableView.
Edit 1
What I want is ONLY ONE SCREEN. In the link that I have provided, it is second screen.
That should work fine. But make sure you also have done a
[tableview reloadData];
after you've changed the data in your table view data source delegate.
What I am wanted to do is instead of another UITableViewController (first screen in storyboard, where we have list), I will drag a UITableView and show list of items there instead of showing in another screen.
Why you want to do this way?
UX will not be at best.
What I feel is, I would need to add UITableView. Add delegate and dataSource method on it and add a method where I will have all data shown in UITableView.
Even if you want to do, yes you need to set delegate and datasource methods.

Adding navigation controller to custom made table view

I want my table to show up only half way from the bottom of the screen, the upper half meant for an image. To do this, I added a table view through code using CGRectMake specifying the (x,y) co-ordinates I wanted it to start at.
Now, I want each row of this table to navigate to a new view. How do I make the navigation controller push my new view? I tried to add a navigation controller to my main view, but that its helping any.
Please advise on the best way to do this.
Check out the UITableView Class Reference, specifically the methods under the header Managing Selections:
The method you most likely want is – selectRowAtIndexPath:animated:scrollPosition:. You can push a view controller onto the stack from this method based on the selected row. To push onto the stack, you will need to set up the UINavigationController, then call
[self.navigationController pushViewController:viewController animated:YES];
Apple has a nice tutorial on this topic, complete with many examples.

Can a splitview be loaded inside the detail view of another splitview?

I am trying to develop an application that has screen flow similar to oracle app. I have attached the images here. Can anyone please tell how this can be achieved ?
Thanks in advance.
What you are looking for is a custom Split View Controller. The screenshots you provided are of custom split view controllers. The UIKit has UISplitViewController but this must be a fullscreen view controller.
To make a custom split view controller there's the old way, by having a main view controller and making your two master and detail controllers, adding their view to the main view controllers view.
You need to forward on calls from viewWillAppear:, viewWillDisappear: etc from the main view controller to the two controllers that you manage.
As of iOS 5, you can do something similar with view controller containment, this has a few more bells and whistles, more interesting it handles rotation animations better and all the call forwarding to the children controllers that you had to do manually in the first solution.
Check out this link for more details on custom split view controllers:
http://www.mindtreatstudios.com/how-its-made/custom-uisplitviewcontroller-ios/
To answer your question directly: if you make a custom split view controller - yes you can add this as a detail view controller. But watch out, this isn't a UISplitViewController, so just be careful not to use that term so much.
Haven't really tested this, but doesn't this solve your problem?
Create a storyboard file
Drop in a SplitViewController
Delete the DetailViewController
Drop in another SplitViewController
Link the two together using CTRL-drag and select Detail
Set the size of the detail-splitviewcontroller to Detail
????
Profit!
Anyways, not sure if it really works, but give it a try. This is IOS5 though (I think, might try it out with IB).
It'll look something like this:
If you're going to have to write your own class, you might want to first look at https://github.com/mattgemmell/MGSplitViewController for inspiration.

Putting an ADBannerView on top of a UINavigationController

I'm trying to put an iAd banner in an app that is based on a UINavigationController (it's not the standard nav-base app proposed by xcode, cause I don't need the table view).
I'd like to place an ADBanner on its bottom, to be always visible, no matter how the user pops and pushes views.
I studied the iAdSuite example in the apple sample code, but, though it's reported among the "best practices", I don't think it's the best practice for what I need. It basically declares an ADBannerView in the app delegate class and then implements the ADBannerViewDelegate methods for every single view the app needs. That means implementing the ADBannerViewDelegate methods over and over again on every view controller class you need! It doesn't seem too smart... :(
I'd rather prefer to have an approach more similar to what Apple itself does in the tab bar based app, where you have a part of the window always occupied by the tab controller and all the views switching above without affecting the tab bar view below.
You can't directly put an ADBannerView along with the nav controller in the app delegate, because ADBanner needs to be placed in a view controller (you get a runtime error otherwise).
I tried to subclass from UIViewController, implementing the ADBannerViewDelegate in this class, and place it in the rootViewController along with a UINavigationController but I'm not having good luck with this approach...
Has anybody found a good, simple way to to this? Any hint?
Thank you for any help...
You can have just one class for ADBannerViewDelegate, and just one instance of ADBanner itself. When the currently active view changes, remove ADBanner from the old view, add it as a subview to the new view.
EDIT:
to clarify, you don't need each view implement the ADBannerViewDelegate. You only should have one class implement it (that class doesn't have to be a view controller for that matter).
You would also need to maintain a somewhere a property that would point to the currently active view (e.g. you can update that property in your Navigation Controller's navigationController:didShowViewController:animated:, or come up with your own protocol for that if your views appear in a more complex way).
Then in your ADBannerViewDelegate you'd just resize the view currently pointed to by that current view property. The actual view doesn't even have to know it has an ad in it ;)