Referencing an array of IBOutlet 's (NSButton) - objective-c

I am making an application where there are 32 Check Box, and a NSTextField.
If the user clicks on the NSTextField the buttons shall assume the value that describes the binary rappresentation of this number.
No problem receiving the "clicked" action on the NSTextField, but for the buttons I have declared an array of 32 NSButtons:
#import <Foundation/Foundation.h>
#interface Handler : NSObject
{
#private
IBOutlet NSTextField* textField;
IBOutlet NSButton* bits[32]; // here are the buttons
}
- (void)awakeFromNib;
- (void) setTextField : (int) value;
- (int) getTextField;
#end
But when I try to link a Check Box with the IBOutlet "bits", I can't do it for each member.
So I can only make that array of 32 pointers to NSButton to one box.
I also show an image:
This is a problem for me, do I have to manually declare 32 different outlets with 32 different names?

You do not have to use 32 different IBOutlet references. You can declare an IBOutletCollection:
#property (retain, nonatomic) IBOutletCollection(NSButton) NSArray *buttons;
Use this to link them all up. Keep in mind that the order of the buttons is non-deterministic at runtime, meaning, you cannot guarantee that the buttons will be in any specific order when the app is running.

This is probably a good example of using an NSMatrix object.
You can add one button to your interface and then with the button selected in Xcode 4 go to Editor > Embed In > Matrix. Then you can option drag on a corner of the button to expand it into a matrix.
NSMatrix allows you to retrieve the cell values by searching for a given tag or by column/row coordinates.
HOW TO:
1) Embedding the NSButton object:
2) Option-Drag any of the button corners to expand the matrix:

I expanded it into a matrix.But graphically it looks like a single button.
This is what I get:

Related

Xcode Objective-C Mac: How to place object on top of AVPlayerView?

I'm currently working on a game. It plays a movie and the story changes depending on the players actions. I'm using quick time events (press a button in x seconds). When there's a quick time event I want to show the button that has to be pressed and the time left to press that button.
I play the movie(s) using an AVPlayerView. On top of that AVPlayerView I have placed a label. But the label is always invisible. It's because of the AVPlayerView.
So does anyone know a way around this?
Edit
I didn't use any code to place the label on top of the AVPlayerView. I just placed it there in the MainMenu.xib. When I view it in Xcode it looks like this:
But when I run the application it looks like this:
As you can see in the first image, all objects are under "View" and the label appears lower in the list so it should be on top of the AVPlayerView. Xcode displays the label on top of the AVPlayerView (image 1), but when I run the application the label is underneath the AVPlayerView (image 2). So my question was how to display the label on top of the AVPlayerView.
I hope this is enough information ;)
Link your label to an IBOutlet in the ViewController which describes your view and in the viewDidLoad() method write
/*top of your file*/
#property (strong, nonatomic) IBOutlet UILabel *myLabel;
/* other properties */
#implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*code*/
[self.view bringSubviewToFront:myLabel];
}
/*rest of your code*/
This should do the trick for you.

Populating a UILabel from a NSString

wow weird !!, I am passing a value NSString from my prepare for segue to another class. Ok, the string has a value and when passed to my -(void) the value is still there. I have an IBOutlet UILabel connected but the below code does not populate the UILabel. I am logging this from the void method.
2016-09-01 14:21:56.839 ApiAIDemo[1263:586780] The msgString has this in it {
speech = "The Moon is Earth's only permanent natural satellite. It is the fifth largest natural satellite in the Solar System, and the largest among planetary satellites relative to the size of the planet that it orbits. It is the second-densest satellite among those whose densities are known.";
}
Here is my property.
#property (weak, nonatomic) IBOutlet UILabel *myMsgLabel;
-(void)passedMsgFromVoicButtonVC:(NSString *)msgString{
**NSLog(#"The msgString has this in it %#",msgString);**
self.myMsgLabel.text = [NSString stringWithFormat:#"%#",msgString];
NSLog(#"BELOW the msgLabel has this in it %#",_myMsgLabel.text);
}
Create a non-outlet property in your controller to hold the string that's being passed in from the segue. Then update the outlet in viewDidLoad.
The problem is that segues are called before iOS has set up the controller's UI correctly.

Buttons in NSTableView to open file

I have an NStableView that is populated with an NSMutableArray that is built from a SQL data base... all good.
One cell displays a path to a locally stored pdf. I'd like to have that cell or text open up the pdf with Preview when clicked. The cell can be a button or just text- that's not really important (I'll do whatever is easiest) so long as it opens up Preview with the pdf. I know I can implement the following to call Preview
[[NSWorkspace sharedWorkspace] openFile:theUrl withApplication:#"Preview"];
The part I'm not sure of is how to make a cell call that action... That is, how do I turn a cell into a button where the contents of the cell (the path) is passed to the action as "theUrl"
Thanks in advance...
Create a subclass of NSTableCellView and add the following:
In your interface:
#property (nonatomic, strong) NSURL *url;
#property (nonatomic, weak) IBOutlet NSButton *urlButton;
In your implementation:
- (void)setURL:(NSURL *)url {
_url = url;
_urlButton.title = url.absoluteString ?: #"Missing url";
}
- (IBAction)didPressURLButton {
[[NSWorkspace sharedWorkspace] openFile:_url withApplication:#"Preview"];
}
In your storyboard/nib:
Design your NSTableView and NSTableCellView
Assign the above subclass to NSTableCellView and don't forget to set its identifier to something unique and connect its IBOutlet and IBAction
In your NSTableViewDataSource/Delegate:
Create a table cell with makeViewWithIdentifer: and pass in the identifier you assigned above.
Call setURL: on your tablecell for that row
This is more thorough than required, but once you know how to do it this way, you should be able to add and expand it to whatever other functionality you want to add.

put UILabel/UITextField on next line instead of pushing out of view

I'm creating a natural language form and whenever the user enters an input which is quite large, I want the entire line to move to the next line (just like an UITextView). Right now, I get this result:
This indicates two obvious problems: for one: the element the UITextField is pushing should go to the next line, and secondly, when spacing back, the element that was 'pushed away' does not get 'pushed back' into place. Also, the UITextField should move to the next line when exiting the view.bounds. It is arbitrary whether it's best to use a UITextField or UITextView for. It should be applicable to a situation in picture 2.
This is a more graphical approach to what I'm trying to achieve:
How do I solve this? And is this the right approach?
Update
The answer of Robert is very good one, next to some bugs that it still has there are is also the issue that it's not flexible. I've started refactoring the code and tried to subclass a UITextField and a UITextView, following the approach of Robert.
When subclassing the code there needs to be some delegation by the UITextField to the UITextView. Secondly, every part of the sentence needs to be split whenever there's a UITextField in between, but I feel like that can be hard coded into the VC as well. The constraints need to be converted to code as well.
Whenever I've got a solution to either one of all those problems I'll update the question and hopefully get to a flexible solution :)
Your approach works for me.
Let's say you have a UITextView that displays the selectable, but non-editable full sentence, including the entered parameter values. And then you have an editable UITextField for each form parameter. With this setup you can leave it to the UITextView to handle the text flow and use the UITextViews to handle the input.
In order to let the UITextField appear within the text flow, the trick is to hide it – or rather reduce it to the width of its caret – and display it at the position of last character of the parameter's value's.
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UITextView *fullTextView;
#property (strong, nonatomic) IBOutlet UITextField *friendField;
// Using AutoLayout constraints to position the friendField
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *friendFieldLeadingConstraint;
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *friendFieldTopConstraint;
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *friendFieldWidthConstraint;
#property (assign, nonatomic) CGFloat initialFriendFieldWidth;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Store the intrinsic size of the friendField displaying the placeholder
// (there's probably a better way to this than storing the value on view load)
self.initialFriendFieldWidth = self.friendField.intrinsicContentSize.width;
}
- (IBAction)friendFieldEditingChanged:(UITextField *)friendField {
// Insert the friend name into the sentence
NSString *sentencePart1 = #"I'm paying ";
NSString *sentencePart2 = #"\n$ amount\nfor description";
self.fullTextView.text = [#[sentencePart1, friendField.text, sentencePart2] componentsJoinedByString:#""];
// Render the fullTextView, so that we can retrieve the friend name's last character position
[self.fullTextView setNeedsLayout];
[self.fullTextView layoutIfNeeded];
// Retrieve the frame of the friend name's last character (in relation to the textView's coordinates)
UITextPosition *last = [self.fullTextView positionFromPosition:self.fullTextView.beginningOfDocument offset:sentencePart1.length + friendField.text.length];
UITextPosition *secondToLast = [self.fullTextView positionFromPosition:last offset:-1];
UITextRange *range = [self.fullTextView textRangeFromPosition:secondToLast toPosition:last];
CGRect rangeRect = [self.fullTextView firstRectForRange:range];
// Here comes the trick:
// The friendField's width will be reduced to the width of the caret and
// placed at the last character's position within the fullTextView.
// This way the UITextView handles the display of the full text,
// incl. the parameter values. And the UITextFields will handle the input,
// while only appearing as a caret.
// Retrieve the caret width
CGFloat width = [self.friendField caretRectForPosition:nil].size.width;
// If no text is entered, unfold friendField to reveal the placeholder
if (friendField.text.length == 0) {
width = self.initialFriendFieldWidth;
}
// Using AutoLayout constraints (see Main.storyboard)
// Note: A proper implementation needs to display the caret right where it is in
// relation to the input value. For now we only display it at the end of the value.
self.friendFieldLeadingConstraint.constant = - rangeRect.origin.x - rangeRect.size.width;
self.friendFieldTopConstraint.constant = - rangeRect.origin.y;
self.friendFieldWidthConstraint.constant = width;
}
It will look like this: (highlighting the textfield dimensions)
You can download a fully working example here: https://github.com/widescape/NaturalLanguageForm

Syntax error: IBOutletCollection

I am trying to declare an IBOutletCollection.
What I want to do is to manage multiple buttons in a way that I don't have to declare them one per one.
#import <Foundation/Foundation.h>
#interface WindowManager : NSObject
{
#private
IBOutlet NSTextField* textField;
IBOutletCollection (NSButton) NSArray* buttonsArray;
}
#property (nonatomic , retain) IBOutletCollection (NSButton) NSArray *buttonsArray;
- (void) awakeFromNib;
#end
I get these errors:
type name requires a specifier or qualifier
expected ; at end of declaration
And I don't understand the reason.
I want to have this situation: when the .xib file is loaded, I have all the button in a NSArray so that I am able to manage them.Everytime a button is pressed I want to know which button was pressed.
Is that possibile? The problem is that I find only iPhone guides, I found an example similar to this code on an iPhone guide, saying this thing works.
I would recommend using an IBAction instead:
-(IBAction)button_Clicked:(id)sender;
The reason is that the button that was pressed is the 'sender' of the press event. This way you will always know what button was actually pressed. If you want to make it even more identifiable, you can set the 'Tag' property for the buttons, so that your implementation file would look like this:
-(IBAction)button_Clicked:(id)sender {
switch([sender tag]) {
case 1:
// do stuff
break;
//etc.....
default:
break;
}
}
You can assign any number of controls to use the action. I hope this helps.