I need to create a Menu for a selected row of a table,
I am able to create by overriding
-(NSMenu*)menuForEvent:(NSEvent*)evt
Method, of a table, This particular Menu has two sub-menu, i am able to make the Sub-Menu
but facing Following Problem
1 -- To Add a Sub-Menu in a MenuItem, i need to get Sub-Menu from a different class/Interface, i am calling following method
pSubMenu = [CommonUIUtil GetCommonUIMenu:pSubMenu
ActionId:self
Selector:#selector(MyMenuAction)];
Where prototype of this function is as follow
+(NSMenu *)GetCommonStatusMenu:(NSMenu *)pMenu ActionId:(NSObject*)aDelegate Selector:(SEL)selector
Implementation as below,
// pStrArray is Array of String to have the Menu Title
for(int idx =0;idx<[pStrArray count];idx++)
{
pTempStr = [pStrArray objectAtIndex:idx];
pImage = [CommUIController CommonGetImage:[CommonUIUtil GetImageByStatus:pTempStr]];
[pImage setSize:NSMakeSize(20,20)];
pMenuItem =[[NSMenuItem alloc]init];
[pMenuItem setTitle:pTempStr];
// this should set the selector
[pMenuItem setAction:selector];
// Setting the target
[pMenuItem setTarget:aDelegate];
[pMenuItem setImage:pImage];
[pMenuItem setEnabled:YES];
[pMenu addItem:pMenuItem];
[pMenuItem release];
}
return pMenu;
I am able to see the Image, String on this Sub Menu, but the problem i am facing is this menu is not at all Enable, can anyone guide me , where i am making the mistake,
This function will return the Menu and Menu i will add as below,
pMenuItem = [pCTTableMenu itemWithTitle:#"Status"];
//status menu is the menu returned from the above function,
[pMainMenu setSubmenu:pStatusMenu forItem:pMenuItem];
Thanks in Advance :)
looks like, i am not passing the selector method properly, in fact i don't know how to pass function pointer in Cocoa, probably i am mixing Cocoa/Objective C and Normal C both :), corrected, it, in the method just creating the view and in the main class/interface assigning target and action
Related
I an new to macOS XCode Obj-C so bear with me. I am trying to use validateMenuItem to disable and enable specific menu items.
I currently have this working to disable and enable *all menu items:
BOOL g_bEnableAllMenus = YES;
and then I am using the validateMenuItem
- (BOOL)validateMenuItem:(NSMenuItem *)item
{
if( !g_bEnableAllMenus )
return NO;
return YES;
}
To test, I am calling g_bEnableAllMenus in awakeFromNib and it is disabling all my menu items correctly.
- (void)awakeFromNib {
g_bEnableAllMenus = NO;
}
What I am trying to do now is this scenario:
Instead of g_bEnableAllMenus disabling and enabling ALL menu items when g_bEnableAllMenus = YES/NO; is called, I want it to disable all menu items EXCEPT a list of several other menuItems under a menu called TestMenu.
Then I want this list of other menu items under TestMenu separately controlled with a different BOOL: so I can enable and disable this menu item separately and not have it changed by g_bEnableAllMenus - only by g_bEnableTestMenu.
BOOL g_bEnableTestMenu = YES;
There is where I am stuck. I think I have to use some combination of [item action] == #selector(TestMenuItem:) in validateMenuItem so when g_bEnableTestMenu == NO it disables all menu items under my TestMenu (but doesn't touch other menu items).
Then when g_bEnableAllMenus == NO it still disables all my menus (as it correctly does now) but excludes my TestMenu.
Here's a more concrete answer based on my comment above.
You can set a tag value on a menu item to avoid having to match by action/selector. If you're creating the menu items in code just set the .tag property directly; in an .xib layout set it on the main properties tab of the menu item:
Then in your validation handler you can just check the range of the tag:
func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
if item.tag >= 1000 && item.tag <= 1100 {
// return your test validation logic result
} else {
// return your normal validation result
}
}
Of course you could improve this sample by defining constants for your test-menu-item tag range min and max, or with other data structures (e.g. a CaseIterable enum of test-item tag values). You might want to use tags for other groups of commands that share validation logic that isn't readily determined by selectors/responder-chain.
The project have some views with different buttons. When I hide a view and show the other view, I can't get the focus on my button.
I think is related to setNeedsFocusUpdate. I have read the Apple doc. There is not any example.
Does anyone know how to do it and put an example (Objective C)?
You need to override preferredFocusedView, and when you are hiding one view and showing there call this method setNeedsFocusUpdate, your preferredFocusedView implementation should be something like this
- (UIView *)preferredFocusedView
{
// Add your logic here, it could be more complicated then what is below
if (view1.hidden)
{
return _button;
}
else
{
return _button2
}
}
And if you want to make custom view get focus, override canBecomeFocused method and return true
Edit
You can use add a breakpoint and execute this command po [buttonYouWantToFocus _whyIsThisViewNotFocusable] it will tell you why its not focusable.
If you are adding a sub view programmatically, maybe this is what you want:
- (UIView *)preferredFocusedView {
return [view1 preferredFocusedView];
}
I realize your question is specific to Objective-C but here is a way to solve for this in Swift. You need to override the preferredFocusedView property.
override var preferredFocusedView: UIView? {
guard primaryView.hidden == false else {
return secondaryView
}
return primaryView
}
Then just call setNeedsFocusUpdate() whenever an event happens that causes your views to be hidden. Hope this helps...
Another option (if you don't want to use preferredFocusedView) is, instead of setting your view to be hidden, simply remove it from it's superview, like so:
myView.removeFromSuperview()
This automatically takes the focus away from the button that is removed and gives it to another one that is still on screen.
I would like to mimic the Finder's predicatesList. Especially the LeftExpressions popup with its "other" (in German: "Andere...") menu entry and like to popup a NSSheet with a user selectable list of search predicates.
My approach was to create some NSPredicateEditorRowTemplates and one last custom rowTemplate with a leftExpression named "other...".
Then I override the templateViews method and added a separatorItem:
-(NSArray *)templateViews{
NSMutableArray * views = [[super templateViews] mutableCopy];
// I tried already to add here my custom menu entry, but if I add more templates my custom entry (and the separator line) is not fixed at the last index.
if (!isCustomMenuItemAdded) {
NSPopUpButton *leftButton = views[0];
// Add a menu separator
[[leftButton menu]insertItem:[NSMenuItem separatorItem] atIndex:[leftButton menu].itemArray.count-1];
}
return views;
}
My custom predicateEditor is now shown correctly, But if I click on the last menu item 'Other.." the dummy NSPredicateRowTemplate shows up.
I tried to override the -(id)copy method in my rowTemplate class to suppress the new line but that feels strange to me.
-(id)copy{
return nil; // OK, now there is no new row, but this throws an internal exception
}
My question is:
Is there a better way to add a custom menu entry in the left expressions popupButton?
and
How can I suppress that a new predicateTemplateRow is shown in the PredicateEditor?
Is there a way to set the starting selected segment in a UISegmentedControl in Interface Builder, or do I have to do it in the code? If it's in the code, is viewDidLoad the best place to set it?
From code, you can do:
self.segmentedControl.selectedSegmentIndex = someDefaultIndex
Whether you should set it in viewDidLoad: or not depends entirely on the structure of your application. For example, if your app is starting up and loading the view for the first time and needs to set the control to whatever value it had during the previous run of the app, then it definitely makes sense to do it there.
In Interface Builder when you select UISegmentedControl object on your UI, then in attributes pane, in segment control there's segment drop down menu, select segment that you want selected (0,1 and so on) and tick the 'selected' option below it.
Select default value for your UISegmentedControl via storyBoard
Open ur storyboard and select the UISegmentedControl
Select atributes inspector of your UISegmentedControl (in the right corner)
Search the 'segment' atribute and open de dropdown.
Select the item you want to be selected by default.
Mark the selected checkbox to set selected by default.
If you don't use storyboards and want to set a default index after some setup/networking like me, this little snippet will select something if the user hasn't. I placed this in my subclass of UISegmentedControl, but you could place this anywhere. (Swift 3)
Decl: var UISegmentedControlNoSegment: Int { get }
Desc: A segment index value indicating that there is no selected segment. See selectedSegmentIndex for further information.
Short version:
if selectedSegmentIndex == UISegmentedControlNoSegment {
selectedSegmentIndex = initialIndex
}
Longer version
func reloadData() {
guard let numberOfItems = dataSource?.numberOfItems() else {
return
}
removeAllSegments()
for index in 0...numberOfItems {
insertSegment(with: $image, at: index, animated: false)
}
if selectedSegmentIndex == UISegmentedControlNoSegment {
selectedSegmentIndex = initialIndex
}
}
After clicking the Segmented Control go to where you created the segments and choose the one you want be default. Then below that there will be a Box with "Selected" by it. Select that and it will be default.
I'm currently struggling to use UI elements in Interface Builder. I keep trying to do things "in the .NET way."
I have several buttons that all map down their TOUCH event to the SAME FUNCTION:
-(IBAction) onTouch:(id) sender
{
// do something with touch, DEPENDING ON WHAT BUTTON WAS PUSHED
// I want to do something like
if( sender.tag == "something" )
{
//...doesn't work on apple, of course..
}
}
I want to uniquely identify each BUTTON USING SOMETHING like the TAG property in .NET. I tried using the INTERFACE BUILDER "NAME" field that is on the "Identity" panel of interface builder, but I don't know how to access that field programmatically.
-(IBAction) onTouch:(id) sender
{
// do something with touch, DEPENDING ON WHAT BUTTON WAS PUSHED
// I want to do something like
if( sender.InterfaceBuilderName == "something" )
{
//...doesn't work..
}
}
So, WHAT / IS THERE a way to uniquely identify a UI element (such as a button) OTHER THAN doing something like
-(IBAction) onTouch:(id) sender
{
// look at
[sender currentTitle]
}
The reason that's bad is because if the text on the button changes for some cosmetic reason you break the whole app, right
The last solution I can think of is write seperate functions for each button's touch event but I really want to know if it is possible to uniquely identify a button by something similar to .Net's TAG property.
In the iPhone SDK all UIView objects have a property also called tag which is an integer value and can basically be used to do what you are intending.
I usually define a constant for the tag values I'm going to use for a specific purpose.
You can access the tag on the button object:
myButton.tag = MYBUTTON_TAG_CONSTANT
// button tag constant
#define MYBUTTON_TAG_CONSTANT 1
For buttons, there is a Tag entry in the View section (click on your button, select Attributes Inspector from the Tools menu). You can then use this integer value in your code.
Here is a link that may help as well:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/25582-using-tags-interface-builder.html
UIView's tag property is accessible from Interface Builder. Unlike .NET, it's an integer rather than a string.