Reputation: 23
I am trying to convert the following iOS code into MonoTouch and cannot figure out the proper conversion for the @selector(removebar) code. Can anyone provide guidance about the best way to handle @selector (since I've come across that in other places as well):
- (void)keyboardWillShow:(NSNotification *)note {
[self performSelector:@selector(removeBar) withObject:nil afterDelay:0];
}
My C# code is:
NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification,
notify => this.PerformSelector(...stuck...);
I am basically trying to hide the Prev/Next buttons that show on the keyboard.
Thanks in advance for any help.
Upvotes: 1
Views: 947
Reputation: 32694
Stephane shows one way you can use our improved bindings to convert that.
Let me share an even better one. What you are looking for is a keyboard notification, which we conveniently provide strong types for, and will make your life a lot easier:
http://iosapi.xamarin.com/?link=M%3aMonoTouch.UIKit.UIKeyboard%2bNotifications.ObserveWillShow
It contains a full sample that shows you how to access the strongly typed data that is provided for your notification as well.
Upvotes: 2
Reputation: 19239
You have to take into account that:
[self performSelector:@selector(removeBar) withObject:nil afterDelay:0];
it's exactly the same that
[self removeBar];
The call to performSelector
is just a method call using reflection. So what you really need to translate to C# is this code:
- (void)keyboardWillShow:(NSNotification *)note {
[self removeBar];
}
And I guess that also the notification subscription, that sums up to this code:
protected virtual void RegisterForKeyboardNotifications()
{
NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification);
NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
}
private void OnKeyboardNotification (NSNotification notification)
{
var keyboardVisible = notification.Name == UIKeyboard.WillShowNotification;
if (keyboardVisible)
{
// Hide the bar
}
else
{
// Show the bar again
}
}
You usually want to call RegisterForKeyboardNotifications
on ViewDidLoad
.
Cheers!
Upvotes: 1
Reputation: 16232
NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, removeBar);
where removeBar
is a method defined elsewhere.
void removeBar (NSNotification notification)
{
//Do whatever you want here
}
Or, if you prefer using a lambda:
NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillShowNotification,
notify => {
/* Do your stuffs here */
});
Upvotes: 2