UILabel subclass - cocoa

I know that this is a newbie question but I am a newbie so here goes:
I wish to use Chalkduster font quite a lot throughout my app (buttons, labels etc) and have tried subclassing UILabel to achieve this. I have the following in Default.h:
#import <UIKit/UIKit.h>
#interface Default : UILabel
{
UILabel *theLabel;
}
#property (nonatomic, strong) IBOutlet UILabel *theLabel;
#end
and this in my .m:
#import "Default.h"
#implementation Default
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIFont *custom = [[UIFont alloc] init];
custom = [UIFont fontWithName:#"Chalkduster" size:18];
self.font = custom;
NSLog(#"h");
}
return self;
}
#end
When I change the class in interface builder and run, I'm not seeing the Chalkduster font. I'd appreciate a hand in getting this set up as I believe it will save me a lot of time.
Cheers.

Some problems to fix:
1) You're mixing up the idea of Default being a label and Default containing a label. To subclass, get rid of the property inside your class and make your changes to self rather than theLabel (inside the if (self) { section).
2) Anything you code after an unconditional return isn't going to get executed...and I'm surprised the compiler didn't complain about those statements.
Edit: ...and one more thing that just dawned on me.
3) If you're loading from a xib or storyboard, the initialization is done by initWithCoder: instead of initWithFrame:, so:
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
self.font = [UIFont fontWithName:#"Chalkduster" size:18];
}
return self;
}

First of all I don't think that You're subclassing UILabel correctlly. So I made tutorial for You explaining how to do it. You don't need to IBOutlet object which is subclassed. JUST CALL IT WITH SELF. for example: self.font = ... If You want to subclass UILabel do this:
Create new class with title myLabel like this:
.h
#import <UIKit/UIKit.h>
#interface MyLabel : UILabel {
}
#end
.m
#import "MyLabel.h"
#implementation MyLabel
-(void)awakeFromNib {
UIFont *custom = [[UIFont alloc] init];
custom = [UIFont fontWithName:#"Chalkduster" size:18];
self.font = custom;
}
#end
Now select Your label in storyboard and go to indentity inspector and in Custom Class select created class above. Like this:
Output:
Note: Don't forget to release custom because You are allocating it.

Move the return self; three lines down. You return from the init method before you do your custom initialization.
Edit to reflect new information from comment:
When deserializing the view from a nib you also have to override initWithCoder:

Related

IB_DESIGNABLE Class not rendering on Interface Builder

This is my first try with IB_DESIGNABLE on Xcode.
I have this class to add color to a NSView.
header
#import <Cocoa/Cocoa.h>
IB_DESIGNABLE #interface NSViewComCor : NSView
#property (nonatomic, weak) IBInspectable NSColor *backgroundColor;
#end
implementation
#import "NSViewComCor.h"
#implementation NSViewComCor
#synthesize backgroundColor = _backgroundColor;
- (void)awakeFromNib {
[super awakeFromNib];
[self setWantsLayer:YES];
self.backgroundColor = [NSColor whiteColor]; //default color
}
- (NSColor *) backgroundColor
{
CGColorRef colorRef = self.layer.backgroundColor;
return [NSColor colorWithCGColor:colorRef];
}
- (void) setBackgroundColor:(NSColor *)backgroundColor
{ // color should change when changed on interface builder inspectable color box
self.layer.backgroundColor = backgroundColor.CGColor;
_backgroundColor = backgroundColor;
}
even with IB_DESIGNABLE, this class does not render with the correct color on interface builder... why?
As the point out in WWDC 2014 "What's New in Interface Builder" video, you have to:
Create framework.
Create class.
Mark view as designable (and properties as inspectable).
Go back to the main project and specify the base class in Interface Builder.
For example, I added a new "framework" target to the project, and added the following NSView subclass source to that framework:
IB_DESIGNABLE #interface CustomView : NSView
#property (nonatomic, strong) IBInspectable NSColor *backgroundColor;
#end
and
#implementation CustomView
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
[self.backgroundColor setFill];
NSRectFill(dirtyRect);
}
#end
Then, when I went back to my main project and tried adding this CustomView as a subview on my storyboard, the "background color" was IB "inspectable" and I could see the changes in the color immediately rendered in IB.

MapKit pin title not showing

The location is working but the title isn't appearing, most strange.
location.latitude = (double) 51.501468;
location.longitude = (double) -0.141596;
// Add the annotation to our map view
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:#"ABC" andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
// [newAnnotation release];
MapViewAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapViewAnnotation : NSObject <MKAnnotation> {
NSString *title;
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
#end
and MapViewAnnotation.m
#import "MapViewAnnotation.h"
#implementation MapViewAnnotation
#synthesize title, coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
title = ttl;
coordinate = c2d;
return self;
}
#end
[newAnnotation release] is remmed out to keep the ARC happy :-)
Any ideas?
This did the trick:
[mapView selectAnnotation:newAnnotation animated:YES];
previously the title would only show if you clicked on the Pin.
you call annotation delegates refer this link, mapkit-example-in-iphone
That code looks fine (except for the non-standard implementation of the init method).
The most likely reason the title (callout) isn't appearing is that in your viewForAnnotation delegate method, you are not setting canShowCallout to YES (it defaults to NO).
In the viewForAnnotation delegate method, after you create the MKAnnotationView, set canShowCallout to YES.
Unrelated to the title/callout not showing, the init method in your MapViewAnnotation class should be implemented like this:
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
self = [super init];
if (self) {
title = ttl;
coordinate = c2d;
}
return self;
}

xCode: Activating an IBAction from within a dynamically added subview

I have a separate UIView class that constructs a simple footer bar that contains a UIButton.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:CGRectMake(0, 410, 320, 50)];
if (self) {
int buttonHeight = self.frame.size.height;
int buttonWidth = 70;
int nextBtnPosX =0;
int nextBtnPosY =0;
self.backgroundColor =[UIColor colorWithRed:254.0/255.0 green:193.0/255.0 blue:32.0/255.0 alpha:1.0];
[self sendSubviewToBack:self];
UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextBtn setTitle:#"Next" forState:UIControlStateNormal];
nextBtn.frame = CGRectMake(nextBtnPosX, nextBtnPosY, buttonWidth, buttonHeight);
[nextBtn addTarget:self.superview action:#selector(GoToNextPage) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:nextBtn];
}
return self;
}
I have several ViewControllers that then add this footer view class above to each view as a subview.
UIView *newFooter = [[theFooter alloc] init];
[self.view addSubview:newFooter];
Now the actual UIButton within the footer view needs to have its taget different for each viewController it is added to.
So I though it would be best to add the IBAction to the actual view controller then call this via the footer view.
But this is where I have come into a problem. How do I call the parent Controller to init the IBAction(GoToNextPage) from within the footer subview, from the addTarget?
Also would it be easier to have it all within the footer subview and pass in the target required, if so how else would this be done.
Here is a rough overview on what you should do.
This is how your UIView's header file should look
#protocol myViewControllerDelegate <NSObject>
#optional
- (void)didPushButton:(id)sender;
#end
#interface UIViewController : UITableViewController
{
__unsafe_unretained id <myViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <myViewControllerDelegate> delegate;
#end
Remember to #synthesize delegate; in your main file.
Finally in your main file, you will have an IBAction that receives the UIButton action.
Let's say that action is named buttonPushed.
Set that action to:
- (IBAction)buttonPushed:(id)sender
{
if (delegate)
[delegate didPushButton:sender];
}
Finally remember, that you need to set the delegate to each viewController you are using this viewController.
UIView *newFooter = [[theFooter alloc] init];
[self.view addSubview:newFooter];
newFooter.delegate = self;

IBOutlet for NSOutlineView doesn't point a valid instance

I created a new Cocoa application project in Xcode then add a NSOutlineView and a NSTextView objects onto window. Those two objects were subclassed as MyOutlineView and MyTextView. After that I made two outlets for them and wrote code like below.
The problem, I found, is application has two different MyOutlineView instances in runtime. Working(valid) outline view instance is not equal to the myOutlineView outlet instance. What am I missing?
//
// AppDelegate.h
#import <Cocoa/Cocoa.h>
#import "MyOutlineView.h"
#import "MyTextView.h"
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet MyOutlineView *myOutlineView;
#property (unsafe_unretained) IBOutlet MyTextView *myTextView;
#end
//
// AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)n
{
NSLog(#"AppDelegate.myOutlineView(INVALID)::%#", _myOutlineView);
NSLog(#"AppDelegate.myTextView::%#", _myTextView);
}
#end
//
// MyOutlineView.h
#import <Cocoa/Cocoa.h>
#interface MyOutlineView : NSOutlineView <NSOutlineViewDataSource>;
#end
//
// MyOutlineView.m
#import "MyOutlineView.h"
#implementation MyOutlineView
- (id)initWithCoder:(NSCoder *)aDecoder
{
// This method is called first.
self = [super initWithCoder:aDecoder];
NSLog(#"MyOutlineView initWithCoder(INVALID)::%#", self);
return self;
}
- (id)initWithFrame:(NSRect)frame
{
// This method is also called but through a different instance with first one.
self = [super initWithFrame:frame];
NSLog(#"MyOutlineView initWithFrame(valid)::%#", self);
return self;
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
NSLog(#"MyOutlineView data source delegate(valid)::%#", self);
return 0;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
return nil;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
return NO;
}
#end
//
// MyTextView.h
#import <Cocoa/Cocoa.h>
#interface MyTextView : NSTextView
#end
//
// MyTextView.m
#import "MyTextView.h"
#implementation MyTextView
- (id)initWithCoder:(NSCoder *)aDecoder
{
// This method is called.
self = [super initWithCoder:aDecoder];
NSLog(#"MyTextView initWithCoder::%#", self);
return self;
}
- (id)initWithFrame:(NSRect)frame
{
// But this method is NOT called at all.
self = [super initWithFrame:frame];
NSLog(#"MyTextView initWithFrame::%#", self);
return self;
}
#end
Output:
MyTextView initWithCoder:: [MyTextView: 0x10013be80]
MyOutlineView initWithCoder(INVALID):: [MyOutlineView: 0x10014bc90]
MyOutlineView initWithFrame(valid):: [MyOutlineView: 0x1001604a0]
MyOutlineView data source delegate(valid)::[MyOutlineView: 0x1001604a0]
AppDelegate.myOutlineView(INVALID):: [MyOutlineView: 0x10014bc90]
AppDelegate.myTextView:: [MyTextView: 0x10013be80]
Because of this, I have to put "AppDelegate.myOutlineView = self;" into MyOutletView's implementation wherever it calls related methods of AppDelegate. It does not seem natural.
Xcode doesn't seem to let you set an outline view's delegate or data source to itself.
So I'm guessing you're doing something like this:
Which is to say: instantiating a second copy of your custom outline view class.
Here's the output from this setup:
2012-09-26 14:11:34.511 testproj[30255:403] -[MyOutlineView initWithCoder:]
2012-09-26 14:11:34.531 testproj[30255:403] -[MyOutlineView initWithFrame:]
By removing the extra (highlighted) instance of My Outline View, the initWithFrame: line goes away.
To make the outline view its own delegate, do this instead:
- (void) awakeFromNib {
self.delegate = self;
}
That said, the point of the Delegation pattern is avoiding the need to subclass. If you do need an outline view subclass, try overriding the NSOutlineView / NSTableView methods directly, instead of using the delegate protocol.
I can't reproduce your problem. I dropped all your code that you posted into a test app, and I only get one instantiation of each object. Neither of the initWithFrame methods are getting called when I try it. My output is:
2012-09-26 09:00:38.945 TextViewDoubleInstantiationProblem[451:303] MyTextView initWithCoder::<MyTextView: 0x100123990>
Frame = {{0.00, 0.00}, {381.00, 182.00}}, Bounds = {{0.00, 0.00}, {381.00, 182.00}}
Horizontally resizable: NO, Vertically resizable: YES
MinSize = {381.00, 182.00}, MaxSize = {463.00, 10000000.00}
2012-09-26 09:00:38.953 TextViewDoubleInstantiationProblem[451:303] MyOutlineView initWithCoder(INVALID)::<MyOutlineView: 0x101a1cb90>
2012-09-26 09:00:39.005 TextViewDoubleInstantiationProblem[451:303] AppDelegate.myOutlineView(INVALID)::<MyOutlineView: 0x101a1cb90>
2012-09-26 09:00:39.005 TextViewDoubleInstantiationProblem[451:303] AppDelegate.myTextView::<MyTextView: 0x100123990>
Frame = {{0.00, 0.00}, {381.00, 182.00}}, Bounds = {{0.00, 0.00}, {381.00, 182.00}}
Horizontally resizable: NO, Vertically resizable: YES
MinSize = {381.00, 182.00}, MaxSize = {463.00, 10000000.00}
Do you have any other code in your app that you're not showing?
The calls to initWithCoder: come from loading objects that are defined in a nib file. I assume that's what you want to have happen since you mention creating outlets. In that case, they call to initWithFrame: strikes me as more likely to be "invalid" than the coder one.
I'd set a breakpoint in initWithFrame: and trace where that call is coming from in order to identify the extra allocation.

IBOutlets Not Setting in NSViewController

So I've got an NSViewController (MyVC) set up like so:
//MyVC.h
...
#property (nonatomic, retain) IBOutlet NSTextField *input;
...
//MyVC.m
...
#synthesize input;
- (id)init
{
self = [super initWithNibName: #"MyVC" bundle: [NSBundle mainBundle]];
NSLog(#"%#", input); //prints (null) always
return self;
}
- (void)loadView
{
[super loadView];
NSLog(#"%#", input); //still (null)
}
...
//MyVC.xib
Custom View [Referencing Outlet: File's Owner.view]
Text Field [Referencing Outlet: File's Owner.input]
Now, when I load this NSViewController (by way of MyVC *vc = [[MyVC alloc] init];) and load it into a window, I see the Text Field appropriately. However, as the above paste (and several BAD_ACCESSes) would suggest, vc.input is never properly pointing to the Text Field.
Notes:
This project is running ARC.
This is not a simplification or generalization. I've run this exact code to no avail.
All IBOutlets are definitely set up appropriately.
The error was a combination of things.
One of my revisions was missing the IBOutlet tag, and none of them were retaining references to the ViewController at runtime.

Resources