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

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.)

Related

How to add a UIView to an UIAlertView?

I have to create a custom AlertView with a view that contain some label. I create the .h and .m of CustomAlert
this is the CustomAlert.h.
#import <Foundation/Foundation.h>
#interface CustomAlert: UIView
#property (nonatomic, weak) IBOutlet UILabel *ok;
#property (nonatomic, weak) IBOutlet UILabel *ok1;
#property (nonatomic, weak) IBOutlet UILabel *ok2;
#property (nonatomic, weak) IBOutlet UILabel *ok3;
#property (nonatomic, weak) IBOutlet UILabel *ok4;
#property (nonatomic, weak) IBOutlet UILabel *ok5;
#end
and this is the CustomAlert.m.
#import "CustomAlert.h"
#implementation CustomAlert
#synthesize ok = _ok;
#synthesize ok1 = _ok1;
#synthesize ok2 = _ok2;
#synthesize ok3 = _ok3;
#synthesize ok4 = _ok4;
#synthesize ok5 = _ok5;;
#end
I also create the xib with just a view and i connect the label with all the "ok".
Now, i want to add this View to an AlertView with just an Ok button. How can i do it? I want to use it with ios 7 and 6.
Thanks!!
In iOS 7 you should not munge a UIAlertView like this - and there is no need, because thanks to custom view transitions you can make your own small presented view in front of your interface, so that it looks and acts like a UIAlertView but can contain anything you like.
I've written an online tutorial on how to do this:
http://programming.oreilly.com/2014/01/transcending-uialertview-on-ios-7.html
And there's a downloadable github project that goes with it:
https://github.com/mattneub/custom-alert-view-iOS7

Xcode displays error when building Mac app for release

I'm writing an application for OS X using core data. It's no problem to build and run it for debugging but when I switch to release or try to archive it, it throws the error
'/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/CoreData.framework/Headers/NSFetchRequestExpression.h:15:39: Attempting to use the forward class 'NSExpression' as superclass of 'NSFetchRequestExpression'
When I delete all fetch request from the code, it works fine.
Does anybody know what problem this could be? Thank you.
Here are the header:
#import <Cocoa/Cocoa.h>
#import <CoreData/CoreData.h>
#import "EFMainController.h"
#interface EF_AppDelegate : NSObject
{
IBOutlet NSMenu *statusMenu;
NSStatusItem *statusItem;
NSWindow *window;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
EFMainController *mainController;
NSTabView *tabView;
IBOutlet NSImageView *dockTileView;
}
#property (nonatomic, retain) IBOutlet NSWindow *window;
#property (nonatomic, retain) IBOutlet EFMainController *mainController;
#property (assign) IBOutlet NSTabView *tabView;
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator;
- (NSManagedObjectModel *)managedObjectModel;
- (NSManagedObjectContext *)managedObjectContext;
- (IBAction)saveAction:sender;
#end
and
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface EFMainController : NSObject {
/* some variables */
}
/* some properties and actions */
#end
The stuff where I put the comments has nothing to do with core data
I have a program where I experience the same Problem and so far I could not really solve it but I was able to somewhat build it for release. So I guess this is not a real/full answer but for me it worked to use Product > Build For > Archiving.

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.

Method definition not found

What am I doing wrong? This is the .h file:
#import <UIKit/UIKit.h>
#import <sqlite3.h>
#class ReaderViewController;
#interface ReaderAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ReaderViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ReaderViewController *viewController;
- (void)checkForDatabase;
//- (void)SQLiteConnection: (NSString *)defaultDBPath;
#end
The error is shown here:
You are calling the [self checkForDatabase] method which doesn't appear to exist in the .m file.
The Incomplete Implementation warning is because you have declared the checkForDatabase method in your interface
The Method Definition error is because you are attempting to call the missing method in the application:didFinishLaunchingWithOptions: method.
You have failed to implement checkForDatabase in ReaderAppDelegate.m (or in any other file you're linking into the project). You said you would in the header, and then you didn't.

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?