I'm trying to pass an image between 2 different views that are added as subclasses to the MainCanvasController.
the image seems to get passed (it is shown, when printed to the Console) but it doesn't display anything...
this is how I try to receive and display the image
-(void)receiveNumber:(C4Image*)number{
C4Log(#"number:%#", number);
number.center=self.canvas.center;
[self.canvas addImage:number];
receivedImage=number;
C4Log(#"received number: %#", receivedImage);
}
and here is how I post the image
[secondView receiveNumber:originalImage];
I don't really know what's going wrong. (well, honestly, I don't know at all...) So any hints are very much appreciated!
I had a look at your project and found the answer.
Your FirstView object has a variable called secondView, which is exactly the same name as the object in your main workspace. However, despite having the same name both of these are different objects.
I've done a couple things:
1) instead of using variables in the interface file for your objects, use properties.
2) create a SecondView property in your FirstView class
3) set the property of firstView to the same object in your workspace, secondView
My FirstView.h looks like:
#interface FirstView : C4CanvasController{
C4Label *goToSecondView;
}
#property (readwrite, strong) C4Window *mainCanvas;
#property (readwrite, strong) C4Image *postedImage;
#property (readwrite, strong) SecondView *secondView;
#end
My postNoti: looks like:
-(void)postNoti{
C4Log(#"tapped");
[self postNotification:#"changeToSecond"];
[self.secondView receiveNumber:self.postedImage];
}
NOTE I got rid of the [SecondView new]; line.
The following is the part that you were missing
My workspace has the following line:
firstView.secondView = secondView;
Which sets the variable of the first view to have a reference to the secondView object.
You hadn't done this, and so you were passing the image to an object that had the same name as the view that's visible from your main workspace's canvas.
Related
I've been struggling with trying to create an NSCollectionView that has a set of NSCollectionViewItems with a custom view. The code works fine when the controls on the item view are standard AppKit controls, but once I add a custom NSView, there's no way to bind it from Interface Builder.
From spending some hours searching the internet, there appears to be a lot of options to solve this but all seem specialised. Is there some simple example code that demonstrates how, given a CustomImage * on the item view, to set the image property on that custom view?
The model that provides data for each item is:
#interface MyItem : NSObject
#property (retain, readwrite) NSImage * image;
#property (retain, readwrite) NSString * name;
#end
The NSCollectionViewItem subclass is:
#interface MyCollectionViewItem : NSCollectionViewItem
// Properties
#property (strong) IBOutlet NSTextField * name;
#property (strong) IBOutlet CustomImage * image;
#end
where CustomImage is simply a subclass of NSImageView.
I tried subclassing NSCollectionView and overriding newItemForRepresentedObject as some answers suggested and assigning there:
MyItem * item = (MyItem *)object;
MyCollectionViewItem * newItem = (MyCollectionViewItem *)[super newItemForRepresentedObject:object];
NSView *view = [newItem view];
[view bind:#"name" toObject:item withKeyPath:#"name" options:nil];
[view bind:#"image" toObject:item withKeyPath:#"image" options:nil];
return newItem;
but this just blew up in the bind call with an error that 'name' doesn't exist.
This should, in theory, be an extremely simple thing to solve but none of the answers I've found make this clear. An alternative would be to ditch NSCollectionView and use one of the simpler replacements on GitHub but I'd like to have a last attempt to see if this is solvable first.
Thanks!
How did you add the CustomImage instance to the item view?
If you drag a "Custom View" in and then change the class, IB doesn't treat it like an NSImageView.
However, if you drag out an NSImageView and then change the class, IB should still treat it like an NSImageView and you should be able to bind its bindings like normal. In that case, you can bind its Value binding to the collection view item, model key path "representedObject.image".
i am trying to archiv something really simple.
I add a property for a NSScrollView in my ViewController header file called PanelController:
#property (strong) IBOutlet NSScrollView *listurls_fld;
I add the ViewController.h file to my NSObject Interface called "qhandler.h"
#import "handler.h"
#import "PanelController.h"
i have a +(void) function inside the qhandler.m ->
+ (void)do_handle:(NSDictionary *)response
{
PanelController *MyView=[[PanelController alloc] init];
NSLog(#"add moo");
[MyView.listurls_fld setStringValue:#"moo"];
}
which doesn't work...
It does neither work with setAlphaValue or whatever, i guess it's because i am allocating a new instance of PanelController, but as a matter of fact, I tried to change the main instance.
I know it's basic but i have enormous problems using IBOutlets from a viewcontroller, inside an external obj-c file.
Thanks,
john
ViewController.h
id mainDelegate;
ViewController.m
in viewDidLoad oder what function ever triggers after load:
mainDelegate=self;
so i can use [mainDelegate ...:..]; in every file..
I just started Mac programming coming over from iOS and was playing around with bindings.
I'm trying to make a simple directory popup that shows a history of recently selected directories and last element would read other... which will open the opendialog box.
I can't seem to figure out how to Bind an NSPopupButton to my Model though.
Its setup like this:
MainUIViewController, NSController, NSObject Controller all wired up in the nib
I do connect an outlet in MainUIViewController to the Directory Array Controller in the NIB
I have a class for eachDirectory, and a class for DirectoryArrayController(NSObject) I bind the NSPopupButton on view this way:
and I have the Directory Array Controller bound to the Directory Popup Array Controller thus
Here is the .h file that is connected to the Directory Popup Array Controller
#interface DirectoryPopupArrayController : NSObject
#property (weak) IBOutlet NSPopUpButton *directoryPopupButton;
#property (nonatomic) IBOutlet NSMutableArray *allDirectoryHistory;
#property (nonatomic) eachDirectory *currentlySelectedDirectory;
#end
I fill some sample directory info with the following code in the corresponding .m file
- (void)awakeFromNib {
[super awakeFromNib];
//testing sample directories
self.allDirectoryHistory = [[NSMutableArray alloc] initWithCapacity:10];
NSString *name;
eachDirectory *newDirectoryName;
for (int i = 0; i < 5; i++) {
name = [NSString stringWithFormat:#"directory %d", i];
newDirectoryName = [[eachDirectory alloc] initWithDirectoryName:name];
[self.allDirectoryHistory addObject:newDirectoryName];
}
}
and here is the code for the eachDirectory.h
#interface eachDirectory : NSObject
#property (nonatomic) NSString *directoryPath;
#property (nonatomic) NSString *directoryVisibleName;
-(id) initWithDirectoryName:(NSString *)newName;
#end
Now When I go to my code if I place the code for creation of the Array and bind the Array controller directly to the UIViewController.m file things seem to work fine.
What I want to do is handle all the array stuff in a separate class file and only get back the final directory choice to the main controller. When I bind the NSArrayController to the Object controller in the NIB as described above I get nothing showing in the popup and I don't understand why!
Any Help is greatly appreciated, Sorry for the long-winded post - just wanted to make myself clear.
Ok, so I had an IBOutlet of a UIImageView that looks like this:
.h
#property (nonatomic,retain) IBOutlet UIImageView *ground;
Then I can Use the variable like this "ground.frame" And in my code looks like this:
.m
if (CGRectIntersectsRect(player.frame,ground.frame)) {
"STUFF"
}
This works fine, but then I found out that I needed an "IBOutletCollection". I changed the .h code for this:
.h
#property (nonatomic,retain) IBOutletCollection(UIImageView) NSArray *ground;
Now I dont know how to make the other to work since it gives me an error telling me "frame is not found on object of type NSArray."
So my question is how can I change the code in the .m file so that it works the same way as before but now as an array?
Thanks
The array is a collection of UIImageViews so you take an object off the array and query that.
Maybe...
for(UIImageView *xground in ground)
{
if (CGRectIntersectsRect(player.frame,xground.frame)) {
//do stuff
break;
}
}
You ask each view in the array about its frame.
If you want to be really paranoid run a class check on the object to be sure it's an UIImageView coming off the array, but probably not necessary as your have already declared it as an array of UIImageView.
Got a little problem i asked about it before but maybe i didnt ask properly.
I have a cocoa application, which amongst other things, must do the following task:
- load some images from the disk, store them in an array and display them in a custom view.
In the Interface Builder i have a CustomView and an OBJECT that points to TexturesController.h
The custom view is a custom class, TextureBrowser.
Below is the code for the controller and view:
TexturesController
#import <Cocoa/Cocoa.h>
#class TextureBrowser;
#interface TexturesController : NSObject {
IBOutlet NSTextField *logWindow;
IBOutlet TextureBrowser *textureView;
NSMutableArray *textureList;
}
#property textureView;
-(IBAction)loadTextures:(id)sender;
-(IBAction)showTexturesInfo:(id)sender;
#end
TextureBrowser
#interface TextureBrowser : NSView {
NSMutableArray *textures;
}
#property NSMutableArray *textures;
-(void)loadTextureList:(NSMutableArray *)source;
#end
These are just the headers. Now , what i need to do is:
when loadTextures from the TexturesController is called, after i load the images i want to send this data to the view (TextureBrowser), for example, store it in the NSMutableArray *textures.
I tried using the -(void)loadTextureList:(NSMutableArray*)source method from the view, but in the TextureController.m i get a warning : No -loadTextureList method found
This is how i call the method :
[textureView loadTextureList: textureList];
And even if i run it with the warning left there, the array in the view class doesnt get initialised.
Maybe im missing something...maybe someone can give a simple example of what i need to do and how to do it (code).
Thanks in advance.
In TexturesController.m, you have to import TextureBrowser.h so that the controller knows what methods the property textureView has. Right now, you've just got a blank placeholder symbol instead of an actual class.
Since textureView is defined by an outlet, you need to make sure that its class is properly set in Interface Builder. If you provide a generic NSView instead, it won't have the loadTextureList: method.
If you imported TextureBrowser.h in TexturesController.m, I don't see why it wouldn't find your method.
But why don't you simply call self.textureView.textures = textureList; from the TexturesController ?