how to implement didSelectRowAtIndexPath in table view in Xcode 4.2? - objective-c

SliderDemoController *sliderDemoController=[[SliderDemoController alloc] initWithNibName:#"" bundle:nil];
[self.navigationController pushViewController:sliderDemoController animated:YES];
i implemented this method using storyboard. but this method is not working. i want to open new controller in table view cell.

If You want to access any view controllers which is in story board then you should use UIStoryBoard class.If you not sure about how to do that so, Here is the link for apple document about UIStoryBoard and use the method + (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *) storyboardBundleOrNil to access your storyboard.
Access your view controller with the method - (id)instantiateViewControllerWithIdentifier:(NSString *)identifier and use the returned view controller object.Hope this helps you....

You don't need to query the UIStoryboard object -- setting up transitions between view controllers is what storyboards do automagically. And with storyboards, you don't need to implement tableView:didSelectRowAtIndexPath: either.
If you haven't already, define a segue from the (prototype) table cell to the destination view controller (looks like that's SliderDemoController for you) by control-dragging. (Choose the Push segue type.) For functionality equivalent to the non-storyboard code you posted, that's all there is to it -- now tapping any row in the table will push in a SliderDemoController.
However, you probably want to configure that SliderDemoController based on which row was selected, right? Then, in your table view controller, implement prepareForSegue:sender:. There you can get a reference to the destination view controller (from the segue parameter) and set it up however you like.

Related

How to use protocol pattern in storyboard

I have used protocol to send data between two view controllers without using storyboard.
ViewControllerB * viewB=[[ViewControllerA alloc] initWithNibName:#"ViewControllerB" bundle:nil];
viewB.delegate=self;
[self.navigationController pushViewController:viewB animated:YES];
How to impliment this with storyboard.
How to impliment this with storyboard.
There's less need to create a protocol to communicate between two view controllers when you use a storyboard. Typically, you'll use storyboard segues to transition between view controllers. When a segue is triggered, the current view controller will get a -prepareForSegue:sender: message, which has the segue as its first parameter. The segue has references to both the "source" and "destination" view controllers, so you can get a reference to the new view controller (the destination) and pass it whatever data you like at that time.
If you do still want to make the current view controller the delegate of the destination and let them use whatever protocol you can dream up, you can set the new controller's delegate in -prepareForSegue:sender:. The rest (creating the protocol, implementing it, etc.) is exactly the same as you've always done.

How to switch views using UIStoryboard and UIViewcontroller

I'm still pretty new to Obj-C and iOS so bear with me. In my app I am trying to implement a share button that brings up a second view in the current view controller. I'm following this tutorial(http://www.youtube.com/watch?v=DZ3SyGInklQ) and I run into a problem where the dev uses initWithNibName and I am using a storyboard. I'm wondering why my second view is not coming up when I press my button. It's probably very obvious but I can't figure it out.
Heres some of my current code:
Menu.h
//Share button
- (IBAction)shareButton:(id)sender;
Menu.M
- (IBAction)shareButton:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MenuStoryboard" bundle:nil];
UIViewController *sharebuttonview = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"sharebuttonview"];
[self presentViewController:sharebuttonview animated:YES completion:NULL];
}
First you go into the storyboard and then you connect the Share Button with the View you want to show. After that you name this segue with an Identifier.
In you Menu.M in the IBAction you write:
[self performSegueWithIdentifier:#"mySegueIdentifier" sender:self];
(mySegueIdentifier is the Identifier you used before)
Based on your description of the issue it appears that you may want to do a manual segue, which allows you to open a target view controller from any number of UI controls. The workflow has changed from Xcode 5 compared to prior versions of Xcode. You may want to see my blog post for additional tips but essentially you will want to open your storyboard and do a Ctrl+Drag from one view controller in the document outline view to a second view controller.
Then you will need to name the segue that is created by clicking the Segue in the middle of the gray line connecting the two view controllers and update the segue id in the identity inspector.
In your source view controller you should be able to have any action call [self performSegueWithIdentifier:#"MySegueID"];
Additionally, you can add a prepareForSegue method to the source view controller, which allows you to initialize and pass information to the target view controller.
Apple has a sample project that you can download to see this workflow in detail (MultipeerGroupChat)
Hope this helps.

How do I Access an Already Existing View Controller

I want to move from one view controller to the next, using code. I have this:
[self.navigationController pushViewController:ViewController2 animated:YES];
This code is used in the first screen that launches. I want it to push (under a certain condition) to the NEXT view controller which is called ViewController2. ViewController2 already exists (its a storyboard project). But the current view controller doesn't know what ViewController2 is in the above code. How do I 'get' or 'access' ViewController2? It already exits, with .h and .m files, but how do I call to it!?
if you're using storyboards, you can just access it through its identifier like this:
ViewController2 *viewController2 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
Make sure to properly set its identifier to "ViewController2" or whatever name makes sense.
This isn't the way that you normally push a view controller when you're using storyboards. You should have a push segue set up in IB and the use performSegueWithIndentifier:sender: to go to the next controller. You should also implement prepareForSegue:sender: where you can get access to both the source and destination controllers. This is the method where you typically provide any data needed for the second view controller to do its job.

Reuse UIViewController instances when using storyboard

I decided to give the use of storyboards a go in my current iPhone app. I am facing a bit of a problem. I really need to reuse my UIViewController instances.
What do I mean by that? Well, for example I have a table view controller. When I tap a cell, another view controller is loaded from the storyboard and pushed onto the navigation controller stack. This all works well, but it takes about half a second to a second each time this view controller is loaded. Before I was using story boards I simply solved this problem by caching the created instance so the second time you tap a cell the view controller can be immediately shown.
By caching the created instance I mean something like this:
if (!cachedInstance) {
cachedInstance = [MyViewController new];
}
[self.navigationController pushViewController:cachedInstance];
Does anyone know how to accomplish this using the storyboard? Thanks in advance.
If you are using segues, you will need to create custom segues in order to use a cached view controller like you did before. Otherwise, the typical "push" segue will create a new instance of the view controller for segue.destinationViewController. If you write a custom UIStoryboardSegue class and use custom segues you can override initWithIdentifier:source:destination: and put your cached view controller in for the destinationViewController, and then override perform to use the classic pushViewController call.
That is how you handle the segue if you are really intent on using them. I would just skip it though, unless you really want the fancy arrows to lay everything out on your storyboard. If you skip it you can just instantiate the view controllers into the cache and then push them on just like you did before.
If your question is more about locating a view controller inside a storyboard then you can use:
UIViewController *vc = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"Some View Controller"];
Then you can save that to your cache and push it like you did in your example code.
Hope that helps.

Storyboard relationship in iOS

I'm attempting to use the new Storyboard feature, however I am confused about Storyboard Relationships? How are they different to IBOutlets?
Also how can I add my own relationship, to my own UIViewController subclass?
I have tried looking in the documentation but can't find much about them.
The way I understand it is: relationships are iOS's way of representing a 'parent-child' relationship and while that does seem quite recursive, an example of parent-child relationship is a UIViewController Container containing a UIViewController.
That's the theory anyway -- it's better understood using the UINavigationController. It is called a 'container' because it contains as many regular UIViewControllers in a 'stack' metaphor so you can do your normal UITableView drill downs and pop offs.
The key point is that the segue between UINavigationController and the first UIViewController in your stack, there is a 'relationship' while the segue between all the rest of the UIViewControllers is just a regular push segue.
The same thing is evident in the UISplitViewController -- it needs two view controllers (sometimes called content view controllers) from launch and these are connected up between the parent UISplitViewController (the container) and two regular (content) view controllers
(thus relationships are not like IBOutlets, but more like segues -- they are even in the 'segues' category of the standard view controller containers)
Now - we aren't allowed to subclass the standard view controller containers, but we are allowed to create custom view controller containers, but I can't for the life of me define a relationship in my custom view controller container!!!!!!!!!!!
so: "can I use them in my own controllers?" the answer is yet unknown (to me at least, and the documentation is thin at best)
Create a subclass of the UIStoryboardSegue like this:
#implementation JTARelationshipSegue
- (void)perform
{
return;
}
#end
Make a custom segue between your two objects and set the class as JTARelationshipSegue. In your view controller make the view controller perform the segue like this:
- (void)viewDidLoad
{
...
[self performSegueWithIdentifier:#"addChild" sender:self];
}
You need to have set the segues identifier in interface builder to addChild.
Impement prepareForSegue:sender so that it adds the segues destination view controller as a child of the current view controller, like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *destination = [segue destinationViewController];
[self addChildViewController:destination];
...
}
You will need to have a different segue identifier for each child that you want to create (or another way of identifying the specific view controller.
This will make your storyboard file look prettier, and more readable, however you may do better to just load your other view controllers and add them as children in your view did load method:
- (void)viewDidLoad
{
[self addChildViewController:
[[self storyboard]
initiateViewControllerWithIdentifier:#"myIdent"]];
...
}
I wrote a few tutorials on how to use storyboards over on my site...
Part 1 of tutorial
Part 2 of tutorial
Perhaps that will help a little bit? In essence, the Segue relationships provides an identifier for the link between two items on your storyboard. You can use these identifiers to manage how things work.