I am quite new in Objective C, but learning fast. My project is quite complete and I have implemented as well classes than delegates etc etc.
I am using an MKMapView to display things on a map. I also need the user to click on Pins which will give back some informations, but I do want that the MKViewMap makes some estimations before sending info.
As such I have subclassed MKMapView, and I am using this class to display. But I don't get the delegates called. It worked normally when I was using MKViewMap in a standard way. Here is some code. The subclass has a delegate protocol, and this works perfectly
// .h file
#import < MapKit/MapKit.h >
#protocol MyMapViewDelegate < NSObject >
#required
- (void) ReturnAddressToHail:(NSString*) FullAddresse andNumber:(NSString*) FullNumber;
- (void) ReturnSelectedDriverHash:(NSString*) DriverHash;
#end
#interface MyMapViewClass : MKMapView < MKMapViewDelegate >
{
id < MyMapViewDelegate > delegate;
CLLocationCoordinate2D mapCenter;
NSDictionary *Info;
CLLocation * ClientNewLocation;
}
#property (retain) id delegate;
// .m file
#import "MyMapViewClass.h"
#import "NSObject+MapAnnotationHelper.h"
#implementation MyMapViewClass
#synthesize geoCoder;
#synthesize delegate;
self.delegate = self;
-(void) mapView:(MKMapView *) mapView regionDidChangeAnimated:(BOOL)animated
{
(void) self.updateAddress;
NSLog(#"regionchange delegate");
}
The latter is never called. Any ideas?
I'm sure you have your reasons, but the docs state that you shouldn't subclass MKMapView, and instead use the delegate to change its behaviour. There may be an easier way to do whatever you're doing.
Although you should not subclass the MKMapView class itself, you can get information about the map view’s behavior by providing a delegate object.
Related
Hi I'm implementing a subclass of NSSlider and NSSliderCell in swift, I would like to check/retrieve the _scFlags.isPressed property from NSSliderCell, I found that there is a 'hack' in Objective-C to do it when calling from another class (which is not really needed if you subclass NSSlider in Objective-C):
http://www.cocoabuilder.com/archive/cocoa/54913-nsslider.html
However, in swift, when I search in the API (AppKit -> NSSliderCell), I can find a struct which I suppose is the one I need, and nothing else but the init() inside:
public struct __sliderCellFlags {
public init()
}
and I can't even call it from the subclass. All these show as error:
self.__sliderCellFlags
self._scFlags
super.__sliderCellFlags
super._scFlags
Am I missing something? Is there a different way to call these properties?
Use Objective-C to implement a category on NSSliderCell which has a property to access the struct data and use the property from Swift.
NSSlider+FlagsAccessor.h
#interface NSSlider (FlagsAccessor)
#property (nonatomic, readonly) BOOL isPressed;
#end
NSSlider+FlagsAccessor.m
#import "NSSlider+FlagsAccessor.h"
#interface NSSliderCell (FlagsAccessor)
#property (nonatomic, readonly) BOOL isPressed;
#end
#implementation NSSlider (FlagsAccessor)
- (BOOL)isPressed
{
NSSliderCell *cell = self.cell;
return cell.isPressed;
}
#end
#implementation NSSliderCell (FlagsAccessor)
- (BOOL)isPressed
{
return self->_scFlags.isPressed == 1;
}
#end
Then to use It from Swift just call self.isPressed. Here's a sample project.
Don't forget to import your category header in your bridging header (Xcode will prompt you to create a bridging header when you add Objective-C code to a Swift project).
I followed the advice here on how to setup a MainWindowController: NSWindowController for my project's single window. I used a Cocoa class to create the .h/.m files, and I checked the option Also create .xib for User Interface. As a result, Xcode automatically hooked up a window, which I renamed MainWindow.xib, to my MainWidowController.
Next, I deleted the window in the default MainMenu.xib file (in Interface Builder I selected the window icon, then I hit the delete key). After that, I was able to Build my project successfully, and my controller's window in MainWindow.xib displayed correctly with a few buttons on it.
Then I tried adding an NSTableView to my MainWindowController's window. In Xcode, I dragged the requisite delegate and datasource outlets for the NSTableView onto File's Owner, which is my MainWindowController, and I implemented the methods in MainWindowController.m that I thought would make the NSTableView display my data:
- tableView:viewForTableColumn:row:
- numberOfRowsInTableView:
Now, when I Build my project, I don't get any errors, but the data doesn't appear in the NSTableView.
My code is below. Any tips are welcome!
//
// AppDelegate.h
// TableViews1
//
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#end
...
//
// AppDelegate.m
// TableViews1
//
#interface AppDelegate ()
#property (weak) IBOutlet NSWindow *window;
#property (strong) MainWindowController* mainWindowCtrl;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[self setMainWindowCtrl:[[MainWindowController alloc] init] ];
[[self mainWindowCtrl] showWindow:nil];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
#end
...
//
// MainWindowController.h
// TableViews1
//
#import <Cocoa/Cocoa.h>
#interface MainWindowController : NSWindowController
#end
...
//
// MainWindowController.m
// TableViews1
//
#import "MainWindowController.h"
#import "Employee.h"
#interface MainWindowController () <NSTableViewDataSource, NSTableViewDelegate>
#property (strong) NSMutableArray* employees;
#property (weak) IBOutlet NSTableView* tableView;
#end
#implementation MainWindowController
- (NSView*)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {
Employee* empl = [[self employees] objectAtIndex:row];
NSString* columnIdentifier = [tableColumn identifier];
//The column identifiers are "firstName" and "lastName", which match my property names.
//You set a column's identifier by repeatedly clicking on the TableView until only
//one of the columns is highlighted, then select the Identity Inspector and change the column's 'Identifier' field.
NSString* emplInfo = [empl valueForKey:columnIdentifier]; //Taking advantage of Key-Value coding
NSTableCellView *cellView =
[tableView makeViewWithIdentifier:columnIdentifier
owner:self];
NSLog(#"The Table view is asking for employee: %#", [empl firstName]);
[[cellView textField] setStringValue:emplInfo];
return cellView;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [[self employees] count];
}
- (void)windowDidLoad {
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
Employee* e1 = [[Employee alloc] initWithFirstName:#"Joe" lastName:#"Blow"];
Employee* e2 = [[Employee alloc] initWithFirstName:#"Jane" lastName:#"Doe"];
[self setEmployees:[NSMutableArray arrayWithObjects:e1, e2, nil]];
//Test to see if the employees array was populated correctly:
Employee* e = [[self employees] objectAtIndex:0];
NSLog(#"Here is the first employee: %#", [e firstName]);
//I see the output: "Here is the first employee: Joe"
}
- (id)init {
return [super initWithWindowNibName:#"MainWindow"];
}
- (id)initWithWindowNibName:(NSString *)windowNibName {
NSLog(#"Clients cannot call -[%# initWithWindowNibName] directly!",
[self class]
);
[self doesNotRecognizeSelector:_cmd];
return nil;
}
#end
...
//
// Employees.h
// TableViews1
#import <Foundation/Foundation.h>
#interface Employee : NSObject
#property NSString* firstName;
#property NSString* lastName;
- initWithFirstName:(NSString*)first lastName:(NSString*)last;
#end
...
//
// Employees.m
// TableViews1
//
#import "Employee.h"
#implementation Employee
- (id)initWithFirstName:(NSString *)first lastName:(NSString *)last {
if (self = [super init]) {
_firstName = first; //I read that you shouldn't use the accessors in init methods.
_lastName = last;
}
return self;
}
#end
File's Owner(=MainWindowController) connections:
NSTableView connections:
Response to comments:
Here is why calling [self tableView] reloadData] at the end of -windowDidLoad, as suggested in the comments, didn't work:
My _tableView instance variable--created by my #property declaration in MainWindowController.m--doesn't point to anything; therefore calling:
[[self tableView] reloadData]
I think is equivalent to calling:
[nil reloadData]
which doesn't do anything.
I never assigned anything to the _tableView instance variable in the -init method, nor did I assign it a value by dragging an outlet somewhere in Interface Builder. To fix that problem, I selected MainWindow.xib (the controller's window) in the Project Navigator(left pane), and then in the middle pane(Interface Builder), I selected the cube representing the File's Owner(selecting the Identity Inspector in the right pane reveals that the File's Owner is the MainWindowController). Then in the right pane, I selected the Connections Inspector, and it revealed an outlet called tableView, which is the IBOutlet variable I declared in MainWindowController.m.
Next, I dragged from the tableView outlet onto the TableView in the middle pane:
Doing that assigns the NSTableView object to the _tableView instance variable that was created by my #property declaration in MyWindowControler.m:
#property (weak) IBOutlet NSTableView* tableView;
As an experiment, I disconnected the outlet, then commented out the #property declaration for tableview, and the tableView outlet no longer appeared in the Connections Inspector. Also, if I change the declaration from:
#property (weak) IBOutlet NSTableView* tableView;
to:
#property (weak) NSTableView* tableView;
...then the tableView outlet doesn't appear in the Connections Inspector. That experiment answered a couple of questions I had about whether I should declare a property as an IBOutlet or not: if you need to assign one of the objects in Interface Builder to one of your variables, then declare the variable as an IBOutlet.
Thereafter, calling [self tableView] reloadData] at the end of -windowDidLoad succeeds in populating the TableView. However, I have not seen any tutorials that call reloadData, and even Apple's guide does not do that.
So, I am still puzzled about whether calling -reloadData is a hack or it's the correct way to do things.
Without it, your table view sits there blissfully clueless about your
expectation that it should even bother asking its datasource for data.
I assumed that an NSTableView automatically queries its datasource when it is ready to display itself, and that my code needed to be able to provide the data at that time.
I don't see you sending -reloadData to your table view anywhere. Tacking it onto the end of -windowDidLoad would be a good place. Without it, your table view sits there blissfully clueless about your expectation that it should even bother asking its datasource for data.
For all it knows, the data is simply not ready / available, so why would it try? More importantly, when should it try? It'd be rather rude of it to try whenever it pleases, considering the UI may not have finished loading / connecting to outlets, or its datasource may be in a vulnerable state (like teardown during/after dealloc) and sending datasource requests may result in a crash, etc.
Two things:
1st, set some breakpoints on when you set your employees array in windowDidLoad vs. when the table first attempts to populate itself and your numberOfRowsInTableView implementation gets called. If the latter happens before the former, then you'll need to add a reloadData after you create your array.
2nd, I personally always use NSCell instead of NSViews for my tables, so I always implement objectValueForTableColumn in my table's datasource. So I'm not sure if there's something different you need to do when you use NSView objects and implement viewForTableColumn. Is there a reason you're not using NSCell?
I am developing an application in XCode 4.6.
To get text-change notifications from NSTextField controls I:
Put NSTextField control on window.
Connect control delegate to File's Owner via right-click in IB, drag from delegate to File's Owner.
Implement controlTextDidChange in window class.
For the application, the window class is my AppDelegate and File's Owner is NSApplication. For the modal dialog, the window class an NSWindowController and File's Owner is of the same type.
If I put a breakpoint in controlTextDidChange, in the AppDelegate class, it never fires. If I do the same procedure with a modal dialog it works fine.
I know in the main application window case the delegate for the control is not my AppDelegate.
What am I doing wrong in hooking up my control delegate in the main window? I must be missing something simple. Is File's Owner the correct delegate to set for controls?
Any help would be appreciated.
Here is some code as requested.
// AppDelegate.h
// SimpleApplication
#import <Cocoa/Cocoa.h>
#import "SimpleTest/SimpleTest.h"
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSTextField *textField;
#end
// AppDelegate.m
// SimpleApplication
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Not much to do here for now.
}
// Breakpoint set in this function never fires.
- (void)controlTextDidChange:(NSNotification *)obj
{
NSMutableString* description= [[NSMutableString alloc] init];
id aDelegate= [_textField delegate];
Class delegateClass= [aDelegate class];
[description setString:[delegateClass description]];
[description release];
}
// To provide some information about the delegates.
- (IBAction)textChange:(id)sender
{
NSTextField* theTextField= (NSTextField*)sender;
NSMutableString* description= [[NSMutableString alloc] init];
id aDelegate= [theTextField delegate];
Class delegateClass= [aDelegate class];
[description setString:[delegateClass description]];
[description release];
}
#end
Here is a shot of the right-click information for the NSTextField on the main window -
Identity inspector shows File's Owner as NSApplication, which is what I see in the debugger when I put a breakpoint in textChange and hit return in the text field. However, self, the implementor of controlTextDidChange, is AppDelegate. By contrast, in a modal dialog, self and File's Owner are the same object, derived from NSWindowController.
So, the upshot is that I do not have the correct delegate assigned to the control in the main window - how do I do that?
Can you post some code?
When using delegates make sure you specify that a class implements the required protocol.
#interface MyClass : NSObject <SomeProtocol>
Also make sure you are creating a property to store the delegate.
#property (strong, nonatomic) id<SomeProtocol> delegate;
RE this:
Note that although NSControl defines delegate methods, it does not
itself have a delegate. Any subclass that uses these methods must have
a delegate and the methods to get and set it. In addition, a formal
delegate protocol NSControlTextEditingDelegate Protocol also defines
delegate methods used by control delegates.
...
These include: controlTextDidBeginEditing:, controlTextDidChange:, and controlTextDidEndEditing:
Oh, wow - in adding more detail to my question, I think I figured out the answer. Instead of dragging from text field delegate to File's Owner, just drag to the blue cube that represents App Delegate!
This error very strange. I have a bunch of properties in app delegate which I have no problem to access. My code worked fine without ARC, but when I turned on ARC I receive this very strange error.
Property 'navigationController' not found on object of type 'AppDelegate *'
A bunch of other properties work just fine except this.
I already clean everything and restarted Xcode.
Here is the code:
AppDelegate.h:
#interface AppDelegate : UIResponder <UINavigationControllerDelegate> {
UINavigationController *navigationController;
NSString *appName;
}
#property (strong, nonatomic) NSString *appName;
#property (strong, nonatomic) UINavigationController *navigationController;
AppDelegate.m
#import "AppDelegate.h"
#synthesize navigationController, appName;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
appName = [NSString stringWithString:[[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleName"]];
navigationController = [[UINavigationController alloc] initWithRootViewController:self.startViewController];
}
MyClass.h
#class AppDelegate;
#interface MyClass: UIResponder {
AppDelegate *appDelegate;
}
MyClass.m
#import "AppDelegate.h"
#import "MyClass.h"
#implementation MyClass
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self initFacebook];
}
return self;
}
- (void) doStuff {
NSLog(#"App Name:%#",appDelegate.appName); //this works fine
//This one doesn't
UINavigationController *myNavigationController = appDelegate.navigationController;
}
Update:
I did a workaround. Basically I created a property in MyClass called navigationViewController and I pass the object after MyClass is instantiated in AppDelegate instead of getting directly from AppDelegate in MyCLass (as shown above). I'm still puzzled, it must be a bug in the compiler. I'm still very interested in how to make the original code work.
In MyClass.m, you are importing "MyClass" instead of "MyClass.h". Could this be the problem?
Sometimes cleaning a project is not sufficient: you may need to open the Organizer window, select your project under the Projects tab, and click the button to delete Derived Data.
OK, I put only the problematic code into a empty project as seen in the question. It compiles fine. It's really puzzling so I concluded it must be a bug in Xcode, or maybe some settings.
Anyways I couldn't figure out what went wrong so I just pass the problematic object (navigationController) when MyClass is instantiated in AppDelagate. It's fine with one object and thankfully all the other appDelegate properties/selectors can be accessed fine.
I'm pretty new to mac development (coming from a web and iOS background) and I can't work out how I could get a notification every time the value of an NSTextView changes. Any ideas?
Ups I just saw that you want a callback from NSTextView and not NSTextField
Just add in the header of the object which should be the delegate the protocol
#interface delegateAppDelegate : NSObject <NSApplicationDelegate, NSTextViewDelegate> {
NSWindow *window;
}
After that you add a method like
-(void)textDidChange:(NSNotification *)notification {
NSLog(#"Ok");
}
Make sure you connected the delegate property of the NSTextView (not NSScrollView) with the object which should receive the delegate
Here's the solution:
NSTextView *textView = ...;
#interface MyClass : NSObject<NSTextStorageDelegate>
#property NSTextView *textView;
#end
MyClass *myClass = [[MyClass alloc] init];
myClass.textView = textView;
textView.textStorage.delegate = myClass;
#implementation MyClass
- (void)textStorageDidProcessEditing:(NSNotification *)aNotification
{
// self.textView.string will be the current value of the NSTextView
// and this will get invoked whenever the textView's value changes,
// BOTH from user changes (like typing) or programmatic changes,
// like textView.string = #"Foo";
}
#end
set the nstextfield's delegate. in the .h file of the delegate you add the delegate protocol
In the .m file you add a method like -(void)controlTextDidChange:(NSNotification *)obj {
NSLog(#"ok");
}
I hope that helps
Set the delegate and then use
- (void) controlTextDidChange: (NSNotification *) notification
{
}