Disclaimer: The program I wrote works. I just need help understanding it.
Today I just started learning XCode and Objective-C. I have tons of experience with Java and I must admit this is very different. I’m currently following a book that has us dealing with two scenes in our story board. The Main View Controller Scene and the Flipside View Controller Scene.
In the main scene I have one label outlet that says Hello World. It’s name is label. In the flipside scene I have a text outlet. When the user flips from the flipside scene to the main scene the text in the text outlet is applied to the label outlet. So if I type in Hello StackOverflow in the flipside, and then flip it I will see Hello StackOverflow in the main scene.
We did this by going to the method that controls the flip and is in the main scene .m class and added this code.
self.label.text = controller.labelText.text;
Can anyone explain this code please? I understand that label and labelText are my names. And text is looking for the text. But I have no idea where self and controller came from and it is not explained in the book. Thank you.
EDIT
Here is the full code with the function that has controller in the function heading. I don’t get what is going on in this method. Any explanation would be great.
- (void)flipsideViewControllerDidFinish:(HWFlipsideViewController *)controller
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
[self dismissViewControllerAnimated:YES completion:nil];
}
else
{
[self.flipsidePopoverController dismissPopoverAnimated:YES];
self.flipsidePopoverController = nil;
}
self.label.text = controller.labelText.text;
}
self is the java this pointer and therefore points to your object
text is a property and would be getText()
so self.label.text is this.getLabel().getText() BUT
as it sets text=something it is setText!
=> java-bean style getter and setter
controller is likely a local variable.. PASSED to the method as an argument
OR it is a member variable on the object of the this instance
so it’d be
this = the object you’re in and that the main view I think
controller is the flipview
the ‘overwrite’ is setting the String of our label to the Controller’s textfield string