iOS: How to dynamically change DetailView in SplitView template? - objective-c

I've got a SplitView template project.
I want to change DetailView on user selecting item in RootViewController.
In fact I can't just change what's inside that view (DetailView) like it's done in the template (when you check "use core data storage" on creating project).
I want to switch between whole views.
I've tried that:
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.detailViewController setView:curretnDetailViewController.view];
appDelegate.detailViewController = currentDetailViewController;
By the way I don't understand why do I need line#2 (..setView:..)? Why assigning detailViewController (line#3) is not enough? - Code looks awful=(
But anyway that doesn't work exactly as I want. Everything except detailView disappears. Like I've changed the main view, not just DetailViewController's
I guess I should change something in splitViewController but didn't find out what=(
Thanks for your attention.

I too had your questions about a week ago and I too got little help in this forum. I hope I can change that for you.
First did you know apple has a Multi Detail View example? It is here Apple Examples
You have to understand what a splitviewcontroller is to get the idea of what Apple is doing. Think of the splitview controller as a container within your device window. This container has 2 compartments, a thin left hand side a bigger right hand side. Now to change over either side you have to replace this compartment with another compartment of similar size and nature. You cannot put in there a compartment that does not fit or that does something fancy cause it will simply not fit.
In technical talk find this below code in the example. The splitview controller has an array of 2 views and you can change the views in this array. Thus in this array is 2 views the left is called navigationcontroller and the right is detailviewcontroller. If you alloc and initiate a view and add it to either of these two then this view will replace the current view in the splitviewcontroller.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;
Now to your second part to your question - which I think is from the left hand side you want to be able to always be able to choose something and be able to change the right hand side....am I right?
I have read three different ways of doing this and not seen anyone who says one is better than the other.
set up a notification - whereby when you click on something on the left a noticiation is sent to the right hand side.
when you start with the bottom or root view of the left hand side you will have a reference to the right hand side, if you add subviews etc to the left hand side you always pass along a reference to the right hand side.
The way I did it was to set up a protocol in the appdelegate or in the rootview delegate, then on a view that I want to talk to the right hand side it uses this protocol and my right hand side implements this protocol. This last one I learnt as it is a preferred method of getting modal views to talk to other views.
I am not an expert so I have not added code for each of the 3 choices above, what I suggest is that you do a little bit of googling for the topics and you will see examples. If you get really stuck on one let me know and I will try to find again with google the examples that I have found in the past. The least complicated I think is No.1.
I hope this get you going in the right direction.

Related

Manipulate UI from AppDelegate

I am a newbie in all this as will be apparent really soon.
I am using the iOS: Application: Tabbed application template. I have placed a UIImageView in the first view and two standard rounded buttons. One button is attached to an action in the FirstView Controller which places a picture into the UIImageView. The second button is attached to an action in the AppDelegate which calls a method in the FirstViewController which in turn places a second picture into the UIImageView.
The AppDelegate method does not replace the picture. It doesn't crash… it just does not seem to do anything.
How can I manipulate the view in the First and Second View Controller from the AppDelegate?
#dasdom
Well that's one issue explained. I've been reading the theory of MVC and trying to put it into practice now. Short version is I am trying to write a Battleship app for practice. Was planning on using the first screen to setup the game pieces, prefs, etc.. and use the second screen for actual game play.
I've created another class to use as my "brain center" but I ran into the same issue of not being able to manipulate anything on the screen for the first or second views. (That's why I tried the appDelegate).
That's my life story right now… can you throw some pointers my way on how to proceed and how to solve my one of many problems?
First you shouldn't do that. The AppDelegate should only be responsible for bringing the first view onto the screen.
Second you should have a look into the Model-View-Controller design pattern. Search for it in your preferred search machine.
But I you really still want to do that you should have a look into delegation and/or notifications. For example you could send the First View Controller a notification from the AppDelegate to change the image.

UISplitViewController Master / Detail communication

I just started playing with the UISplitViewController - I've cobbled together some code from various tutorials, but I'm having trouble seeing how to send data from the Master to the Detail. I'm creating an RSS reader just to illustrate to myself how it should work. I've parsed an RSS feed and populated the MasterViewController with a UITableView, but I'm stuck figuring out how to take a row click and load the corresponding article in a UIWebView in the detailViewController. Any tips are appreciated.
A good approach is to use delegates. That allows one view to call a callback provide by the other. In this case the detail view relies on the master existing so having it callback is fine. I would avoid letting them have direct references to each other and reading each others data directly.
What exactly does delegate do in xcode ios project?
Here's a tutorial with UISplitViewController that does just that (delegate between master/detail):
http://www.raywenderlich.com/1040/ipad-for-iphone-developers-101-uisplitview-tutorial
Specifically this section:
Hooking Up The Left With the Right
Time to play matchmaker and hook
these two sides together.
There are many different strategies for how
to best accomplish this. In the Split View Application template they
give the left view controller a pointer to the right view controller,
and the left view controller sets a property on the right view
controller when a row gets selected. The right view controller
overrides the property to update the view when the property is
updated. That works fine, but we’re going to follow the approach
suggested in the UISplitViewController class reference here – use
delegates. The basic idea is we’re going to define a protocol with a
single method – “selectedBotChanged.” Our right hand side will
implement this method, and our left hand side will accept a delegate
of somebody who wants to know about this.
Another approach would be to have a shared model - sort of like a singleton with notifications to trigger different views to update themselves based on either the data from the notification or querying the model in reaction to a model changes. This is sometimes better in an app with many views that don't rely on each other and just bubble up data in various ways (which is not the case here - the detail view relies on the master existing so a delegate is fine).

How can I do an animated switch to UISplitView?

I have looked at the sample generated by xcode when creating a new UISplitView app on the iPad along with countless other tutorials and the documentation from the apple developer site. I have not seen an example where the UISplitView used was not the root of the application. Is this even possible?
What I am trying to accomplish: I have a UITableView to start out and once an item in the list is selected I would like to display a splitview with two different sets of information that is based on the item that was selected.
I curious if this type of implementation is even possible, or just frowned upon, and why. If it is possible, how would I go about implementing and hooking up a UISplitView to behave in this way?
Edit: I'm updating this with what I have. I can now switch to my UISplitView, though the transition is not animated. What is the way to correctly switch to a UISplitView so the transition is animated?
Code for switching right now:
[appDelegate.window addSubview:appDelegate.splitViewController.view];
appDelegate.window.rootViewController = appDelegate.splitViewController;
EDIT 2: In hopes of bumping this back up so more people see it, I have managed to switch from my navigationController to my splitViewController, but when I add the button to be able to navigate back, nothing I do makes a difference and I seem to be locked in. I tried reverse mirroring the code to switch to the splitViewController, but that had no affect, and I am completely out of ideas. Can anyone shed some light on this?
You should always use SplitViewController as a rootViewController: Split view controller must be root view controller
There may be some hacks around it, but when Apple have a strong recommendation and design guidance, I suggest to try to re-think your design before going against the platform -it should save you effort in the long term.
I recommend using the MGSplitViewController, it also works as a non-rootViewController, even nested into an another MGSplitViewController, and there's i.e. a one-liner for the animation to blend in the Master-View, if that is what you want.
In your UITableView didSelectRowAtIndexPath method you would have something like:
UISplitViewController *mySplitView = [[UISplitViewController alloc] init];
[self.navigationController pushViewController:mySplitView animated:YES];
[mySplitView release];
Probably you'll want to subclass UISplitViewController just like you would other view controllers and set in there the master and detail views and so on.

Can I use UITabBarController as a simple viewController switcher?

I'm creating an iPad app based on a UINavigationController (with the bar hidden) so I can push and pop other viewControllers for navigation around the app. However, I am now wanting to add a section in which there are two viewControllers that I want to be able to switch between, so in other words they are side-by-side, rather than hierarchical.
Is it okay to use a UITabBarController for this? I am aware that on the iPhone it is recommended they are used only at the root level of the app, but since this is an iPad app I wondered if I could use it? Also, I guess I need to create an empty viewController, create a UITabBarController within it and set the delegate to it, then add the two viewControllers to it... So in effect I will have a viewController within another viewController, and when I have done that in the past the results have been very flaky.
Can I do it this way? The only other way I can think of doing it is to have two plan UIViews within a UIViewController, but that also means I shouldn't really put any business logic in them (bad MVC!), and not being able to will be a right pain in the a**.
Any ideas?
Thanks!
:-Joe
EDIT: I also need to be able to swipe-animate between the two VCs within the TabBarController, AND have a menubar over the top which doesn't animate... Can I do this?
Sure.
I do this kind of thing all over the place in an app I'm working on. I actually have several different types of "toolbars" that can be optionally shown at different times.
What I do is create a "parent" member in my toolbar's class - and when a button is pressed, I have the toolbar call a method in the parent class to do whatever needs to be done - (i.e. display another view).
This avoids the whole mess of creating a view inside another view (or viewcontroller inside another viewcontroller - or whatever) - the toolbar can take the button hits, but all the views are opened by the root view/controller.
Hope this helps/makes sense!

Passing Object Between View Controllers in Objective C

I have a very simple iPhone view based application I need help on. It's a view based application with a nav. bar in the footer that switches between 4 view controllers.
What I need to do is pass a UILabel value from view 2 to view 4. The UILabel field is a value calculated in view 2, but I want it to appear in view 4 (if 5+5=10, I want the 10 to appear in view controller 4, not view controller 2).
How do I go about doing this? Does anyone have any sample code I can review? I've searched awhile in Apple's docs and online and haven't found anything helpful yet. Keep in mind I'm a real newb. when it comes to development. I'm just starting to learn!
Thanks in advance.
There are a few ways. I would probably just have a variable created in the application delegate's interface and just change it and access it there.
NSObject *myVariableFromDelegate = [[[UIApplication sharedApplication] delegate] myVariable];
[[[UIApplication sharedApplication] delegate] setMyVariable:10];
Take a look at NSNotificationCenter. You can send a messages and handle them anywhere in the app, best solution in most similar cases.
The application delegate might work for you, but if you are going to have a lot of values to keep track of, you are probably better off implementing a separate class for your data. Otherwise it will get unwieldy quick.
When you load a new view, you pass off a pointer to the data cass to that view controller so that the current values can be pulled out and put into the correct fields. In addition, you could use register for notifications in the view controller to catch any changes that other views make. Just make sure you de-register the notification when the view unloads, and you should be good.