Two way relation for UIViewController - iphone

I have a container view controller say ContainerViewController and another UIViewController(PhotoViewcController) which i am adding as a childviewcontroller in ContainerViewController. So that message is forwarded from ContainerViewController to PhotoViewcController. But how can i use the two way relation in the parent child view controllers? so that message is passed form PhotoViewcController to ContainerViewController.

make a property in child class like this
#property (assign) id pDelegate;
and synthesize it, and when you go to child view controller, set the delegate as self before push/present as
childController.pDelegate = self;
now when you want to send the message, do this
if([self.pDelegate respndsToSelector:#selector(popedBackFromChild:)])
{
[self.pDelegate popedBackFromChild:<some object>];
}
tell me if you need further explanation

There are two ways that you can do this. Either define a protocol for it in the child class and define it in the parent class or you can post and notification and pass the necessary information as a dictionary. As #The Saad has told you, this is the best way you can define a protocol but notifications are easier to manage.
P.S. Parent class is the class where you will update the information and Child class will be providing it.

Related

What would be the appropriate way to use a delegate protocol from 2 views?

I have a view controller, which uses a delegate protocol in order to pass back an array of strings. I now have another view controller, which I'd like to use the same protocol, but I've I use it I get a warning in Xcode Duplicate protocol definition of 'SearchDetailsDelegate' is ignored.
I need these two views to pass back an array for the parent view controller to parse. What would be a more appropriate way of achieve what I need to do here? Would key value observing be the way to go here?
You have few options:
rename your protocols to be different.
create an external protocol and adopt that protocol on each view
Add a property to your view called ParentController with a type of it's parent.
#property (strong,nonatomic) ParentViewController *ParentController;
(synthesise that off course)
Then, in your viewController, when you instantiate the view assign the viewController as the parent
YourView *childView = [[YourView alloc]init];
childView.parentController = self;
Now you can add a method in your viewController that can receive the strings array
-(void)setStringsArray:(NSArray*)arr{
//do what ever you need with the array
//don't forget to add this method to your .h file so it will be visible
}
Lastly send the strings array from the view:
[self.parentController setStringsArray:yourArray];
BTW
if you want to know what view send the array you can:
-(void)setStringsArray:(NSArray*)arr fromView:(UIView*)senderView{
//do what ever you need with the array
//don't forget to add this method to your .h file so it will be visible
}
and use
[self.parentController setStringsArray:yourArray fromView:self];
BTW 2
an other option will be to use notifications.
Define the protocol in a separate .h file (new file of objective c protocol) and then include it in the required view controllers.Redefining the same protocol in two different view controllers is not recommended as it has been in your case

How to pass change the value of a variable in another view(objective c)?

i have two controller
in view1.h i have
NSString *bg;
#interface Lottery5ViewController : UIViewController {...}
in view2.m i have
-(IBAction) switchView2{
self.bg = #"blueButton.png";
}
but it cannot compile and said
request for member 'bg' in something not a structure or union
i just want to modify the value of a variable in view1 from view2
Your problem is that in view2's switchView2 method you are calling self and trying to change an ivar (or property, not sure which it is from the code shown) of a different class. If you want to send something from one instance to another you have to establish some sort of connection between them first. There are a few ways you can get them talking. The better ways to be able to pass something like this is to set up a delegate protocol or use notifications, then view2 could call the delegate method that makes the change in view1 or post a notification that view1 would be registered for so that it could make the change. The less desirable way to accomplish this would be to give view2 an ivar (or property) that is view1. You would then be able to use something like view1.bg = #"blueButton.png";.

How to get super class delegate and update the variable in super class from subclass

#interface first{
nsstring one;
second secondobject;
}
#interface second{
nsstring two;
}
in the above classes from second class I want to update the first class "one" string.
I knew that we can update the "two" string from the first class
but I want to update the string "one" from second class
should not use appdelegate
should not use inheritance
*I want to know like our AppDelegate has the [[uiapplication sharedapplication]delegate]
by getting above delegate of appdelegate we can access properties of appDelegate class
Like this how can we can get the delegate for first class and access the properties of first class from the second class.
if any pictorial tutorial for tree structure please specify the link
here is my structure
It seems that you don't really understand the meaning of delegates and what they are used for.
In your example class first aggregates (contains) instance of class second. It means that the reference to that class is an ivar. You can access all public members, properties and methods of that instance from class first.
If you want to access the ivars of the superclass then you can do that like they were declared in the child class.
Delegates are used when you need to notify another object about something during execution. Objects usually have weak references (they don't retain) to delegates.
So i think the best choice for you would be to read some good book about object oriented programming. This is really good book about that
You can pass a pointer from the first class to the second, and the second can use this to call methods or access data members in the first class.
Or if first will be a singleton class (like UIApplication), you can set up a class method (like sharedApplication) that second can call to get a reference to the first object.
Generally, structuring iPhone apps using the MVC pattern helps to reduce these kinds of tangles.

iphone - delegates

I have an object that belongs to a class. Lets call it classA. This classA is a subclass of classB.
ClassA has a delegate protocol.
I create an classA object on my main code. This object is inside a view. Lets call it viewX.
Now I am in classB and I would like to get a reference to viewX.
remember that classA has a delegate protocol, so it has a reference to its delegate, that is the viewController where viewX is. From class A I can access viewX doing [delegate view], but how do I do that from classB???
thanks.
If you have access to class B, I would say you should add a variable to the class of type id, and set that variable as the view, and that would be a very easy way to do it. Otherwise, I don't think it's possible. But I may be wrong.

Adding item to a subview from a external class

I've created a UIView and can add things to it by using [self.topView addSubview:image]; Now I am importing a class to create a calendar which has a heap of buttons. I could put it in the same class and say [self.topView addSubview:button] but if its in another class how do I add it to the subview of the class that owns it? Hope that makes sense...
You need a reference in your external class to the class that owns the view (call it the "owner class"), and presumably write a method in your owner class to add a passed-in view to your chosen subview. Something along the lines of:
- (void) insertSubview:(UIView*)newView {
if (newView) [self.topView addSubview:newView];
}
Setting up the reference can be done a number of ways, so I'll leave that one to you.