Problems with NSTextView where NSTextField worked - objective-c

I'm just starting out with obj-c, I'm trying to build an importer to fetch a couple numbers from a piece of formatted text. I started with a wrapping TextField and was able to both get the text into a string and search it as I wanted with
NSString *varImport = [NSString stringWithString:[importTextView stringValue]];
When I switch over to the TextView in the Interface Builder I get there error
-[NSScrollView string]: unrecognized selector sent to instance 0x100429160
I think this may be the root of my problem, although I am dragging over a TextView when I look in the inspector panel its labeled as a ScrollView which I'm not familure with yet.
Through my research I came across two different sites saying that TextView wont directly feed into a string but it was for odd reasons, IE TextView stored the data as a MutableString that was constantly updating and to access it you had to copy the original (Second example) Anyways I'm turning to the experts because I'm clearly doing something wrong and I can't make sense of the answers on the web.
NSString *varImport = [NSString stringWithString:[[import textStorage] string]];
NSString *varImport = [[import string] copy];
Don't worry so much about the other stuff if you don't have the time to explain what's going in the web examples, I mainly want to know about the ScrollView stuff and how to get a string out of it to be able to search it.
Thanks in advance!
Graham

Simply because you are using the wrong method.
NSString *varImport = [NSString stringWithFormat:[importTextView string];
Also, you might have connected the importTextView instance variable to the wrong connection. A NSTextView is always in a NSScrollView. Simply just right-click your object from interface builder and drag the instances to the top of the textview (Where it should show "NSTextView").
Save the nib and try run it again. It should work.

To get text value from a text field or text view you should use - (NSString *)string method.

Related

What is the simplest way to display a NSString value?

I'm trying to display a variable value to the IPhone screen. I'm using XCode and the simulator. I can use NSLog to print the output to the debug screen but I'm at a loss to understand how to get the same output to the actual screen.
What I have now in main.m.
NSString *testvalue = #"Test";
NSLog(#"Output %#\n", testvalue);
My understanding is that all display operations occur in viewcontroller.m but from what I have been reading ,it seems that the action is not as simple as creating debug output.
You'll need to put some sort of UI control on the screen with the capability of displaying text. UILabel would probably be the simplest. Then set the content of the label to the text you want to display.

Longpress On UI Element (Or How To Determine Which Element Is Pressed?)

I have an app that has a rather large help section in PDF form. It's getting to me that the PDF is so large and I can tell in instruments that it's putting a little too much strain on the processor, so I'm going to phase it out.
I figured, a lighter implementation would be using a UILongPressGestureRecognizer that I could attach to every UI element that would display specified text in maybe a popover or a UIMenuController indicating the function of the selected element.
So my question is this: How can I attach something like a tag to EVERY element in the view so it can be passed to a single method? When I tried tags, I found there was no way to access it through the (id)sender section of my method signature and thus there was no way to differentiate between the elements.
EDIT: to the person below: While you have solved my facepalm of a question as to determining the tags of views, how might one go about attaching gesture recognizers to a UIBarButtonItem so as to ascertain it's tag? Your implementations allow for a very nasty unrecognized selector because UIGestureRecognizers dont have a tag property.
You can derive the tag from an object passed in as the sender. Just have to check it's class and cast it appropriately. tag is a UIView property so we'll start there.
- (void)someMethod:(id)sender
{
if (![sender isKindOfClass:[UIView class]])
return;
UIView *senderView = (UIView *)sender;
NSInteger tag = senderView.tag;
}
You can access tags through the -(IBAction)xxxxxx:(id)sender; like so:
NSInteger tagValue = [sender tag];
But why can't you just connect the actions to your elements through Interface Builder?
What are the UI elements your using here?

Scrolling process status window

As part of a Mac application I am working on, the user fills out a screen full of stuff and then presses a 'process' button. There are edits performed and if everything passes the edit, a couple of minute process is performed which either end ok or not. I would like to have that process spit out a series of status and processing messages into a separate scrolling window so that if something goes bad, the user can go back through the log and see if anything shows up there.
What would be the best objects and methods for me to review and use for this type of processing?
Added 11/24/2011
As per the first suggestion, I created a second XIB, created a NSWindowController to match and put it all together as some prep work. When the button in pressed in the app delegate, I have this thing do the following:
- (IBAction)runButtonPressed:(id)sender {
RunResultWindow *wc;
wc = [[RunResultWindow alloc] initWithWindowNibName:#"RunResultWindow"];
[wc showWindow:self];
}
RunResultWindow is the name of the XIB and the NSWindowController class that controls it. I also added a finish button and wired that up with the intention of having the results of the process fill up the text window and then hang there until the user presses 'done' or 'finish' or whatever I wind up calling the button.
It actually shows the window when I press the button on the main window but when the code for the button finishes, the window vanishes. Clearly I am leaving out (an important) step.
Once I get the window then I can add the text view etc.... and get that working. What I would like is for the new Window to get focus and then close out when the user presses the 'done' button.
Additionally, I got the window for the window controller from the window method (it returned an address) and tried a couple of window focus methods in the windowDidLoad method of the NSWindowController but no dice.
Thanks again for whatever info I can get on this.
Added 11/25/2011
Duh. Maybe if I make the class instance an ivar instead of embedding it in the button method it will work and, lo, it did. Le Oops.
Sounds like you want to drop a NSTextView into a window where one can select & scroll the text but not edit the contents.
You can insert text as easily as using the insertText: method.
So... The NSTextView and insertText combination worked out somewhat ok but I don't' think it is the final answer. First, my understanding is that insertText is really only meant for user input and not background 'system' input to a NXTextStorage object. I'm not sure why but that's fine so I'll avoid it. There are other options. I did find the beginEdit and endEdit methods and it works pretty much about the same way though I have some more work to do on some detail delegate methods.
The part that doesn't work so well is getting the NSScrollView in the window to update on demand. I do the beginEdit and endEdit stuff and am able to update the NSTextStorage object properly. I can do this multiple times in the same method (a test button on the window containing the scroll view). I can tell that because print-object in debug shows me what I'm expecting at the right times. However, I'd like to be able to show an updated NSScrollView multiple times during the court of the windowDidLoad method. The scroll view updates properly when the button push method ends.
Here is some sample code. I do mix insertText and the begin/end edit methods in here but it was more of a test thing than any code I would use for real....
(IBAction)FinishButtonPush:(id)sender {
NSString *teststring;
teststring = [NSString stringWithString: #"show"];
[RunResultWindowTextView setString:teststring];
teststring = [NSString stringWithString: #"show show"];
[RunResultWindowTextView setString:teststring];
teststring = [NSString stringWithString: #"show show show show"];
[RunResultWindowTextView setString:teststring];
[RunResultWindowTextView insertText:#"123"];
NSTextStorage *tempTextStorage;
tempTextStorage = [RunResultWindowTextView textStorage];
[tempTextStorage beginEditing];
[tempTextStorage replaceCharactersInRange:NSMakeRange(5,13)
withString:#"Hello to you!"];
[tempTextStorage endEditing];
[tempTextStorage beginEditing];
[tempTextStorage replaceCharactersInRange:NSMakeRange(10,13)
withString:#"second change"];
[tempTextStorage endEditing];
[RunResultWindowTextView insertText:#"xxx123"];
[RunResultWindowTextView insertText:#"xxx123567"];
}
Even though the NSTextStorage object is updated properly, the scroll view only updates when the method completes. My understanding is that processEdit is called automatically during endEdit. I added processEdit in there just to see and all I got was either abends or no change depending on where I put the command.
This got deleted and I'm not sure why. If you're going to gong the post, please let me know why you did so. Can't improve my post unless I have an idea what was wrong with it....

Cocoa button won't display image

Just started exploring Cocoa so pretty much a total noob.
I've written a very simple game. Set it up in Interface Builder and got it working fine.
It contains a number of buttons and I'm now trying to get the buttons to display images.
To start with I'm trying to get an image displayed on just one of the buttons which is called tile0 .
The image file (it's nothing but a green square at the moment, but I'm just trying to get that working before I attempt anything more exotic) is sitting in the same directory as the class file which controls the game.
I have the following code sitting in my wakeFromNib method:
NSString *myImageFileName = [[NSString alloc] init];
myImageFileName = #"greenImage.jpg";
NSImage *myImage = [[NSImage alloc] initByReferencingFile:myImageFileName];
[tile0 setImage: myImage];
Trouble is, the game runs fine, but the image isn't appearing on my button.
Is there someone who could kindly tell me if I'm doing something obviously wrong?
Many Thanks.
The first problem is you allocate a string but then replace it with a string constant. That isn't causing your image problem but it is a memory leak.
I've never used initByReferencingFile, I usually just use imageNamed:. There is an example of using imageNamed here.

Changing an NSImage in XCode - this line of code not working

I am having a problem that is eating me alive. I really hope I am just missing something small here. It appears to be a rather "n00b" issue.
I have a blank NSImageView that I want to display a picture when a button is pressed — simple as that.
Here is my line of coding
NSBundle *mb = [NSBundle mainBundle];
NSString *fp = [mb pathForResource:#"tiles" ofType:#"PNG"];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:fp];
if ( [image isValid] ) {
[selection setImage:image];
[selection setImageScaling:NSScaleProportionally];
}
Whereas,
tiles.PNG is a resource in my bundle
and if [image isValid] is satisfied, because I've inserted dummy code into the clause and had that work
selection is defined in my header file as follows
IBOutlet NSImageView *selection;
It is also linked up to the application delegate in IB.
I have a feeling I might not be linking it properly?
WHy wouldn't the image display? If anyone can see an error - or provide me with working code - I would be soooooo thankful
Brian
It's not a linking issue—your app wouldn't even launch (assuming it even links successfully) if you'd failed to link against Cocoa or AppKit.
More probably, either you haven't connected the outlet to your image view in your nib, or you haven't loaded the nib yet. The way to check this would be to NSLog the value of the imageView pointer, using the %p formatter.
I had a similar issue where my view wasn't displaying, and it turned out that the view was hidden. This was a setting in the view properties in Interface Builder. Just a punt, but give it a go.
You need to use the debugger and see what's going on as it runs. Is fp nil? Is image nil? Is selection nil? The debugger is your friend.
did you remember to send -setNeedsDisplay to the NSImageView after you set the image?