Can't load an NSMutableArray with UIView objects - xcode

On my game board (which is called GameViewController), I have six Seats (in the xib) which are nothing more than subclassed UIViews (so I also have Seat.h and .m files in my project). When the game board gets initialized these seats also get created (thanks to the xib). I want to have the seats loaded into an NSMutableArray so that I can use them later. For some reason I can't get it to work.
In my GameViewController header file I've added NSMutableArray *seats; as an instance variable and included #class Seat; above the interface declaration.
In my awakeFromNib method of the GameViewController I have seats = [[NSMutableArray arrayWithCapacity:6] retain]; So the array should be initialized when the game board appears.
Then in my Seat header file I've included GameViewController *controller; as an instance variable and included #class GameViewController above the interface declaration. I've also added and synthesized a property for the GameViewController.
In the Seat's awakeFromNib method I have [controller registerSeat:self];
This calls a method in my GameViewController that has only one line: [seats addObject:seat]; This should add the seat to the array. But for some reason this method never seems to get called. Or if it does, I can never tell. When I debug, focus never goes to the registerSeat method even though the seats do get added to the board. I hope this all makes sense. If the code is needed, I can provide it. It might be easier to do that anyway. What do you guys think? I'm stumped at the moment.
The method declaration is as follows:
- (void) registerSeat:(Seat *)seat;
GameViewController.h:
#import <UIKit/UIKit.h>
#class Seat;
#interface GameViewController : UIViewController {
NSMutableArray *seats;
}
- (void) registerSeat:(Seat *)seat;
#end
GameViewController.m
#import "GameViewController.h"
#import "Seat.h"
#implementation GameViewController
- (void)awakeFromNib {
seats = [[NSMutableArray arrayWithCapacity:6] retain];
}
- (void) registerSeat:(Seat *)seat {
[seats addObject:seat];
NSLog(#"seat has been registered");
}
#end
Seat.h:
#import <UIKit/UIKit.h>
#class GameViewController;
#interface Seat : UIView {
GameViewController *controller;
}
#property (nonatomic, retain) IBOutlet GameViewController *controller;
#end
Seat.m:
#import "Seat.h"
#import "GameViewController.h"
#implementation Seat
#synthesize controller;
- (void) awakeFromNib {
[controller registerSeat:self];
}
#end

What you're doing here is what Xcode 4's IBOutletCollection is designed to solve. In your NIB, select all of your Seat views (these should be called SeatView, BTW. Seat should be a model class, not a view class). Drag the selected group to your GameViewController header and request an IBOutletCollection. This will wire them all as a random-ordered array. (Why they chose to make it a random-ordered array is beyond me; it's a somewhat insane construct, but it exactly matches what you're trying to do above.)
As #highlycaffeinated notes, the most likely reason for your current code failing is that you've failed to wire your controller and it's nil. When "nothing happens" in Objective-C, it's almost always because you're talking to nil.

Related

allocating view controller the right way

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..

Outlets and instance methods

I have a little problem and hope that you can help me.
I want to call a instance method of a subclassed window and set the user interface up there:
//AppDelegate.h
#import <Cocoa/Cocoa.h>
#class MainView;//The main window
#interface DownloadedAppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet MainView*mainview;//the objects are in the same nib, outlet connected with the window
}
#property(nonatomic,retain) IBOutlet MainView*mainview;
#end
.
//AppDelegate.m
#import "MainView.h"
#synthesize mainview;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[mainview launched];//But sometimes this code fails, I don't know why
//launched sets up the interface
}
-(void)dealloc {
mainview=nil;
}
MainView belongs to NSWindow.
Is there something wrong or something to improve? Should I build up the UI somewhere else? Do you know why this code does not work always?
Try putting
[mainview launched];
in
-(void)awakeFromNib {
}
Use the debugger! Is launched even getting called?
Set a break point at the launched call and look at the value of mainview. It is nil? This is because outlets are not guaranteed to be connected until awakeFromNib.

Getting around IBActions limited scope

I have an NSCollectionView and the view is an NSBox with a label and an NSButton. I want a double click or a click of the NSButton to tell the controller to perform an action with the represented object of the NSCollectionViewItem. The Item View is has been subclassed, the code is as follows:
#import <Cocoa/Cocoa.h>
#import "WizardItem.h"
#interface WizardItemView : NSBox {
id delegate;
IBOutlet NSCollectionViewItem * viewItem;
WizardItem * wizardItem;
}
#property(readwrite,retain) WizardItem * wizardItem;
#property(readwrite,retain) id delegate;
-(IBAction)start:(id)sender;
#end
#import "WizardItemView.h"
#implementation WizardItemView
#synthesize wizardItem, delegate;
-(void)awakeFromNib {
[self bind:#"wizardItem" toObject:viewItem withKeyPath:#"representedObject" options:nil];
}
-(void)mouseDown:(NSEvent *)event {
[super mouseDown:event];
if([event clickCount] > 1) {
[delegate performAction:[wizardItem action]];
}
}
-(IBAction)start:(id)sender {
[delegate performAction:[wizardItem action]];
}
#end
The problem I've run into is that as an IBAction, the only things in the scope of -start are the things that have been bound in IB, so delegate and viewItem. This means that I cannot get at the represented object to send it to the delegate.
Is there a way around this limited scope or a better way or getting hold of the represented object?
Thanks.
Firstly, you almost never need to subclass views.
Bind doesn't do what you think - you want addObserver:forKeyPath:options:context: (You should try to understand what -bind is for tho ).
When you say "the key seems to be it being the "prototype" view for an NSCollectionViewItem" I think you are really confused…
Forget IBOutlet & IBAction - they don't mean anything if you are not Interface Builder. "Prototype" means nothing in Objective-c.
The two methods in the view do not have different scope in any way - there is no difference between them at all. They are both methods, equivalent in every way apart from their names (and of course the code they contain).
If wizardItem is null in -start but has a value in -mouseDown this is wholly to do with the timing that they are called. You either have an object that is going away too soon or isn't yet created at a point you think it is.
Are you familiar with NSZombie? You will find it very useful.

Unable to write to NSTextField from Model Controller object

After always running monolithic blocks of code from within my AppController object, I've just learned how to modularize my code using separate model controller objects (so much neater and less confusing to work with :-))
My simple test app has a main AppController and two model objects (Model1 and Model2). I can successfully pass data from the AppController to the models, and the models themselves can run methods and process the passed data 'internally' as they were intended to do -- but I can't get them to communicate with a darned NSTextField in the UI. Here's the relevant parts of my code:
In AppController.m #import "AppController.h"
#implementation AppController
- (IBAction)passObjectsToModelController:(id)sender
{
NSString *stringToPass = #"Hello from Model2 :-)";
int numToPass=12345;
Model2 *ObjController2 = [[Model2 alloc]initWithStuff:stringToPass:numToPass];
[ObjController2 release];
}
#end
...in Model2.h
#import
#interface Model2 : NSObject
{
IBOutlet NSTextField *passedStringField;
}
- (id)initWithStuff:(NSString*)passedString :(int)passedNum;
#end
...and finally in Model2.m
#import "Model2.h"
#implementation Model2
- (id)initWithStuff:(NSString*)passedString :(int)passedNum
{
if(self = [super init])
{
NSLog(#"now inside 'Model2' controller...");
NSLog(#"the passed string reads: %#",passedString); //••• this works •••
NSLog(#"the passed number is:%d",passedNum); //••• this works •••
[passedStringField setStringValue:passedString]; //••• WTF!!... this DOESN'T work! •••
// do something internally with passedNum here...
}
return self;
}
#end
Both model objects have outlets to the common NSTextField and I've control-dragged from both objects to the field and connected them. My AppController doesn't know about the NSTextField (and I assume, doesn't even want to know). No IB connections have been made between the controller object and model objects.
NSLog tells me that the model objects are being created, and that the passed values are making it that far... but not from there into the text field in the GUI window. I'm not getting any compiler errors or warnings. Am I missing some kind of 'setTarget:' call perhaps?
Any help/ideas would be much appreciated. Thanks :-)
Aside from the lack of MVC that mihirsm mentions, the actual problem is that you're trying to access an outlet in an -init method.
When a object is initialized, outlets are not guaranteed to be connected.
If you want to set the value of an NSTextField declared as an outlet, you should implement -awakeFromNib, which is called when the nib has been loaded and all outlets are guaranteed to be live.
in Model1.h:
#interface Model1 : NSObject
{
IBOutlet NSTextField* passedStringField;
NSString* modelString;
}
- (id)initWithString:(NSString*)passedString number:(int)passedNum;
#end
in Model1.m:
#implementation Model1
- (id)initWithString:(NSString*)passedString number:(int)passedNum
{
if(self = [super init])
{
//copy the string to our ivar
modelString = [passedString copy];
}
return self;
}
//awakeFromNib is called when our outlet is live
- (void)awakeFromNib
{
[passedStringField setStringValue:modelString];
}
//don't forget to release the string, because we created it using -copy
- (void)dealloc
{
[modelString release];
}
#end
The Controller sits between the Model and the View. The Model should not communicate with the View.
It should be the job of the Controller to pass any incoming values from the View to the Model. The Model then processes the data and sends back to the Controller which then updates the View with the new data.
So, in your code you should only have one IBOutlet for the TexField declared in the AppController.
Given all this, I am not exactly sure why the TextField is not being updated. From the given code looks like it should. Maybe multople IBOutlets are causing some issue? Can you try with only one Model having the IBOutlet?

Which delegate method should I use to respond to clicks on an NSTextField?

I am trying to respond to a click within a textfield. When the click occurs, I am going to open a panel. My initial thought was to use a delegate method to respond to the click event - but I found that:
This method doesn't work:
(void)textDidBeginEditing:(NSNotification *)aNotification
This method does work, but only when I actually edit the text within the text field, not when I first click it. And - if I edit the text a second time, this method stops working:
(void)controlTextDidBeginEditing:(NSNotification *)aNotification
I could use as much detail as possible - or a code example, ideally. I know that an nstextfield inherits from NSControl, which has a mouseDown event. Is there a similar way to respond to the event with a textfield, also?
Since NSTextField inherits from the NSControl class, it also inherits the -(void)mouseDown:(NSEvent*) theEvent method.
I needed to have an NSTextField call a delegate function upon clicking it today, and thought this basic code might be useful. Note that NSTextField already has a delegate and that in SDK v10.6, the delegate already has a protocol associated with it. Note that if you don't care about protocols, compiler warnings, etc., you don't need the protocol and property declarations or the getter and setter.
MouseDownTextField.h:
#import <Appkit/Appkit.h>
#class MouseDownTextField;
#protocol MouseDownTextFieldDelegate <NSTextFieldDelegate>
-(void) mouseDownTextFieldClicked:(MouseDownTextField *)textField;
#end
#interface MouseDownTextField: NSTextField {
}
#property(assign) id<MouseDownTextFieldDelegate> delegate;
#end
MouseDownTextField.m:
#import "MouseDownTextField.h"
#implementation MouseDownTextField
-(void)mouseDown:(NSEvent *)event {
[self.delegate mouseDownTextFieldClicked:self];
}
-(void)setDelegate:(id<MouseDownTextFieldDelegate>)delegate {
[super setDelegate:delegate];
}
-(id)delegate {
return [super delegate];
}
AppDelegate.h:
#interface AppDelegate <MouseDownTextFieldDelegate>
...
#property IBOutlet MouseDownTextField *textField;
...
AppDelegate.m:
...
self.textField.delegate = self;
...
-(void)mouseDownTextFieldClicked:(MouseDownTextField *)textField {
NSLog(#"Clicked");
...
}
...
If you're building with 10.5 SDK, don't have the protocol inherit from NSTextFieldDelegate.

Resources