How to connect to a UITableViewController - ios

I have 2 views in my storyboard.
One is the defualt view connected to ViewController. I'll refer to it as VC.
The other view is one I created by dragging to the storyboard a UITalbeViewController. It's connected (through the custom class) to a new cocoa touch class, MyTVC. I'll call it TVC.
I connected a button on VC to TVC through a segue.
The problem is, now I have an array in ViewController, and I can't pass it to the MyTVC instance that populates the TVC view.
If I just create an instance of MyTVC in my ViewController.m, it's not connected to TVC. How could I access the MyTVC instance that's already connected to the TVC view?

In the VC class, you need to implement the prepareForSegue method. That method can obtain a pointer to TVC, which you can use to pass information from VC to TVC.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ( [segue.identifier isEqualToString:#"ShowTable"] )
{
MyTVC *tvc = segue.destinationViewController;
tvc.arrayForTable = self.arrayForTable;
}
}
Note that the segue identifier is a string that you assign to the segue in the storyboard. You don't really need to check the segue identifier if you only have one segue.

Related

ios Delegate Objective-c data sending from 2nd view controller to 1st view controller

I have 2 view controllers . First one is UIview controller and second one is table view controller.
I want to send data 2nd (table view controller) to first(uiview Controller) after the selection of rows of 2nd view controller.
For this i have written a delegate protocol.
But my delegate protocol is not working...
I figured out the problem.The object of second view controller that i am creating.
address = [[second_viewcontroller alloc] init];
address.delegate = self;
is different from self of second_viewcontroller view controller page.
How to make this two same.
self = [[second_viewcontroller alloc] init];
your problem my delegate protocol is not working... I figured out the problem.The object of second view controller that I am creating. address=[[second_viewcontroller alloc]init]; address.delegate=self; is different from self of second_viewcontroller view controller page.
It's clear say that way you create the second_viewcontroller object is not right.
You have to create the object from ViewController storyboard identifier.
First give the Storyboard ID to ViewController from Storyboard.Follow this step to Giving the Storyboard ID.
Select the particular ViewController in Storyboard.
Go to IdentityInspector.
Under IdentityInspector, There is identity section and add the Storyboard ID In "Storyboard ID" Field.
Syntax For Creating a ViewController Object.
Second_viewController *aVC = [self.storyboard instantiateViewControllerWithIdentifier:#"Second_viewController"];
aVC.delegate = self;
I assume that you are calling the Second_ViewController from storyboard instead of doing programmatically.
In that case, the correct instance of Second_ViewController can be accessed in prepareForSegue. For that, you need to set a Storyboard segue identifier, eg "Second_ViewController"
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Second_ViewController"]) {
SecondViewController *aSecVC = segue.destinationViewController;
// Register the Delegate to self.So when we call the delegate method from secondVC, SendMessage will be call of ViewController
aSecVC.delegate = self;
}
}
If you use alloc-init or instantiateViewControllerWithIdentifier, when you are using a storyboard push segue, it will create another instance.
Yes as your instantiating a new instance of the second view controller. From what I could make out from your question, I guess if you obtain the instance of your secondViewController from the Navigation Stack it should work
I created a sample project for you to get the basic knowledge of how to pass data backward using NSUserDefaults. try this in GitHub. hope this will help to you. project url Pass data backward using NSUserDefaults in Objective-C

Perform Segue from Root View Controller of a UIPageViewController

I have a UIPageViewController and have a button in it. Whenever the button is pressed I want to perform a Segue from the parent view controller (which has a navigation controller embedded) to the next view controller in the navigation stack. I also want to be able to pass data through the segue. I've tried a couple of things but I'm very new to iOS development and have not been successful at all.
You need to select the segue in your storyboard and give it a unique identifier and the put that in the code below.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"YourSegueIdentifier"]) {
// get a reference to the destination View Controller
UIViewController *destinationVC = [segue destinationViewController];
XXYourViewControllerSubclass *yourVC = (XXYourViewControllerSubclass*)destinationVC;
// create the data and pass it to the view controller
id someData = // create your data (unless you have a property holding it already)
[yourVC acceptData:(id)someData]; // make a public method on your VC subclass to accept the data
}
// after this method has returned the seque will be performed
}
Does that make sense?

Can't access parentviewcontroller's properties

Here's a quick and dirty question:
I have a "main" view controller (VC) which is opened up from a parent VC, that uses a navigation controller. Then I have a "sub" VC that is opened (modal segue) from the "main" VC.
I have set a property in the main VC's interface:
#property (nonatomic) int myVar;
Then set it from the button's action that is touched to display the "sub"VC from the "main"VC's interface:
self.myVar=1;
I imported the mainVC.h in the subVC.h
Then at the viewDidLoad method of the subVC, I'm trying to access myVar's value, but can't do that with:
NSLog(#"Myvar is %i", ((mainVC*)self.parentViewController).myVar);
Which returns the value as 0.
And when I try presentingViewController method instead, I get an error (which did not cause the error when I pushed the segue instead of making it a modal:
[MainVC myVar]: unrecognized selector sent to instance
I'm trying to code for iOS 5, and needless to say that I'm still a noob.
What am I missing?
Thanks!
The parentViewController of the "sub" view controller is not the mainVC, it's the navigation controller. The mainVC is not accessible - for all you know, it may be deallocated to save memory.
If you need to pass data from the main controller to the sub controller on the segue, add an instance variable to the "sub" view controller, and set it in the prepareForSegue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"open_sub"]) {
subVC *sub = segue.destinationViewController;
sub.myVar = 1;
}
}

How can I pass value between NavigationController and ViewController with StoryBoard?

I have a problem,
The following is my StoryBoard,
the first Controller is a TabBarController,
and it relation with A (ViewController).
A is a ViewController,
B is a NavigationController, A change page to B by modal segue
C is a ViewController, C will change to another page by push so I need a NavigationController
OK, I want to pass value from A to C,
now I can pass value from A to B by prepareForSegue,
However, because B and C have relationship but not segue,
So I can't pass value from B to C by prepareForSegue!!!
How can I pass value between NavigationController and ViewController with StoryBoard?
The Storyboard image is a little misleading here.
When you segue to B, actually you are segueing to the B/C combo as NavControllers always have at least one viewController in their stack (which is their topViewController and their [viewControllers objectAtIndex:0]).
So you do have a relationship directly from A to C.
How you access that controller depends on whether your segue is modal or push. In your case it is modal, but I will describe both so you can see the difference.
In either case, to pass data to C, you need to declare a property in it's header file
#interface CviewController: UIViewContrller
#property (assign) int dataFromA;
#end
push segue
In a push segue, it is actually C that is the destinationViewController, not B. In fact the push segue is governed by B, which is the UINavigationController for both A and C. The code behind the push segue is of the form
[self.navigationController pushViewController:otherViewController];
In AviewController's prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
CviewController* controller = segue.destinationViewController;
[controller setDataFromA:self.data];
}
It is possible in the storyboard to make a push segue line between two viewControllers that do not share a common UINavigationController. However when you run this you will get a crash error:
'Could not find a navigation controller for segue 'pushC'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
Behind every good push segue lies a Navigation Controller.
modal segue
The code hiding behind a modal Segue is the UIViewController method
- (void)presentViewController:(UIViewController *)viewControllerToPresent
In a modal segue to a NavController/ViewController combo, the destination viewController is whatever the segue line points to. If it points to a viewController, that is the segue.destinationController (and the UINavigationController will be ignored, which is not what you want here); if it points to a UINavigationController, as in this case, that will be it's destinationController. But it is still straightforward to access the viewController, as it will be the navigation Controller's topViewController.
In AviewController's prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
CviewController* controller =
(CviewController*)[[segue destinationViewController] topViewController];
[controller setDataFromA:self.data];
}
Note that in this case we have to use old-style [[message passing] syntax]. If we use modern.property.syntax we get a compile error. That's because the program does not know the type of desinationViewController, and refuses to accept topViewController as a property of an unknown type. But it is happy to [send [real messages]] to an unknown type. We also have to (typecast*) to avoid compiler warnings.

How to prevent Storyboard Segue from resetting UIViewController inside UIPopoverController

Here's my problem. With Storyboard's segue I am having a popover segue for a bar button. The UIViewController inside the UIPopoverController requires to load the data from a server. With Storyboards, everytime I close the popover the view is released so whenever the popover appears again it tries to load data again from server. I dont want this behavior. How can I prevent Storyboard from resetting the view controller inside popover controller? Something like what UITabBarController does. UITabBarController calls viewDidLoad for the first time and for the subsequent tab switches viewWillAppear is called.
Segue is designed so. Every time you do segue - view will be loaded.
If you need to store it's data - you should store it outside popover and use this method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([[segue identifier] isEqualToString:#"your segue identifier"])
{
//get popover
ViewController *vc = [segue destinationViewController];
//Set popover data to vc here
}
which calls before segue, and in this method set data to popover.
If you will use it - don't forget to set Segue identifier in Interface Builder.

Resources