Objective-C Private Property Inheritance - objective-c

I just wanted to make sure my understanding of property inheritance was correct. I'm currently trying to make a subclass of a UIViewController. In my UIViewController all my outlets and such are declared in the implementation section like so:
#interface BaseClass()
#property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
#end
Thus those properties are then private, correct? Now when I try and make a subclass and access those properties using my getters and setters, I cannot access them from my subclass. Is the proper form to redeclare those properties again in my subclass' implementation section, like so?
#interface SubClass()
#property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
#end
I guess I am ok with doing it this way, but then I feel like it ruins the purpose of inheritance. What is the proper way/what am I doing wrong?

I would declare the property in the public interface for the BaseClass - I don't see any reason to put them in a class extension.
#interface BaseClass : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
#end
#interface SubClass : BaseClass
// No need to redeclare the property as you're inheriting it.
#end
[EDIT]
If you must use the class extension then you could use a private header to achieve the same.
Public header for BaseClass (BaseClass.h)
#interface BaseClass : UIViewController
#end
Private header for BaseClass (BaseClass-Private.h)
#interface BaseClass ()
#property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
#end
Public header for SubClass (SubClass.h)
#import "SubClass.h"
#interface SubClass : BaseClass
#end
Implementation of SubClass (SubClass.m)
#import "BaseClass-Private.h"
#implementation SubClass
#end

Related

cocoa-Why there is an IBOutlet and a property that are of the same name?

I'm new to objective-c. When I'm reading some source code written by others, I encountered a problem.
I found that there is
IBOutlet NSPopover *popover;
as well as
#property NSPopover *popover;
PopoverViewController.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "TimerPopoverViewController.h"
#class TimerLogic;
#class TimerInfo;
#interface TimerPopoverDelegate : NSObject <NSPopoverDelegate> {
#private
IBOutlet NSPopover *popover;
IBOutlet NSWindow *detachWindow;
IBOutlet TimerPopoverViewController *viewController;
}
#property NSPopover *popover;
- (void)showPopover:(id)sender timerInfo:(TimerInfo *)timerInfo;
#end
I think they are different variables. However, I can't figure out what do they do?
As far as I'm concerned, the IBOutlet is to show a popover.
But what does the #property does?
This is either very old code or written in a very old (and now discouraged) style. The IBOutlet here is declaring an instance variable (ivar). The #property is declaring a property that is backed by the instance variable. In modern ObjC you should implement it this way:
PopoverViewController.h
#import <Cocoa/Cocoa.h>
#class TimerInfo;
// Things declared here are public
#interface TimerPopoverDelegate : NSObject <NSPopoverDelegate>
// You could leave this here if it is required by other parts of the program,
// but other parts of the program really shouldn't require it. See below.
// #property (nonatomic, readonly, weak) NSPopover *popover;
- (void)showPopover:(id)sender timerInfo:(TimerInfo *)timerInfo;
#end
PopoverViewController.m
// Generally avoid importing local headers into the .h unless you have to.
#import "TimerPopoverViewController.h"
// Things declared here are private. This is much better than the old #private.
#interface TimerPopoverDelegate ()
#property (nonatomic, readwrite, weak) IBOutlet NSPopover *popover;
#property (nonatomic, readwrite, weak) IBOutlet NSWindow *detachWindow;
#property (nonatomic, readwrite, weak) IBOutlet TimerPopoverViewController *viewController;
#end
(Currently popover is public, but you should avoid exposing an IBOutlet that way. Outside objects should not directly touch a view controller's outlets.)

Property implementation must have its declaration in interface 'FirstViewController'

I am new to Objective-C and am very noobish.
I am trying to pass the text from a UITextField in the SecondViewController to the FirstViewController.
What I have right now is:
(SecondViewController.h)
#interface SecondViewController : UIViewController <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *PrimaryPhone;
#end
(FirstViewController.m)
#import "SecondViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize PrimaryPhone;
Then there is the other default items included within the .m file.
But where it says:
#synthesize PrimaryPhone;
Xcode gives me the error in the title. (Property implementation must have its declaration in interface 'FirstViewController')
You must have the property primaryPhone inside FirstViewController.h
Alternatively within the interface
#interface FirstViewController ()
#end
In FirstViewController.m
What you are trying to synthesize is belonging to an object of type FirstViewController, but it is declared in a class of type SecondViewController.

Can't synthesise NSWindow

I'm trying to synthesise my NSWindow "loginscreen" but I keep getting the error "property implementation must have its declaration in interface "appdelegate"
This is the part of the header
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
#private
NSWindow *window;
NSWindow *loginscreen;
And this my method:
#synthesize loginscreen = _loginscreen;
Any suggestions? thanks for the help.
If you are using ARC you need to add the following line to your AppDelegate.h file:
#property (nonatomic, strong) NSWindow *loginWindow;
If you are not using ARC you should instead use:
#property (nonatomic, assign) NSWindow *loginWindow;
This line would go after the {} block in #interface but before the #end.

change superclass' property value

I got class
#interface PlayScene : UIView
with some properties in it (here are some of them)
#property (readwrite, assign) int Figure1;
#property (readwrite, assign) int Figure2;
#property (readwrite, assign) int Figure3;
and I got a subView that is called in PlayScene
#interface gameOverMenu : PlayScene <UITextFieldDelegate>
and I need to get somehow those properties in my subclass. Actually I need to set them equal to 0 so that "Play Again" button works properly.
For those having same issue the original question was :
#interface gameOverMenu : UIView <UITextFieldDelegate>
He solved it by changing it to :
#interface GameOverMenu : PlayScene <UITextFieldDelegate>

IBOutlet declarations?

I have seen the code below written 3 different ways (with regards to IBOutlet) Does it matter, I would say adding IBOutlet to both the declaration and the #property was more concise.
JUST PROPERTY:
#class SwitchViewController;
#interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
SwitchViewController *switchViewController;
}
#property(nonatomic, retain) IBOutlet UIWindow *window;
#property(nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
#end
JUST DECLARATION:
#class SwitchViewController;
#interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet SwitchViewController *switchViewController;
}
#property(nonatomic, retain) UIWindow *window;
#property(nonatomic, retain) SwitchViewController *switchViewController;
#end
BOTH:
#class SwitchViewController;
#interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet SwitchViewController *switchViewController;
}
#property(nonatomic, retain) IBOutlet UIWindow *window;
#property(nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
#end
cheers gary
IBOutlet does only matter to InterfaceBuilder. For the compiler, UINibDeclarations.h #defines it to nothing.
InterfaceBuilder takes IBOutlet as a hint from the header file to list the available outlets for the class. If you wire up an object to an IBOutlet, no matter whether it is defined as a property or instance variable, this information is written into the nib.
When loading the nib, the loader tries to find the best possible way to setup the connection: First it tries to find a setter method with an appropriate name. If no such setter is found, it falls back to setting the instance variable directly, which is poor style, because memory management is not clear this way.
All your proposed examples have a property (and, of course, a setter method) of the right name. So in each case, the loader would use the setter method, no matter where the IBOutlet tag stands: There’s no difference between your examples, neither in the nib, nor in the way code is executed.
The best style would be to put the IBOutlet tag into the property definition.
Should not matter. With the 10.6 64-bit SDK you can also write the property without the ivar:
#class SwitchViewController;
#interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
}
#property(nonatomic, retain) IBOutlet UIWindow *window;
#property(nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
#end
The current style in Apple's example code puts the IBOutlet in the property declaration. For consistency, that's probably the best place to throw it.
I also found this way (without any property declaration):
#class SwitchViewController;
#interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet SwitchViewController *switchViewController;
}
#end
What about this?