I am new to xcode iOS programming and I really need your help with the error of unrecognised selector sent to instance. Below is my implementation
//.h file
#interface AppAccountController : UIViewController <UIAlertViewDelegate>{
IBOutlet UITextField *txtName;
IBOutlet UITextField *txtEmail;
IBOutlet UIDatePicker *dateOB;
IBOutlet UITextField *txtPwd;
IBOutlet UITextField *txtRePwd;
}
- (IBAction)btnSave:(id)sender;
#end
//.m file
#interface AppAccountController ()
#property (strong, nonatomic) IBOutlet UITextField *txtName;
#property (strong, nonatomic) IBOutlet UITextField *txtEmail;
#property (strong, nonatomic) IBOutlet UIDatePicker *dateOB;
#property (strong, nonatomic) IBOutlet UITextField *txtPwd;
#property (strong, nonatomic) IBOutlet UITextField *txtRePwd;
#property (strong, nonatomic) IBOutlet UIButton *btnSave;
#end
#implementation AppAccountController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)btnSave:(id)sender {
// Create strings to store the text info
NSString *name = [txtName text];
NSString *email = [txtEmail text];
NSDate *dob = [dateOB date];
NSString *passwd = [txtPwd text];
NSString *repasswd = [txtRePwd text];
if ([txtName.text isEqualToString:#""])
{
[txtName becomeFirstResponder];
UIAlertView* finalCheck = [[UIAlertView alloc]
initWithTitle:#"Attention"
message:#"Name Field is empty. Please enter a name"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] ;
[finalCheck show];
finalCheck.tag = 1;
}
else if ([txtEmail.text isEqualToString:#""])
{
[txtEmail becomeFirstResponder];
UIAlertView* finalCheck = [[UIAlertView alloc]
initWithTitle:#"Attention"
message:#"Email Field is empty. Please enter an Email"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] ;
[finalCheck show];
finalCheck.tag = 2;
}
else if ([txtPwd.text isEqualToString:#""])
{
[txtPwd becomeFirstResponder];
UIAlertView* finalCheck = [[UIAlertView alloc]
initWithTitle:#"Attention"
message:#"Password Field is empty. Please enter a Password"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] ;
[finalCheck show];
finalCheck.tag = 3;
}
else if ([txtRePwd.text isEqualToString:#""])
{
[txtRePwd becomeFirstResponder];
UIAlertView* finalCheck = [[UIAlertView alloc]
initWithTitle:#"Attention"
message:#"Confirm Password Field is empty. Please enter to confirm Password"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] ;
[finalCheck show];
finalCheck.tag = 4;
}
else if (![passwd isEqualToString:repasswd])
{
[txtPwd becomeFirstResponder];
UIAlertView* finalCheck = [[UIAlertView alloc]
initWithTitle:#"Attention"
message:#"Passwords do not match. Please enter to confirm Password"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] ;
[finalCheck show];
finalCheck.tag = 5;
}
else{
// Store the data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:name forKey:#"name"];
[defaults setObject:email forKey:#"email"];
[defaults setObject:passwd forKey:#"passwd"];
[defaults setObject:dob forKey:#"dob"];
[defaults synchronize];
NSLog(#"Data saved");
[self performSegueWithIdentifier:#"saveLoad" sender:self];
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(alertView.tag == 1) {
[txtName becomeFirstResponder];
NSLog(#"Name AlertView is clicked");
}
else if(alertView.tag == 2) {
[txtEmail becomeFirstResponder];
NSLog(#"Email AlertView is clicked");
}
else if(alertView.tag == 3) {
[txtPwd becomeFirstResponder];
NSLog(#"Password AlertView is clicked");
}
else if(alertView.tag == 4) {
[txtRePwd becomeFirstResponder];
NSLog(#"Re-Password AlertView is clicked");
}
else if(alertView.tag == 5) {
[txtRePwd becomeFirstResponder];
NSLog(#"Re-Password AlertView is clicked");
}
}
#end
Whenever I press the btnSave button or after moving on to another UI element (one element gaining focus after another one losses it), I get unrecognised selector sent to instance.
UPDATE
2014-12-18 18:06:23.794 XPB[964:35743] -[AppAccountController btnSave:forEvent:]: unrecognized selector sent to instance 0x7fd949d923f0
2014-12-18 18:06:23.834 XPB[964:35743] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppAccountController btnSave:forEvent:]: unrecognized selector sent to instance 0x7fd949d923f0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010bd2df35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010a182bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010bd3504d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010bc8d27c ___forwarding___ + 988
4 CoreFoundation 0x000000010bc8ce18 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001087e98be -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x00000001088f0410 -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x00000001088ef7df -[UIControl touchesEnded:withEvent:] + 522
8 UIKit 0x000000010882f308 -[UIWindow _sendTouchesForEvent:] + 735
9 UIKit 0x000000010882fc33 -[UIWindow sendEvent:] + 683
10 UIKit 0x00000001087fc9b1 -[UIApplication sendEvent:] + 246
11 UIKit 0x0000000108809a7d _UIApplicationHandleEventFromQueueEvent + 17370
12 UIKit 0x00000001087e5103 _UIApplicationHandleEventQueue + 1961
13 CoreFoundation 0x000000010bc63551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x000000010bc5941d __CFRunLoopDoSources0 + 269
15 CoreFoundation 0x000000010bc58a54 __CFRunLoopRun + 868
16 CoreFoundation 0x000000010bc58486 CFRunLoopRunSpecific + 470
17 GraphicsServices 0x000000010c64e9f0 GSEventRunModal + 161
18 UIKit 0x00000001087e8420 UIApplicationMain + 1282
19 XPB 0x00000001083965b3 main + 115
20 libdyld.dylib 0x000000010ce4b145 start + 1
21 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
The problem is that btnSave: and btnSave:forEvent: are two different things.
Your AppAccountController has a btnSave: method, so it can be sent the btnSave: message. But it has no btnSave:forEvent: method. Unfortunately, that's the selector you specified to be used whenever the button is pressed. The button is trying to send btnSave:forEvent: to your AppAccountController, and AppAccountController doesn't know what to do with that. So it crashes.
The easiest solution is to fix the button so that it sends btnSave: and not btnSave:forEvent:. (You will almost never need the two-parameter variant of an action method in any case.)
I'm trying to do an animation in AppDelegate when starting the application. (From this tutorial: http://iosdevtips.co/post/88481653818/twitter-ios-app-bird-zoom-animation)
This is my AppDelegate class:
import UIKit
import QuartzCore
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var mask: CALayer?
var imageView: UIImageView?
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let imageView = UIImageView(frame: self.window!.frame)
imageView.image = UIImage(named: "Screen")
self.window!.addSubview(imageView)
self.mask = CALayer()
self.mask!.contents = UIImage(named: "twitter logo mask")!.CGImage
self.mask!.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
self.mask!.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.mask!.position = CGPoint(x: imageView.frame.size.width/2, y: imageView.frame.size.height/2)
imageView.layer.mask = mask
self.imageView = imageView
animateMask()
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor(red: 70/255, green: 154/255, blue: 233/255, alpha: 1)
self.window!.makeKeyAndVisible()
UIApplication.sharedApplication().statusBarHidden = true
return true
}
func applicationWillResignActive(application: UIApplication!) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication!) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication!) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication!) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication!) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func animateMask() {
let keyFrameAnimation = CAKeyframeAnimation(keyPath: "bounds")
keyFrameAnimation.delegate = self
keyFrameAnimation.duration = 1
keyFrameAnimation.beginTime = CACurrentMediaTime() + 1 //add delay of 1 second
let initalBounds = NSValue(CGRect: mask!.bounds)
let secondBounds = NSValue(CGRect: CGRect(x: 0, y: 0, width: 90, height: 90))
let finalBounds = NSValue(CGRect: CGRect(x: 0, y: 0, width: 1500, height: 1500))
keyFrameAnimation.values = [initalBounds, secondBounds, finalBounds]
keyFrameAnimation.keyTimes = [0, 0.3, 1]
keyFrameAnimation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut), CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)]
self.mask!.addAnimation(keyFrameAnimation, forKey: "bounds")
}
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
self.imageView!.layer.mask = nil //remove mask when animation completes
}
}
I assume that this line: self.window!.makeKeyAndVisible() puts this animation on the top of the window and shows it for the user. But after this my root viewcontroller which is attached to my Navigation Controller does not load. I've tried to put this code in the animationDidStop function:
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let vc = ViewController(nibName: "ViewController", bundle: nil)
self.window!.rootViewController = vc
self.window!.makeKeyAndVisible()
}
But this gives me the following runtime error:
2014-11-28 00:27:59.452 Test[3124:76510] Application windows are expected to have a root view controller at the end of application launch
2014-11-28 00:28:01.444 Test[3124:76510] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/MyComp/Library/Developer/CoreSimulator/Devices/0F42DE92-96A1-4D9B-A0D1-F1606FEEB2B4/data/Containers/Bundle/Application/ACAEFA85-9BE9-4C6C-8074-2692691FDA0C/Test.app> (loaded)' with name 'ViewController''
*** First throw call stack:
(
0 CoreFoundation 0x0000000108127f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000109ed9bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000108127e6d +[NSException raise:format(inlove) + 205
3 UIKit 0x0000000108ee68c3 -[UINib instantiateWithOwner:options(inlove) + 552
4 UIKit 0x0000000108d45f98 -[UIViewController _loadViewFromNibNamed:bundle(inlove) + 242
5 UIKit 0x0000000108d46588 -[UIViewController loadView] + 109
6 UIKit 0x0000000108d467f9 -[UIViewController loadViewIfRequired] + 75
7 UIKit 0x0000000108d46c8e -[UIViewController view] + 27
8 UIKit 0x0000000108c65ca9 -[UIWindow addRootViewControllerViewIfPossible] + 58
9 UIKit 0x0000000108c66041 -[UIWindow _setHidden:forced(inlove) + 247
10 UIKit 0x00000001166c57b0 -[UIWindowAccessibility _orderFrontWithoutMakingKey] + 68
11 UIKit 0x0000000108c7272c -[UIWindow makeKeyAndVisible] + 42
12 Test 0x0000000107b68ad1 _TFC4Test11AppDelegate16animationDidStopfS0_FTGSQCSo11CAAnimation_8finishedSb_T_ + 1297
13 Test 0x0000000107b68b67 _TToFC4Test11AppDelegate16animationDidStopfS0_FTGSQCSo11CAAnimation_8finishedSb_T_ + 71
14 QuartzCore 0x0000000108aa87ee _ZN2CA5Layer23run_animation_callbacksEPv + 308
15 libdispatch.dylib 0x000000010a6907f4 _dispatch_client_callout + 8
16 libdispatch.dylib 0x000000010a6798fb _dispatch_main_queue_callback_4CF + 949
17 CoreFoundation 0x000000010808ffe9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
18 CoreFoundation 0x0000000108052eeb __CFRunLoopRun + 2043
19 CoreFoundation 0x0000000108052486 CFRunLoopRunSpecific + 470
20 GraphicsServices 0x000000010c0f09f0 GSEventRunModal + 161
21 UIKit 0x0000000108c21420 UIApplicationMain + 1282
22 Test 0x0000000107b68efe top_level_code + 78
23 Test 0x0000000107b68f3a main + 42
24 libdyld.dylib 0x000000010a6c5145 start + 1
25 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
How can I show my root viewcontroller after the animation?
I found issue in your code.
Remove self.window!.makeKeyAndVisible() from didFinishLaunchingWithOptions method. Because it's not required at that time. This line is required when we set root view controller of window, so it's better place is in animationDidStop method.
As from your code I can see that you have used Xib file to load view controller. So exception raised because compiler not able to find Xib for ViewController.
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
self.imageView!.layer.mask = nil //remove mask when animation completes
//********** IF YOU USE STORYBOARD ***********//
// var mainStoryBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
// var viewController = mainStoryBoard.instantiateViewControllerWithIdentifier("ViewController") as UIViewController
//********** IF YOU USE Xib/Nib ***********//
var seconViewController = SeconViewController(nibName: "SeconViewController", bundle: NSBundle.mainBundle())
self.window?.rootViewController = seconViewController
self.window!.makeKeyAndVisible()
}
Here I have also written code for storyboard also if you like to load view controller from storyboard after this animation. Just remove above comment part and assign viewController as rootViewController for that.
I have created demo for this. If you not able to figure it out.
Sample Demo
I test an app that uses a custom UIFont. This font is used in a UILabel that is zoomable -- it has a CATiledLayer layer.
This is the code for the UILabel class:
#import "ZoomableLabel.h"
#implementation ZoomableLabel
+ (Class)layerClass
{
return [CATiledLayer class];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setupView];
}
return self;
}
//- (id)initWithCoder:(NSCoder *)aDecoder {
// self = [super initWithCoder:aDecoder];
// if (self) {
// // Initialization code
// [self setupView];
// }
// return self;
//}
-(void)awakeFromNib {
[super awakeFromNib];
[self setupView];
}
- (void)setupView {
CATiledLayer *layerForView = (CATiledLayer *)self.layer;
layerForView.levelsOfDetailBias = 3;
layerForView.levelsOfDetail = 1;
}
-(void)setText:(NSString *)value {
self.layer.contents = nil;
[super setText:value];
[self setNeedsDisplay];
}
-(void)setTextColor:(UIColor *)value {
self.layer.contents = nil;
[super setTextColor:value];
[self setNeedsDisplay];
}
#end
When I run the app on a device or the simulator for the first time (that is the app is installed for the first time on the device or the simulator) I get a crash. Then this crash never happens again! UPDATE: The crash happens VERY randomly (especially when demoing the app...) but not only the first time. This is all the info I managed to get from XCode:
Thread 3 name: Dispatch queue: com.apple.root.default-priority
Thread 3 Crashed: 0 WebCore 0x333adbfa WTF::HashTable<WebCore::FontData const*, std::__1::pair<WebCore::FontData const*, WebCore::GlyphPageTreeNode*>, WTF::PairFirstExtractor<std::__1::pair<WebCore::FontData const*, WebCore::GlyphPageTreeNode*> >, WTF::PtrHash<WebCore::FontData const*>, WTF::HashMapValueTraits<WTF::HashTraits<WebCore::FontData const*>, WTF::HashTraits<WebCore::GlyphPageTreeNode*> >, WTF::HashTraits<WebCore::FontData const*> >::rehash(int) + 42
1 WebCore 0x333adcd4 WTF::HashTableAddResult<WTF::HashTableIterator<WebCore::FontData const*, std::__1::pair<WebCore::FontData const*, WebCore::GlyphPageTreeNode*>, WTF::PairFirstExtractor<std::__1::pair<WebCore::FontData const*, WebCore::GlyphPageTreeNode*> >, WTF::PtrHash<WebCore::FontData const*>, WTF::HashMapValueTraits<WTF::HashTraits<WebCore::FontData const*>, WTF::HashTraits<WebCore::GlyphPageTreeNode*> >, WTF::HashTraits<WebCore::FontData const*> > > WTF::HashTable<WebCore::FontData const*, std::__1::pair<WebCore::FontData const*, WebCore::GlyphPageTreeNode*>, WTF::PairFirstExtractor<std::__1::pair<WebCore::FontData const*, WebCore::GlyphPageTreeNode*> >, WTF::PtrHash<WebCore::FontData const*>, WTF::HashMapValueTraits<WTF::HashTraits<WebCore::FontData const*>, WTF::HashTraits<WebCore::GlyphPageTreeNode*> >, WTF::HashTraits<WebCore::FontData const*> >::add<WTF::HashMapTranslator<WTF::HashMapValueTraits<WTF::HashTraits<WebCore::FontData const*>, WTF::HashTraits<WebCore::GlyphPageTreeNode*> >, WTF::PtrHash<WebCore::FontData const*> >, WebCore::FontData const*, WebCore::GlyphPageTreeNode*>(WebCore::FontData const* const&, WebCore::GlyphPageTreeNode* const&) + 56
2 WebCore 0x333a5cac WebCore::GlyphPageTreeNode::getChild(WebCore::FontData const*, unsigned int) + 264
3 WebCore 0x333a55d8 WebCore::Font::glyphDataAndPageForCharacter(int, bool, WebCore::FontDataVariant) const + 528
4 WebCore 0x333a53b6 WebCore::Font::glyphDataForCharacter(int, bool, WebCore::FontDataVariant) const + 18
5 WebCore 0x333a4b36 WebCore::WidthIterator::advance(int, WebCore::GlyphBuffer*) + 398
6 WebCore 0x333a4794 WebCore::Font::floatWidthForSimpleText(WebCore::TextRun const&, WebCore::GlyphBuffer*, WTF::HashSet<WebCore::SimpleFontData const*, WTF::PtrHash<WebCore::SimpleFontData const*>, WTF::HashTraits<WebCore::SimpleFontData const*> >*, WebCore::GlyphOverflow*) const + 60
7 WebCore 0x333a4546 WebCore::Font::width(WebCore::TextRun const&, WTF::HashSet<WebCore::SimpleFontData const*, WTF::PtrHash<WebCore::SimpleFontData const*>, WTF::HashTraits<WebCore::SimpleFontData const*> >*, WebCore::GlyphOverflow*) const + 250
8 WebCore 0x333a60e0 WebCore::truncateString(WTF::String const&, float, WebCore::Font const&, unsigned int (*)(WTF::String const&, unsigned int, unsigned int, unsigned short*, bool), bool, float*, bool, float, bool) + 296
9 WebCore 0x333a5fac WebCore::StringTruncator::rightTruncate(WTF::String const&, float, WebCore::Font const&, WebCore::StringTruncator::EnableRoundingHacksOrNot, float&, bool, float) + 60
10 WebKit 0x375fc718 applyEllipsisStyle(WTF::String const&, WebEllipsisStyle, float, WebCore::Font const&, WebCore::StringTruncator::EnableRoundingHacksOrNot, float*, bool, float, bool) + 464
11 WebKit 0x375ff3a8 -[NSString(WebStringDrawing) __web_drawInRect:withFont:ellipsis:alignment:letterSpacing:lineSpacing:includeEmoji:truncationRect:measureOnly:renderedStringOut:drawUnderline:] + 5036
12 WebKit 0x375fdfe8 -[NSString(WebStringDrawing) __web_drawInRect:withFont:ellipsis:alignment:letterSpacing:lineSpacing:includeEmoji:truncationRect:measureOnly:renderedStringOut:] + 112
13 WebKit 0x375fdf64 -[NSString(WebStringDrawing) __web_drawInRect:withFont:ellipsis:alignment:letterSpacing:lineSpacing:includeEmoji:truncationRect:measureOnly:] + 108
14 WebKit 0x375fdee4 -[NSString(WebStringDrawing) _web_drawInRect:withFont:ellipsis:alignment:lineSpacing:includeEmoji:truncationRect:measureOnly:] + 108
15 WebKit 0x375fde64 -[NSString(WebStringDrawing) _web_sizeInRect:withFont:ellipsis:lineSpacing:] + 80
16 UIKit 0x353698c2 -[NSString(UIStringDrawing) sizeWithFont:constrainedToSize:lineBreakMode:lineSpacing:] + 122
17 UIKit 0x3535daa6 -[UILabel _legacy_drawTextInRect:baselineCalculationOnly:] + 594
18 UIKit 0x35321c5a -[UILabel _drawTextInRect:baselineCalculationOnly:] + 162
19 UIKit 0x35320a26 -[UILabel drawTextInRect:] + 446
20 UIKit 0x35320860 -[UILabel drawRect:] + 68
21 UIKit 0x3531fd20 -[UIView(CALayerDelegate) drawLayer:inContext:] + 360
22 QuartzCore 0x37b84bb8 -[CALayer drawInContext:] + 108
23 QuartzCore 0x37c62624 tiled_layer_render(_CAImageProvider*, unsigned int, unsigned int, unsigned int, unsigned int, void*) + 1416
24 QuartzCore 0x37bd755c CAImageProviderThread(unsigned int*, bool) + 508
25 libdispatch.dylib 0x37b5e95c _dispatch_root_queue_drain + 248
26 libdispatch.dylib 0x37b5eabc _dispatch_worker_thread2 + 80
27 libsystem_c.dylib 0x38862a0e _pthread_wqthread + 358
28 libsystem_c.dylib 0x388628a0 start_wqthread + 4
Does anybody has any idea on this? I think this crash does not happen on an iOS 5 device...
Here is my take on Summon's answer which uses Core Text to address issues with languages other than English (although I have only tested Swedish) and odd font symbols. Note that it uses initWithCoder as I'm using StoryBoard. It also shows how to center the text in the label horizontally (had less luck with vertical where I simply had to test my way to the right placement).
Header:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface DNLabel : UILabel
#end
Implementation:
#import "DNLabel.h"
#import <CoreText/CoreText.h>
#implementation DNLabel
+ (Class)layerClass
{
return [CATiledLayer class];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self setupView];
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
[self setupView];
}
- (void)setupView {
CATiledLayer *layerForView = (CATiledLayer *)self.layer;
layerForView.levelsOfDetailBias = 10;
layerForView.levelsOfDetail = 10;
}
// LEAVE IT EMPTY
-(void)drawRect:(CGRect)r
{
// UIView uses the existence of -drawRect: to determine if should allow its CALayer
// to be invalidated, which would then lead to the layer creating a backing store and
// -drawLayer:inContext: being called.
// By implementing an empty -drawRect: method, we allow UIKit to continue to implement
// this logic, while doing our real drawing work inside of -drawLayer:inContext:
}
// These calls inside this method are thread SAFE
-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
// Core Text version
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)self.font.fontName,
self.font.pointSize,
NULL);
// set color
CGColorRef color = [[[UIColor blackColor] colorWithAlphaComponent:0.75f] CGColor];
// pack it into attributes dictionary
NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
(id)CFBridgingRelease(ctFont), (id)kCTFontAttributeName,
color, (id)kCTForegroundColorAttributeName,
nil, (id)kCTUnderlineStyleAttributeName, nil];
// make the attributed string
NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:self.text
attributes:attributesDict];
// flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// check size of text and set position centered horizontally
CGSize size = [self.text sizeWithFont:self.font];
CGContextSetTextPosition(context, self.bounds.size.width/2 - size.width/2, 1);
// draw
CTLineRef line = CTLineCreateWithAttributedString(
(CFAttributedStringRef)CFBridgingRetain(stringToDraw));
CTLineDraw(line, context);
}
#end
These are the .h and .m classes of a zoomable label based on CATiledLayer that never crashes:
Header:
#import <QuartzCore/QuartzCore.h>
#interface ZoomableLabel : UILabel {
CGPoint textDrawPoint;
CGPoint shadowDrawPoint;
}
#end
Implementation:
#import "ZoomableLabel.h"
#implementation ZoomableLabel
+ (Class)layerClass
{
return [CATiledLayer class];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setupView];
}
return self;
}
-(void)awakeFromNib {
[super awakeFromNib];
[self setupView];
}
- (void)setupView {
CATiledLayer *layerForView = (CATiledLayer *)self.layer;
layerForView.levelsOfDetailBias = 3;
layerForView.levelsOfDetail = 1;
textDrawPoint = CGPointMake(11, -22);
shadowDrawPoint = CGPointMake(10, -23);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
textDrawPoint = CGPointMake(11, -42);
shadowDrawPoint = CGPointMake(8, -46);
}
}
// DO NOT USER THESE METHODS ANYMORE
//-(void)setText:(NSString *)value {
//// self.layer.contents = nil;
// [super setText:value];
// [self setNeedsDisplayInRect:self.bounds];
//}
//
//-(void)setTextColor:(UIColor *)value {
//// self.layer.contents = nil;
// [super setTextColor:value];
// [self setNeedsDisplayInRect:self.bounds];
//}
// LEAVE IT EMPTY
-(void)drawRect:(CGRect)r
{
// UIView uses the existence of -drawRect: to determine if should allow its CALayer
// to be invalidated, which would then lead to the layer creating a backing store and
// -drawLayer:inContext: being called.
// By implementing an empty -drawRect: method, we allow UIKit to continue to implement
// this logic, while doing our real drawing work inside of -drawLayer:inContext:
}
// These calls inside this method are thread SAFE
-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
// Do all your drawing here. Do not use UIGraphics to do any drawing, use Core Graphics instead.
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextSelectFont(context, [self.font.fontName UTF8String], self.font.pointSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
// CGContextSetShouldAntialias(context, true);
CGContextSetFillColorWithColor(context, [[[UIColor blackColor] colorWithAlphaComponent:0.75f] CGColor]);
CGContextShowTextAtPoint(context, shadowDrawPoint.x, shadowDrawPoint.y, [self.text UTF8String], self.text.length);
CGContextSetFillColorWithColor(context, [self.textColor CGColor]);
CGContextShowTextAtPoint(context, textDrawPoint.x, textDrawPoint.y, [self.text UTF8String], self.text.length);
}
#end
I wrote code like:
+ (CGFloat)tableView:(UITableView*)tableView rowHeightForObject:(id)item {
CustomTTTableSubtitleItem* captionedItem = item;
CGFloat maxWidth = tableView.width - kHPadding*2;
CGSize titleSize = [captionedItem.title sizeWithFont:TTSTYLEVAR(myTitleFont) constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
}
Got this exception:
2011-07-24 03:10:18.762 xinyou[15941:b303] -[TTDefaultStyleSheet
myTitleFont]: unrecognized selector sent to instance 0x5b5e120
2011-07-24 03:10:18.765 xinyou[15941:b303] * Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason:
'-[TTDefaultStyleSheet myTitleFont]: unrecognized selector sent to
instance 0x5b5e120'
* Call stack at first throw: ( 0 CoreFoundation
0x0119a5a9 exceptionPreprocess + 185 1 libobjc.A.dylib
0x012ee313 objc_exception_throw + 44 2 CoreFoundation
0x0119c0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 3
CoreFoundation 0x0110b966 __forwarding + 966
4 CoreFoundation 0x0110b522
_CF_forwarding_prep_0 + 50 5 xinyou
0x000081f9 +[CustomTTTableSubtitleItemCell
tableView:rowHeightForObject:] + 186 6 xinyou
0x000a6c92 -[TTTableViewVarHeightDelegate
tableView:heightForRowAtIndexPath:] + 156 7 UIKit
0x0064a6d5 -[UISectionRowData
In this exception you can see [TTDefaultStyleSheet myTitleFont]: unrecognized selector sent to instance 0x5b5e120 but actually myTitleFont defined in XYDefaultStyleSheet and I've imported XYDefaultStyleSheet.h in my class. XYDefaultStyleSheet.h and XYDefaultStyleSheet.m are like:
XYDefaultStyleSheet.h
#import "Three20/Three20.h"
#interface XYDefaultStyleSheet : TTDefaultStyleSheet
#property(nonatomic,readonly) UIColor* myHeadingColor;
#property(nonatomic,readonly) UIColor* mySubtextColor;
#property(nonatomic,readonly) UIColor* myTitleColor;
#property(nonatomic,readonly) UIFont* myTitleFont;
#property(nonatomic,readonly) UIFont* myHeadingFont;
#property(nonatomic,readonly) UIFont* mySubtextFont;
#end
XYDefaultStyleSheet.m
#import "XYDefaultStyleSheet.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
#implementation XYDefaultStyleSheet
///////////////////////////////////////////////////////////////////////////////////////////////////
// styles
///////////////////////////////////////////////////////////////////////////////////////////////////
// public colors
- (UIColor*)myTitleColor {
return [UIColor blackColor];
}
- (UIColor*)myHeadingColor {
return RGBCOLOR(80, 110, 140);
}
- (UIColor*)mySubtextColor {
return [UIColor grayColor];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// public fonts
- (UIFont*)myTitleFont {
return [UIFont boldSystemFontOfSize:16];
}
- (UIFont*)myHeadingFont {
return [UIFont boldSystemFontOfSize:13];
}
- (UIFont*)mySubtextFont {
return [UIFont systemFontOfSize:12];
}
#end
why always tell [TTDefaultStyleSheet myTitleFont] ... if the problem is really myTitleFont, it should be [XYDefaultStyleSheet myTitleFont], why TTDefaultStyleSheet?
got it! Init my style sheet in AppDelegate.
This is an answer to #Jason Zhao's answer about initialising within the AppDelegate.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[TTStyleSheet setGlobalStyleSheet:[[[CustomDefaultStyleSheet alloc]
init] autorelease]];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
Original code is from here, which has a lot of useful information about using TTStyleSheet's:
Three20 Stylesheets iPhone Tutorial
I have the following problem. I inherited an app and I am trying to modify it. The original functionality lets me add a task with some text notes. I want to add an icon to make it more visually appealing. When I modify the code signature to pass the icon (in reality I am only passing the name of the icon in a NSString) the modules stops working and the app throws the following error, basically an unrecognized selector:
2011-03-19 22:41:17.713 app[82653:207] -[RemindersDataManager addReminder:notes:locations:icon:]: unrecognized selector sent to instance 0xc700bc0
2011-03-19 22:41:17.716 app[82653:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RemindersDataManager addReminder:notes:locations:icon:]: unrecognized selector sent to instance 0xc700bc0'
*** Call stack at first throw:
(
0 CoreFoundation 0x0162abe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0177f5c2 objc_exception_throw + 47
2 CoreFoundation 0x0162c6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x0159c366 ___forwarding___ + 966
4 CoreFoundation 0x0159bf22 _CF_forwarding_prep_0 + 50
5 app 0x000158b1 -[GeoRemindersManager addReminder:notes:locations:icon:] + 121
6 app 0x0000f268 -[AddReminderViewController addReminder] + 655
7 UIKit 0x0053ea6e -[UIApplication sendAction:to:from:forEvent:] + 119
8 UIKit 0x005cd1b5 -[UIControl sendAction:to:forEvent:] + 67
9 UIKit 0x005cf647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
10 UIKit 0x005ce1f4 -[UIControl touchesEnded:withEvent:] + 458
11 UIKit 0x007c9987 _UIGestureRecognizerSortAndSendDelayedTouches + 3609
12 UIKit 0x007ca0fc _UIGestureRecognizerUpdateObserver + 927
13 CoreFoundation 0x0160bfbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
14 CoreFoundation 0x015a10e7 __CFRunLoopDoObservers + 295
15 CoreFoundation 0x01569bd7 __CFRunLoopRun + 1575
16 CoreFoundation 0x01569240 CFRunLoopRunSpecific + 208
17 CoreFoundation 0x01569161 CFRunLoopRunInMode + 97
18 GraphicsServices 0x01ce4268 GSEventRunModal + 217
19 GraphicsServices 0x01ce432d GSEventRun + 115
20 UIKit 0x0054d42e UIApplicationMain + 1160
21 app 0x00002d1c main + 102
22 app 0x00002cad start + 53
)
terminate called after throwing an instance of 'NSException'.
These are the original module's signature
AddReminderViewController.h
// user actions
- (IBAction)addReminder;
AddReminderViewController.m
- (IBAction)addReminder {
if ([self dataVerification]) {
BOOL result = NO;
// try to add one geo fence or several geo fences
if (self.address) {
result = [[GeoRemindersManager sharedInstance] addReminder:self.titleField.text notes:self.detailsField.text
coordinate:self.coordinate address:self.address];
}
else if (self.locations) {
result = [[GeoRemindersManager sharedInstance] addReminder:self.titleField.text notes:self.detailsField.text locations:self.locations];
}
// successfully added - close
if (result) {
[self.navigationController popToRootViewControllerAnimated:YES];
}
GeoRemindersManager.h
// shared instance
+ (GeoRemindersManager *)sharedInstance;
// workflow
- (void)start;
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes
coordinate:(CLLocationCoordinate2D)coordinate address:(NSString *)address;
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes location:(LocationEntity *)location;
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes locations:(NSArray *)locations;
- (void)changeReminder:(NSString *)title title:(NSString *)newTitle notes:(NSString *)newNotes;
- (void)removeReminder:(NSString *)title;
- (ReminderEntity *)getReminderWithTitle:(NSString *)title;
- (NSArray *)getLoctionsWithCoordinate:(CLLocationCoordinate2D)coordinate;
- (void)wakeByLocalNotification:(NSDictionary *)info;
- (void)memoryWarning;
- (CLLocation *)getCurrentLocation;
// extra
- (BOOL)checkReminderExistance:(NSString *)title;
- (void)updateDelegatesWithReminder:(NSString *)title notes:(NSString *)notes;
GeoRemindersManager.m
+ (GeoRemindersManager *)sharedInstance {
geoapp_iphoneAppDelegate *appDelegate = (geoapp_iphoneAppDelegate *)[MLSApplication instance];
return appDelegate.remindersManager;
}
#pragma mark -
#pragma mark workflow
- (void)start {
// start first locating
[deviceLocation forceStart];
// add geo fences to device location
NSArray *locations = [geoDataManager getAllRemindersLocations];
[deviceLocation addMonitoringLocations:locations];
// invoke delegate
[delegateMap didAddMonitoringLocations:locations];
}
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes
coordinate:(CLLocationCoordinate2D)coordinate address:(NSString *)address
{
if (![self checkReminderExistance:title]) {
return NO;
}
// all verifications are OK, adding reminder
[geoDataManager addReminder:title notes:notes coordinate:coordinate address:address];
[self updateDelegatesWithReminder:title notes:notes];
return YES;
}
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes location:(LocationEntity *)location {
return [self addReminder:title notes:notes locations:[NSArray arrayWithObject:location]];
}
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes locations:(NSArray *)locations {
if (![self checkReminderExistance:title]) {
return NO;
}
// all verifications are OK, adding reminder
[geoDataManager addReminder:title notes:notes locations:locations];
[self updateDelegatesWithReminder:title notes:notes];
return YES;
}
and the modified ones:
AddReminderViewController.m
- (IBAction)addReminder {
if ([self dataVerification]) {
BOOL result = NO;
// try to add one geo fence or several geo fences
if (self.address) {
result = [[GeoRemindersManager sharedInstance] addReminder:self.titleField.text notes:self.detailsField.text coordinate:self.coordinate address:self.address icon:(NSString *)iconButton.currentTitle];
}
else if (self.locations) {
result = [[GeoRemindersManager sharedInstance] addReminder:self.titleField.text notes:self.detailsField.text locations:self.locations icon:(NSString *)iconButton.currentTitle];
}
// successfully added - close
if (result) {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
}
GeoRemindersManager.h
// shared instance
+ (GeoRemindersManager *)sharedInstance;
// workflow
- (void)start;
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes
coordinate:(CLLocationCoordinate2D)coordinate address:(NSString *)address icon:(NSString *)icon;
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes location:(LocationEntity *)location icon:(NSString *)icon;
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes locations:(NSArray *)locations icon:(NSString *)icon;
- (void)changeReminder:(NSString *)title title:(NSString *)newTitle notes:(NSString *)newNotes icon:(NSString *)icon;
- (void)removeReminder:(NSString *)title;
- (ReminderEntity *)getReminderWithTitle:(NSString *)title;
- (NSArray *)getLoctionsWithCoordinate:(CLLocationCoordinate2D)coordinate;
- (void)wakeByLocalNotification:(NSDictionary *)info;
- (void)memoryWarning;
- (CLLocation *)getCurrentLocation;
// extra
- (BOOL)checkReminderExistance:(NSString *)title;
- (void)updateDelegatesWithReminder:(NSString *)title notes:(NSString *)notes icon:(NSString *)icon;
GeoRemindersManager.m
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes
coordinate:(CLLocationCoordinate2D)coordinate address:(NSString *)address icon:(NSString *)icon
{
if (![self checkReminderExistance:title]) {
return NO;
}
// all verifications are OK, adding reminder
[geoDataManager addReminder:title notes:notes coordinate:coordinate address:address icon:icon];
[self updateDelegatesWithReminder:title notes:notes icon:icon];
return YES;
}
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes location:(LocationEntity *)location icon:(NSString *)icon{
return [self addReminder:title notes:notes locations:[NSArray arrayWithObject:location icon:icon]];
}
- (BOOL)addReminder:(NSString *)title notes:(NSString *)notes locations:(NSArray *)locations icon:(NSString *)icon {
if (![self checkReminderExistance:title]) {
return NO;
}
// all verifications are OK, adding reminder
[geoDataManager addReminder:title notes:notes locations:locations icon:icon];
[self updateDelegatesWithReminder:title notes:notes];
return YES;
}
So in short, the only thing I added was an additional argument to those function for the icon name as a NSString… Any clues? All help greatly appreciated! Thanks
The error message indicates that the unrecognized selector was called on an object of class RemindersDataManager, probably from the line
[geoDataManager addReminder:title notes:notes locations:locations icon:icon];
You should find the implementation of that class and add your icon parameter to its methods too (or stop passing icon to it if it has no use for the icon).