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.
Related
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.
In my .h file I have set properties for both the text field and the label:
#property (retain, nonatomic) IBOutlet UITextField *phoneNumberEntry;
#property (retain, nonatomic) IBOutlet UILabel *testLabel;
I also synthesized them in the .m file:
#synthesize phoneNumberEntry;
#synthesize testLabel;
how would I save the content of the textview to a variable, and then display it to a label?
WHat you need to do is create a string variable to hold the text of the UITextField.
NSString *textString = phoneNumberEntry.text;
then all you have to do is set the text of the label using the string variable just created
testLabel.text = textString;
EDIT:
If you're having trouble hooking it up in IB, just go to the files owner, and then click on the identity inspector, and find the box that says "class:" or something similar. THis is where you're going to type in your class. Once typed in, just hit enter, and then go to the connections inspector. This will allow you to see the properties you have created in your .h file.
Hope this helps!
There are 2 ways:
1). You need to put one UIButton and on button action event you may save textview value like:
- (IBAction)btnSaveClicked:(UIButton *)sender
{
strTextViewVal = txtTextView.text;
}
or you may save textview value in its delegate like this:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
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'm having problem passing data and executing functions in the viewcontroller from the view. I want to access label outlets in the viewcontroller from the view (yeah I know it might be bad structure of my app).
Got delegations working on UIPopovers but how can I set the delegate of the view to viewcontroller?
For example, you have a subclass of UIView. Let's name it SubClassUIView;
In another view, you want to use the data, from SubClassUIView.
So, your SubClassUIView should be done like this:
SubClassUIView.h
#interface SubClassUIView:UIView
#property (nonatomic, retain) UILabel* someLabelYouWantToUse;
#end.
SubClassUIView.m
#implementation SubClassUIView
#synthesize someLabelYouWantToUse;
#end.
And to access someLabelYouWantToUse
SubClassUIView* scView = [SubClassUIView alloc]init];
NSLog(#"%#", scView.someLabelYouWantToUse.text);
I've been through a bunch of Core Data examples and the Apple documentation. I'm at a wall after working on this all day.
All I want to happen is I type some text into a text field, save the file, open it again and see the text there.
I made a very very simple Core Data document-based app to experiment. Here are the particulars:
1) The data model has one Entity ("Note") with one attribute ("title") which is an NSString.
2) I created a view controller "ManagingViewController" that loads in a view called "NoteView" into a box in MyDocument.xib without a problem. NoteView.nib has just one NSTextField in it.
ManagingViewController.h
#import <Cocoa/Cocoa.h>
#import "Note.h"
#interface ManagingViewController : NSViewController {
NSManagedObjectContext *managedObjectContext;
IBOutlet NSTextField *title;
}
#property (retain) NSManagedObjectContext *managedObjectContext;
#property (retain, readwrite) NSTextField *title;
#end
and ManagingViewController.m
#import "ManagingViewController.h"
#import "Note.h"
#implementation ManagingViewController
#synthesize managedObjectContext;
#synthesize title;
- (id)init
{
if (![super initWithNibName:#"NoteView" bundle:nil]) {
return nil;
}
return self;
}
#end
I have a NSManagedObject called "Note.h"
#import <CoreData/CoreData.h>
#import "ManagingViewController.h"
#interface Note : NSManagedObject
{
}
#property (nonatomic, retain) NSString * title;
#end
and the .m file:
#import "Note.h"
#import "ManagingViewController.h"
#implementation Note
#dynamic title;
#end
In NoteView.nib my:
1) File's Owner is ManagingViewController and the IBOutlets to the Text Field and the view are connected.
2) I dragged over an NSObjectController object into the Interface Builder document window called "Note Object Controller". I set mode to "Entity" and the Entity Name to "Note". "Prepares content" and "Editable" are checked on. (All the examples I've done and been able to find use an NSArrayController here. I don't need an array controller right? I do want to be able to open multiple windows for the same app but I still don't think I need an arraycontroller? All the examples have a NSTableView and a add button. There's no need for an add button here since I don't have an NSTableView).
3) The NSTextView bindings for value I have it bound to "Note Object Controller" with a controller key of representedObject and a Model Key Path of title.
When I run my app I get
[<NSObjectController 0x20004c200> addObserver:<NSTextValueBinder 0x20009eee0>
forKeyPath:#"representedObject.title" options:0x0 context:0x20009f380] was
sent to an object that is not KVC-compliant for the "representedObject" property.
What am I doing wrong? I want to type in the text field, save the file, open it again and see the text there.
[<NSObjectController 0x20004c200> addObserver:<NSTextValueBinder 0x20009eee0> forKeyPath:#"representedObject.title" options:0x0 context:0x20009f380] was sent to an object that is not KVC-compliant for the "representedObject" property.
What am I doing wrong?
The error message tells you what you're doing wrong: You're trying to bind to the representedObject property of your object controller, but it doesn't have one. Binding to properties that don't exist cannot work.
The Note is the content object of the NSObjectController, so that's the controller key you need to bind to: content.