NSImageView setImage causing NSView not correctly initialized - objective-c

I'm writing a networking program which sends an image from an iPhone to a Mac. I have the connection going fine, and passing data alright, but my problem is when I have the image sent over I try to display it and get the following warning:
NSImageView(0x101321fd0) - NSView not correctly initialized. Did you
forget to call super?
Here is the code, it's the "setImage" command throwing the error.
This is my first major venture into Objective-C, I'm worried I messed up something with the xib but I can't be sure.
imageView = [NSImageView alloc];
[imageView setImage:theImage];
NSLog(#"Should be able to see image now..");
If this is too vague for a direct answer then any tips, references or tutorials you can guide me to would be very welcome.
Thanks for your time.

You forget to init the imageView.
imageView = [[NSImageView alloc] initWithFrame:someFrame]

Related

Something very strange UITextView in iOS 7 ... not in iOS 6?

I'm creating a UITextView:
greetingTextView = [[UITextView alloc] initWithFrame:greetingRect];
Using it fine, but when the ViewController it is attached to deallocs I'm getting memory leaks ONLY in iOS 7 ? I'm even Nulling the greetingTextView out of desperation but to no effect:
[greetingTextView.undoManager removeAllActions];
greetingTextView.delegate = Nil;
[greetingTextView removeFromSuperview];
greetingTextView = Nil;
The leaks are in this image:
So it appears something to do with the UITextView UndoManager ? But why only in iOS 7 ?
Regards
I face a similar situation, and after hunting around and some trail and error, i noticed that when ARC is disabled for that particular file, the strange behavior stopped and no memory leaks occurred. check here for how to disable arc for a particular file
You'll need to empty out the undo manager if you want any object you add to it to be released.
Review the steps laid out in this document :
https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/UndoArchitecture/Articles/CleaningUndoStack.html
It did occurs to me once when I didn't specify the delegate of a UITextField. After the delegate was set everything went normal. Hope it helps. BTW, I'm using Storyboard.

Setting window root viewcontroller Bad access without stack trace

Edit: It seems this was a stupid mistake by me, i got a rotationChanged notification too early, before i could process it. That caused a chain reaction which caused a crash in some obscure location. Let me see how i can close/delete this question...
My app's been working fine for a while, but i decided today to finally implement it 'correctly', that is: Use a view controller instead of manually adding a single view to the UIWindow. This went quite smooth for a while, untill i tried setting window.rootViewController to my view controller.
Somewhere after applicationDidFinishLaunchingWithOptions, it crashes with a bad access. There's no useful stacktrace (it crashes in the assembly), and i can't see it getting back into my code anywhere (i placed logs all over the place). Just adding the viewcontroller's view to the main window works fine. This is the part of the code:
-(BOOL) application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
....
self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
self.viewController = [[MainViewController alloc]init];
// if i uncomment this it crashes:
// self.window.rootViewController = self.viewController;
[self.window addSubview: viewController.view]; // Commented out if i use the rootViewController
[self.window makeKeyAndVisible];
NSLog(#"Did finish launching");
return YES;
}
I've tried quite some debugging methods (stacktrace does not exist, stepping through the code manually takes forever, logs are not being called before the crash), but i can't find the problem. It crashes in libobjc.A.dylib'objc_retain:, and the only other thing in the stacktrace is objc_storeStrong. It's not a retain/release problem (at least not directly), since i'm using ARC. The final NSLog is called and it gets out of the didFinishLaunching if i step through manually.
I can get the app working without setting the root view controller, but the whole point of redoing this part was to do everything 'proper' (e.g. rotation has to be done relatively hackish without the root view controller).
so StackOverflow, please tell me you have some ideas! As this is driving me crazy :S
You don't need to do this:
[self.window addSubview: viewController.view];
When you set the view controller as root view controller, it's view already becomes subview of the window.
EDIT
However this wouldn't cause any problem.
Maybe the problem isn't in the code. Sometimes my apps crash too, and I get mad. Usually this happens because I drag the project from another folder and xcode finds troubles with permissions, or for some other (to me) unknown reasons.
Try to clean the project with alt+cmd+k . If that doesn't work you might have problems with permissions. So create another project and copy all the files you wrote to it.
It seems this was a stupid mistake by me, i got a rotationChanged notification too early, before i could process it. That caused a chain reaction which caused a crash in some obscure location. Thanks for the help anyway!

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?

Can I disable UIPickerView scroll sound?

I want to disable the annoying clicks that the UIPickerView generates upon scrolling up and down. Is there a way to do this? I want to play short sounds for each item that the picker view lands upon. It gets ruined by the built in sound.
I understand that the picker sounds can be turned off globally by switching off the keyboard sounds in iPhone/iPod settings. But is there a way to programatically do this?
Any help will be much appreciated!
Thanks
I've been struggling with a UIPickerView sound issue, and even though it's only partially relevant to the original question, I'm posting the problem/solution here because this topic keeps coming up in my search results so I think anyone else in the same boat may end up here too…
I needed to initialize a UIPickerView to restore the currently selected row from saved data. Simple, right? In viewDidLoad, just call the selectRow:inComponent:animated method of UIPickerView:
[myPicker selectRow:currentRowIndex inComponent:0 animated:NO];
This works as expected, but has a side effect that it generates a single "click" sound as if the user had scrolled the control. The click sound only occurs when running on a device (not the simulator), and only if the device has iOS 3.x installed (I tested with 3.1.3 and 3.2). This was apparently a bug in iOS that was fixed starting with iOS 4.0. But if you need to target Gen1 iPhone, you're stuck with iOS 3.1.3 where this problem is present.
I discussed the issue with Apple DTS, but they were unable to suggest any workaround other than upgrading to 4.0. I asked if they would make an exception and permit the use of the undocumented setSoundsEnabled mentioned above (which does actually solve the problem). The answer was, "There are no exceptions."
After some additional detective work, I discovered that you can prevent the sound from occurring by temporarily removing the UIPickerView from the superview, call selectRow, then re-add it to the superview. For example, in viewDidLoad:
UIView *superview = [myPicker superview];
[myPicker removeFromSuperview];
[myPicker reloadAllComponents];
[myPicker selectRow:currentRowIndex inComponent:0 animated:NO];
[superview addSubview:myPicker];
This gets rid of the extraneous click sound without using undocumented/private APIs so should pass Apple's approval process.
After using this specific undocumented api for over a year on the App Store Apple finally asked me to remove it from my App. It is very frustrating for audio apps to have that damn click sound. The best advice is to share with users that the picker sound can be disabled globally in the settings application under "Sounds" and setting "Keyboard Clicks" to "Off". I also strongly recommend visiting https://bugreport.apple.com/ and filing a bug for UIPickerView, as it can cause distortion in audio applications when the picker click is played.
they have just rejected an app of mine because the use of undocumented api's...thats one of them.
Someone I know says he got this past the App Store review just last week:
// Hide private API call from Apple static analyzer
SEL sse = NSSelectorFromString([NSString stringWithFormat:#"%#%#%#", #"set",#"Sounds",#"Enabled:"]);
if ([UIPickerView instancesRespondToSelector:sse]) {
IMP sseimp = [UIPickerView instanceMethodForSelector:sse];
sseimp(self.thePicker, sse, NO);
}
There is an undocumented way (I'm actually not sure if it is still available in iphone 3.0) but here it is any way
#import <UIKit/UIKit.h>
#interface SilintUIPickerView: UIPickerView
{ }
- (void) setSoundsEnabled: (BOOL) enabled;
#end
use this subclass instead and call [view setSoundsEnabled: NO]
I'm interested in knowing how it goes in the latest SDK, give it a shot and let us know.
Could this trick work? Someone was able to suppress the camera shutter sound effect by playing an inverted copy of the sound at the same moment: https://stackoverflow.com/a/23758876/214070
Maybe this not the answer for this particular question, but I had a similar problem - set minimumDate for datePicker, and I wanted set it without annoying "click" sound. After some time found very simple solution:
datePickerCustomTime.minimumDate = [[NSDate date] dateByAddingTimeInterval:300]// min time to set = now + 5 min
[datePickerCustomTime setDate:[[NSDate date] dateByAddingTimeInterval:300] animated:NO];
I found small quickie solution for this try below
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, yPickerView, VIEW_WIDTH, PICKERVIEW_HEIGHT)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
pickerView.alpha = 0.8f;
pickerView.tag = fieldTag;
[pickerView selectRow:pickerViewSelectedIndex inComponent:0 animated:NO];
set the animated:NO for selectRow: method