Calling Cocoa IBAction from Carbon Code - cocoa

I'm trying to call a Cocoa IBAction from Carbon code...
I've set up global keys using this tutorial.
The hot keys are working fine, but I need to fire an IBAction when the global key has been pressed.
I keep getting errors when I use
[self functionName]
How do I call the function?
I've read about passing the Cocoa controller to the carbon method. How would I do this? or what is the best way?

I assume you're calling [self functionName] in a Carbon Event handler callback. That's not an Objective-C method, so of course self is not defined.
When you install a Carbon Event handler, one of the parameters is a "user data" pointer. You can pass an Objective-C object pointer in this parameter, so that your event handler will get it, and you can say something like [(MyController*) inUserData functionName]. Of course, to make this work, your handler must be in an Objective-C or Objective-C++ source file.

you can pass one of these as your user data while keeping the program safe for c++ translations:
/* include the necessary C header, located in objc/ (objc/objc.h?) */
/* of course, definitions with objc messaging belong in your .mm file */
class t_ibaction_invocation {
/* you may want to retain d_target or d_optionalArgument, and release at destruction */
enum { RetainArguments = 0 };
public:
/* IBAction takes the form: [target action:optionalArgument]; */
t_ibaction_invocation(id target, SEL action, id optionalArgument) : d_target(target), d_action(action), d_optionalArgument(optionalArgument) {
assert(this->d_target);
if (RetainArguments) {
[this->d_target retain];
[this->d_optionalArgument retain];
}
}
~t_ibaction_invocation() {
if (RetainArguments) {
[this->d_target release], target = 0;
[this->d_optionalArgument release], optionalArgument = 0;
}
}
id performAction() {
if (this->d_target && this->d_action) {
return [this->d_target performSelector:this->d_action withObject:this->d_optionalArgument];
}
else {
assert(d_target && d_action);
return 0;
}
}
private:
id d_target;
SEL d_action;
id d_optionalArgument;
};

Related

How do I use delegates within Xamarin (specifically UNUserNotificationCenterDelegate)

I need to use the iOS 10 features in UNUserNotificationCenterDelegate. How can I implement this delegate in c# / Xamarin?
When using the an UNUserNotificationCenterDelegate, make sure that you assign it in the WillFinishLaunching or the FinishedLaunching methods in your app's UIApplicationDelegate.
You must assign your delegate object to the UNUserNotificationCenter object no later before your app finishes launching.
Ref: UNUserNotificationCenterDelegate
AppDelegate.cs Example
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
{
// Handle the user approval or refusal of your notifications...
});
UNUserNotificationCenter.Current.Delegate = new MyUNUserNotificationCenterDelegate();
return true;
}
In that example, I am creating/assigning a delegate class named MyUNUserNotificationCenterDelegate, so you need to implement that class.
MyUNUserNotificationCenterDelegate class example:
This UNUserNotificationCenterDelegate example will capture each local notification sent and toggle between showing it on the lock screen or outputting the details to the syslog.
public class MyUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
bool toggle;
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
if (toggle)
completionHandler(UNNotificationPresentationOptions.Alert);
else
{
Console.WriteLine(notification);
completionHandler(UNNotificationPresentationOptions.None);
}
toggle = !toggle;
}
}
Now you will actually need to send some notifications, this sets up a simple repeating notification:
Create/Schedule Local Notification:
// Schedule a repeating Notification...
var content = new UNMutableNotificationContent();
content.Title = new NSString("From SushiHangover");
content.Body = new NSString("StackOverflow rocks");
content.Sound = UNNotificationSound.Default;
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(timeInterval: 60, repeats: true);
var request = UNNotificationRequest.FromIdentifier(identifier: "FiveSecond", content: content, trigger: trigger);
UNUserNotificationCenter.Current.AddNotificationRequest(request, (NSError error) =>
{
if (error != null) Console.WriteLine(error);
});
Every 60 seconds a notification is dispatched and if you are on the lock screen you will receive an alert every 120 seconds...
Recommend reading to understand how you Xamarin.iOS/C# to interact with delegates, protocols, and events:
iOS uses Objective-C delegates to implement the delegation pattern, in which one object passes work off to another. The object doing the work is the delegate of the first object. An object tells its delegate to do work by sending it messages after certain things happen. Sending a message like this in Objective-C is functionally equivalent to calling a method in C#. A delegate implements methods in response to these calls, and so provides functionality to the application.
Ref: Xamarin.iOS and Delegates

Invoke a method from another class, without reinitializing it

I've a ViewController where I call a method from another class (TCP class), where I make a TCP connection to a server, that gives me a response. And I want to, when that TCP class, get's the response from the server, call another method from the ViewController.
Problems:
I'm a noob.
I'm initializing and allocating that first
Viewcontroller on the TCP, and all my vars are reseted (something
that I don't want).
So... What can I do to make it right? I just want to call a method from a different class, that is already allocated in memory.
Tks!
You could set up the ViewController as an observer to the TCP class. This is a link that explains an implementation of the observer pattern in Obj-C. (Very similar to what I use but in a nice write up.)
http://www.a-coding.com/2010/10/observer-pattern-in-objective-c.html
I usually like to separate the persistence layer from the interface as well. I use observers or KVO to notify my business logic and view controllers that something changed.
You can also send the information through the Notification Center that is provided if you prefer...
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html
Basic Code Example:
#implementation ExampleViewController
//...
- (void)viewDidLoad
{
[super viewDidLoad:animated];
[TCPClass subscribeObserver:self];
}
- (void)viewDidUnload
{
[super viewDidUnload:animated];
[TCPClass unsubscribeObserver:self];
}
- (void)notifySuccess:(NSString*)input
{
//Do whatever I needed to do on success
}
//...
#end
#implementation TCPClass
//...
//Call this function when your TCP class gets its callback saying its done
- (void)notifySuccess:(NSString*)input
{
for( id<Observer> observer in [NSMutableArray arrayWithArray:observerList] )
{
[(NSObject*)observer performSelectorOnMainThread:#selector(notifySuccess:) withObject:input waitUntilDone:YES];
}
}
//maintain a list of classes that observe this one
- (void)subscribeObserver:(id<Observer>)input {
#synchronized(observerList)
{
if ([observerList indexOfObject:input] == NSNotFound) {
[observerList addObject:input];
}
}
}
- (void)unsubscribeObserver:(id<Observer>)input {
#synchronized(observerList)
{
[observerList removeObject:input];
}
}
//...
#end
//Observer.h
//all observers must inherit this interface
#protocol Observer
- (void)notifySuccess:(NSString*)input;
#end
Hope that helps!

URL scheme - Qt and mac

I'm trying to implement a custom URL scheme for my application. I've added the necessary lines for my Info.plist. After calling the specified url (eg.: myapp://) the application launches.
If I want to handle the URL, I've found these steps:
#interface EventHandler : NSObject {
}
#end
#implementation EventHandler
- (id)init {
self = [super init];
if (self) {
NSLog(#"eventHandler::init");
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:#selector(applicationDidFinishLaunching:)
// name:NSApplicationWillFinishLaunchingNotification
name:NSApplicationDidFinishLaunchingNotification
object:nil];
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self andSelector:#selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
}
- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
NSLog(#"%#", url);
}
#end
The above code is working if the application is running, but if the URL gets called and the application was terminated, the event is not caught. I think this is because this: NSApplicationDidFinishLaunchingNotification.
Changing it to NSApplicationWillFinishLaunchingNotification causes that non events caught. Maybe Qt handles it before me, but I can't find a workaround for the problem.
I was also trying to get my Qt-based application handle a custom URL scheme on the Mac and went down the same path as the original poster. It turns out that Qt4 already supports URL events on the Mac, and there's no need to write Objective-C code to receive them. This is in fact the reason that you didn't receive any URL events when you set the event handler in response to NSApplicationWillFinishLaunchingNotification: Qt registers its own handler afterward.
When a URL with your custom scheme is triggered, your Qt application will receive a FileOpenEvent. Note that it is the QApplication instance which receives the event. You can catch it by making your application subclass QApplication or by installing an event filter on the standard QApplication. I opted for this second approach.
Here's the eventFilter method of my custom event filter class, FileOpenEventFilter. It just emits the signal urlOpened when the event contains a non-empty URL. It also saves the last opened URL in case my main window isn't completely initialized when the event arrives (which happens in my app when it's not already running when the custom URL is clicked.)
bool FileOpenEventFilter::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::FileOpen)
{
QFileOpenEvent* fileEvent = static_cast<QFileOpenEvent*>(event);
if (!fileEvent->url().isEmpty())
{
m_lastUrl = fileEvent->url().toString();
emit urlOpened(m_lastUrl);
}
else if (!fileEvent->file().isEmpty())
{
emit fileOpened(fileEvent->file());
}
return false;
}
else
{
// standard event processing
return QObject::eventFilter(obj, event);
}
}
I register my handler in my application delegate's applicationWillFinishLaunching: method, and I don't miss any events. You're probably initializing your EventHandler object too late to get that notification. If you want to keep it as a separate class, that's ok, but you should create your object and register it with NSAppleEventManager within the applicationWillFinishLaunching: method of your application delegate.

How to bind a control to a singleton in Cocoa?

I have a singleton in my FTP app designed to store all of the types of servers that the app can handle, such as FTP or Amazon S3. These types are plugins which are located in the app bundle. Their path is located by applicationWillFinishLoading: and sent to the addServerType: method inside the singleton to be loaded and stored in an NSMutableDictionary.
My question is this:
How do I bind an NSDictionaryController to the dictionary inside the singleton instance? Can it be done in IB, or do I have to do it in code? I need to be able to display the dictionary's keys in an NSPopupButton so the user can select a server type.
Thanks in advance!
SphereCat1
I found / made up the answer to this: I simply override the init method so when it's called from the XIB file, it still returns the singleton. I then provide a method named realInit to do an actual initialization the first time init is called.
Code:
-(id)init
{
#synchronized(self)
{
if (_sharedInstance == nil)
{
_sharedInstance = [[VayprServerTypes alloc] realInit];
}
}
[self release];
return _sharedInstance;
}
-(id)realInit
{
if (self = [super init])
{
serverTypesArray = [[NSMutableArray alloc] init];
}
return self;
}
EDIT: Of course you'll need to define _sharedInstance as a static variable at the top of your class implementation:
static ClassTypeGoesHere *_sharedInstance;
ALSO EDIT: Since you now know for sure that your init method will be called at least once, you can go ahead and replace your normal singleton sharedInstance method with this:
+(ClassTypeGoesHere *)sharedInstance
{
return _sharedInstance;
}
If anyone sees any obvious problems with this design, please let me know!
SphereCat1

How to declare a function in Cocoa after the function using it?

I'm slowly building my application to a working state.
I'm using two functions called setCollection and addToCollection. These functions both accept NSArray as input.
I also have a function called add in which I use both of these functions. When I try to compile, Xcode shows an error:
'setCollection' undeclared (first use in this function)
I guess this has to do with the function called being defined below the active function. Another guess would be that the functions should be globalized to be useable inside my add function.
I'm normally a php coder. the way Php handles this is the first one. The functions called should be before the functions using them, because otherwise they just don't exist. Is there a way to make functions still to come available at runtime, or should I rearrange all functions to make them function properly?
You can declare functions ahead of time as follows:
void setCollection(NSArray * array);
void addToCollection(NSArray * array);
//...
// code that calls setCollection or addToCollection
//...
void setCollection(NSArray * array)
{
// your code here
}
void addToCollection(NSArray * array)
{
// your code here
}
If you are creating a custom class, and these are member functions (usually called methods in Objective-C) then you would declare the methods in your class header and define them in your class source file:
//MyClass.h:
#interface MyClass : NSObject
{
}
- (void)setCollection:(NSArray *)array;
- (void)addToCollection:(NSArray *)array;
#end
//MyClass.m:
#import "MyClass.h"
#implementation MyClass
- (void)setCollection:(NSArray *)array
{
// your code here
}
- (void)addToCollection:(NSArray *)array
{
// your code here
}
#end
//some other source file
#import "MyClass.h"
//...
MyClass * collection = [[MyClass alloc] init];
[collection setCollection:someArray];
[collection addToCollection:someArray];
//...
If your functions are global (not part of a class), you just have to put the declaration before the use, just like eJames suggests.
If your functions actually are methods (part of a class), you have to declare an anonymous category of your class before the implementation and put your method declarations in this interface:
#interface Myclass()
- (void) setCollection:(NSArray*)array;
- (void) addToCollection:(NSArray*)array;
#end
#implementation Myclass
// Code that calls setCollection or addToCollection
- (void) setCollection:(NSArray*)array
{
// your code here
}
- (void) addToCollection:(NSArray*)array
{
// your code here
}
#end
This way, you don't need to expose your functions in the main interface of MyClass.

Resources