How to Create Action for NSTouchBarButton with Xcode ?

berkbababerkbaba TRMember ✭✭
edited May 2017 in Xamarin.iOS

Hi,

After opening the storyboard with Xcode, I created a customized NSTouchBar item in Windows Controller and activate it just this code:

public override void DidFinishLaunching(NSNotification notification)       
 {         
   // Insert code here to initialize your application            
NSApplication.SharedApplication.SetAutomaticCustomizeTouchBarMenuItemEnabled(true); // Enable touchbar.              
 }

However, I cannot crate an actions for my NSTouchBar buttons. I drag&drop the blue line via "control" button clicking to "NSResponder.h" which is automatically opened by assistant editor in Xcode. This error fires up: "Could not inserted new action connection: No @implementation found fot the class "NSResponder" "

How can I create action for my NSTouchBar buttons properly ? Thanks.

Best Answer

  • berkbababerkbaba TR ✭✭
    Accepted Answer

    Hi, I found the solution :smile:

        //1. Set an outlet for View Controller's touch bar.
    
        [Outlet]
        AppKit.NSTouchBar Chrono_TouchBar { get; set; }
    
        //2. Override the toch bar in View Controller with your custom controller:
    
            public override NSTouchBar TouchBar => Chrono_TouchBar; // Default touchbar settled to custom touchbar.
    
        //3. Set this touch bar to window in AppDelegate.cs DidFinishLaunching event:
    
          public override void DidFinishLaunching(NSNotification notification)
                {
    
                    // Insert code here to initialize your application
    
                    if (NSProcessInfo.ProcessInfo.OperatingSystemVersion.Major >= 10 && NSProcessInfo.ProcessInfo.OperatingSystemVersion.Minor >= 12 && NSProcessInfo.ProcessInfo.OperatingSystemVersion.PatchVersion >= 2) // If macOS  10.12.2 or newer. Touchbar support guaranteed.
                    {
                        NSApplication.SharedApplication.SetAutomaticCustomizeTouchBarMenuItemEnabled(true); // Enable custom touchbar.
    
                        NSApplication.SharedApplication.SetTouchBar(NSApplication.SharedApplication.MainWindow.ContentViewController.TouchBar);
                    }
    
    
                }
    

    That is all ! Thanks for support.

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    The NSTouchBar API is non-trivial to learn, and it involves returning identifiers and views from call backs. See this example for more details:

    https://github.com/xamarin/mac-samples/tree/master/TouchBarExample/TouchBarExample

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    If there is a way to do it via Xcode's designer, I'm currently unaware (though would be interested in seeing a sample).

  • berkbababerkbaba TRMember ✭✭

    Hi,

    I designed it via Xcode Interface Builder and it runs. However I cannot crate actions for button click.

  • berkbababerkbaba TRMember ✭✭
    Accepted Answer

    Hi, I found the solution :smile:

        //1. Set an outlet for View Controller's touch bar.
    
        [Outlet]
        AppKit.NSTouchBar Chrono_TouchBar { get; set; }
    
        //2. Override the toch bar in View Controller with your custom controller:
    
            public override NSTouchBar TouchBar => Chrono_TouchBar; // Default touchbar settled to custom touchbar.
    
        //3. Set this touch bar to window in AppDelegate.cs DidFinishLaunching event:
    
          public override void DidFinishLaunching(NSNotification notification)
                {
    
                    // Insert code here to initialize your application
    
                    if (NSProcessInfo.ProcessInfo.OperatingSystemVersion.Major >= 10 && NSProcessInfo.ProcessInfo.OperatingSystemVersion.Minor >= 12 && NSProcessInfo.ProcessInfo.OperatingSystemVersion.PatchVersion >= 2) // If macOS  10.12.2 or newer. Touchbar support guaranteed.
                    {
                        NSApplication.SharedApplication.SetAutomaticCustomizeTouchBarMenuItemEnabled(true); // Enable custom touchbar.
    
                        NSApplication.SharedApplication.SetTouchBar(NSApplication.SharedApplication.MainWindow.ContentViewController.TouchBar);
                    }
    
    
                }
    

    That is all ! Thanks for support.

Sign In or Register to comment.