onstwedder.com

All that you have is your soul

2 notes &

Cocoa coding: Blackjack tutorial - part 6

In this part I will set up the MainMenu.xib, containing the “window” of the application. That’s the one I’ve ignored up to now, by going straight to the Log Console.

Basically in the MainMenu.xib I only changed the window title to “Blackjack”. 

That’s it so far. 

Now I am going to add a BlackJackView Class. In the Classes directory add new file, choose as template: Mac OS X | Cocoa | Objective-C class. After clicking Next, the popup will ask you what Subclass it should be. Change this into: NSView.

Then I am going to add a BlackJackController. In the Classes directory add new file, choose as template: Mac OS X | Cocoa | Objective-C class. After clicking Next, the popup will ask you what Subclass it should be. Change this into: NSViewController and go for it.

This will also automatically make a new xib-file: BlackJackController.xib.

I want this xib file to look this, later on:

But I’ll get to that in a second. First the BlackJackView files have to be edited.

BlackJackVIew.h becomes
#import <Cocoa/Cocoa.h>

@interface BlackJackView : NSView {
	NSTextField *tableField;
	NSTextField *playerField;
	NSButton *hitBtn;
	NSButton *standBtn;
	NSButton *restartBtn;
	NSMutableArray *imageviews;
}

@property (retain) IBOutlet NSTextField *tableField;
@property (retain) IBOutlet NSTextField *playerField;
@property (retain) IBOutlet NSButton *hitBtn;
@property (retain) IBOutlet NSButton *standBtn;
@property (retain) IBOutlet NSButton *restartBtn;
@property (retain) NSMutableArray *imageviews;

Trust me on the imageviews, it will be part of the blackjack magic.

The .m file isn’t change much from the default, I’ve added @synthesize on the outlets and imageviews.

@implementation BlackJackView

@synthesize tableField, playerField, hitBtn, standBtn, restartBtn, imageviews;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
		self.imageviews = [[NSMutableArray alloc] initWithCapacity:4]; 
}
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
}

In the BlackJackController.xib I’ve edited the custom class via the identity inspector to class “BlackJackView” instead of NSView. This will give this customview it’s own methods later to be defined in the BlackJackView.

Set the File’s Owner to the BlackJackController.

I’ve added two textfields TablePoints and PlayerPoints, Also 3 buttons: Hit, Stand and Start. Both Hit and Stand are Hidden (set via Attribute Inspector > View > Drawing >Checkbox hidden).

The view should look like the image above.

Now set up the connections of those added outlets in the screen by connecting them to the BlackJackView’s IBOutlets.

There’s a lot to do. A lot.

  1. The tutorial21AppDelegate will have to start up the BlackJackController
  2. The BlackJackController will control the view above. 
  3. The BlackJackController will have to know how blackjack works and what it needs while playing.

That’s a bit too much to do in this part. So I am going to limit this part to starting up the application and showing the BlackJackController.xib.

That has to be done in the tutorial21AppDelegate. First off, let’s delete everything we previously added to the applicationDidFinishLaunching method. All that NSLogging, that’s not going to get this BlackJack in the app store. (Honestly, if you finished this tutorial, don’t put it in the App Store.)  Also by deleting the code in this method, we got rid of annoying warnings.

Also remove the @imports except the import of “tutorial21AppDelegate.h”

Now let’s move to the tutorial21AppDelegate.h itself. It will need to get an extra property *blackJackController of the type BlackJackController (and a heads up on that class by adding a @class BlackJackController. Also add a

@property(assign) BlackJackController *blackJackController;

 line as well.

Of course if you create a @property, you’d have to @synthesize it in the .m file. Also in the .m file of tutorial21AppDelegate I’ve added some code in the applicationDidFinishLaunching method. It resulted in this:

@synthesize window, blackJackController;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {	
	// Insert code here to initialize your application 
    self.blackJackController = [[BlackJackController alloc] initWithNibName:@"BlackJackController" bundle:nil];
	[self.window setContentSize:[self.blackJackController.view bounds].size];
	[self.window setContentMaxSize:[self.blackJackController.view bounds].size];
	self.window.contentView = self.blackJackController.view;
}

It initializes the blackJackController with the blackJackController.xib and sets the window size to the size of that xib’s NSView. The last line sets the contentView of the window application to display the NSView of the blackJackController.xib.

Let’s start and run. It should show an window with a button and two textfields.

Not finished yet. But finished for today.

Previous chapter: Blackjack tutorial - part 5.
Next chapter:
Blackjack tutorial - part 7.

Filed under english blackjack mac osx Cocoa Tutorial Xcode

  1. quantummao posted this