Populating a UILabel from a NSString - objective-c

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.

Related

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.

Changing image in different view

I am attempting to change the character image in the game that I am creating but I am having some issues. I have a view controller where I will have all of the characters as their own buttons, and by selecting a button it should change the character image in the GameViewController
change.h
- (IBAction)charOne:(id)sender;
change.m
- (IBAction)charOne:(id)sender
{
GameViewController *changeView = [GameViewController game];
UIImageView *Romo = [GameView changeView];
[ChangeView setImage:[UIImage imageNamed:#"testOne.png"]];
NSLog(#"Test");
}
GameViewController.m
{
IBOutlet UIImageView *changeView;
}
#property (strong, nonatomic) IBOutlet UIImageView *changeView;
This code isn't working for me, I do not receive an error message but nothing is changed after the image is selected.
On the GameViewController the UIImage name that I am attempting to change the image for is named Romo.
Any help will be appreciated
try :
[Romo setImage:[UIImage imageNamed:#"testOne.png"]];
instead of :
[ChangeView setImage:[UIImage imageNamed:#"testOne.png"]];
You're calling the setImage method on a class rather than an instance of a class. Since Romo is the instance of UIImageView you want to affect (which is a reference pointer to the UIImageView on your game view controller), that should be the instance you affect.
Also, bad practice to name instances starting with a capital letter -- they turn out looking like classes.

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

Referencing an array of IBOutlet 's (NSButton)

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:

Can I use properties to determine the class of an object?

I have a custom UITableViewCell created in IB.
All my cells will have this general layout. However,in some cells, the two white views will be classA and in other cells the two white views will be classB. (Both subclasses of UIView). I tried to assign the specific subclass of the two views using properties.
I set the two views as properties of the cell
#property (nonatomic, retain) IBOutlet UIView *leftView;
#property (nonatomic, retain) IBOutlet UIView *rightView;
And in my view controller where I make the table view, I tried creating an object of type subclassA and assigning it as leftView, so that leftView will be of type subclassA. In other cells, I would create an object of subclass B and set it to leftview, so that in those cells, left view would be of subclass b.
//equationTextField is a subclass of UIView
EquationTextField *textField = [[EquationTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; // arbitrary frame
cell.leftView = textField;
This code doesn't work, however. Is there a better way to assign the specific class of my views?
What you are doing is fine, but you may want to investigate having two different cell types (with your custom views already embedded) to improve performance - each could have a different reuse identifier which you would choose depending on the index path. Creating views every time in cellForRowAtIndexPath is never a good idea.
You could even derive both types from the same cell, just use the different reuse identifier and add in the correct subclasses only when first creating the cell.
You can check to see if cell.leftView is your EquationTextField if needed:
if([cell.leftView isKindOfClass:[EquationTextField class]]) {
NSLog(#"View is EquationTextField");
}
else {
NSLog(#"View is not EquationTextField");
}