Method doesn't run when invoked - objective-c

EDIT: Problem has been solved, I moved allocation and initiation of variables into another method. SubOtherClass never gets initiated (alloc and init are never called).
Classes have been renamed to make this question more general.
Hypothetical class OtherClass extends NSView
Hypothetical class SubOtherClass extends the hypothetical class OtherClass and invokes the update method in a local instance of ClassToUpdate
I understand that updating the view when a key gets released is not the best of ideas, but that's only temporary. I'm not an expert in Obj-C. To repeat the problem, the update method in SubOtherClass gets executed but not in ClassToUpdate, and the content (not shown here) of that method doesn't run. How can I fix this? If anymore info is necessary, just ask.
Thanks.
Edit: Full code (with renamed classes)
Header:
#import "OtherClass.h"
#import "ThingToRender.h"
#import "ClassToUpdate.h"
#interface SubOtherClass : OtherClass
#property (assign) ThingToRender *thingToRender1, *thingToRender2;
#property (retain) ClassToUpdate *classToUpdate;
- (void) createVariables;
- (void) update;
#end
Implementation:
#import "SubOtherClass.h"
#implementation SubOtherClass
- (BOOL) acceptsFirstResponder{
return true;
}
- (void) createVariables{
self.classToUpdate = [[ClassToUpdate alloc] init];
self.thingToRender1 = [[ThingToRender alloc] init];
self.thingToRender2 = [[ThingToRender alloc] init];
}
- (void) keyUp:(NSEvent *)theEvent{
[super keyUp:theEvent];
[self setNeedsDisplay:true];
}
- (void) keyDown:(NSEvent *)theEvent{
[super keyDown:theEvent];
}
- (void) update{
[self.classToUpdate update:self];
}
- (void) drawRect:(NSRect)rect{
[super drawRect:rect];
[self update];
[[NSColor blackColor] set];
NSRectFill(rect);
[self.color1 set]; //this color is declared in superclass
NSString *str1 = [NSString stringWithFormat:#"%d %d %d %d", self.thingToRender1.x, self.thingToRender1.y, 30, 100];
NSRectFill(NSRectFromString(str1));
[self.color2 set]; //this color is declared in superclass
str1 = [NSString stringWithFormat:#"%d %d %d %d", self.thingToRender2.x, self.thingToRender2.y, 30, 100];
NSRectFill(NSRectFromString(str1));
[self.color3 set]; //this color is declared in superclass
str1 = [NSString stringWithFormat:#"%d %d %d %d", self.classToUpdate.x, self.classToUpdate.y, 30, 30];
NSRectFill(NSRectFromString(str1));
}
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
[self setNeedsDisplay:YES];
return self;
}
#end
OtherClass extends NSView

Are you sure that self.classToUpdate isn't nil while executing?
Maybe you're not initializing that class anywhere?
Replace update method in SubOtherClass with this code:
- (void) update{
if(!self.classToUpdate){
NSLog(#"classToUpdate is nil");
}
[self.classToUpdate update:self];
}
And look on the console if 'classToUpdate is nil' text appears

In your drawrect method just include this line:-
[super drawrect:rect]
So that it call super class drawrect method

I'm not exactly sure what was causing the problem, and I find it complicated, but I eventually found the solution. I moved constructors for the variables to another method, and alloc is never called on SubOtherClass. Thanks anyway for everyone's help.

Related

Cannot add CCNode to Scene

I have the IntroScene, and I wanna add a node, but it doesn't seem to work. Here are two different ways I tried doing it, and BOTH failed.
First way, failed:
in hearts2.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface Hearts2 : CCNode {
}
#end
in hearts2.m
#import "Hearts2.h"
#implementation Hearts2
#end
in IntroLayer.m
- (id)init
{
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);
heart2 *heart;
[self addChild:heart z:2];
// done
return self;
}
I didn't expect that to work (actually I was desperate and tried it that way as the second way just to see if it would work). The actual first attempt I tried to do was this, and it also Failed:
in hearts1.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface Hearts1 : CCNode
+ (Hearts1 *)node;
- (id)init;
-(void)selfAnimate;
#end
in hearts1.m
#import "Hearts1.h"
#implementation Hearts1 {
}
+ (Hearts1 *)node
{
return [[self alloc] init];
}
- (id)init
{
self = [super init];
if (!self) return(nil);
return self;
}
- (void)dealloc
{
}
- (void)onEnter
{
[super onEnter];
}
- (void)onExit
{
// always call super onExit last
[super onExit];
}
- (void)selfAnimate
{
}
#end
in IntroLayer.m
- (id)init
{
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);
heart1 *heart;
[self addChild:heart z:2];
// done
return self;
}
Please, I would do anything if someone could help me figure this out thanks everyone very much. I always get the SigABRT so I have no idea what is going wrong. I'm sure I'm just stupid and don't know how to code and missing something simple.
heart2 *heart;
You named your class Hearts2 so use the exact same name, including uppercase.
Secondly you created a variable but this will be nil. If you aren't using ARC (which you should) this will create an uninitialized object.
This will create an instance of Hearts2, assign it to the local var heart and add it as a child:
Hearts2 *heart = [Hearts2 node];
[self addChild:heart z:2];

UIViewController maintains state after being nilled

In my app, I made a BookViewController class that displays and animates the pages of a book and a MainMenuViewController class that displays a set of books the user can read.
In the latter class, when the user taps on one of the books, a function is called that should create a completely new instance of BookViewController, but for some reason the instance maintains its state (i.e. it resumes from the page the user left off).
How can this be if I set it to nil? What am I missing here? (Note that I'm using ARC).
MainMenuViewController.m
#interface MainMenuViewController ()
#property (strong) BookViewController *bookViewController;
#end
#implementation MainMenuViewController
#synthesize bookViewController;
-(void)bookTapped:(UIButton *)sender{
NSString *bookTitle;
if(sender == book1button) bookTitle = #"book1";
else if(sender == book2button) bookTitle = #"book2";
bookViewController = nil;
bookViewController = [[BookViewController alloc] initWithBookTitle:bookTitle];
[self presentViewController:bookViewController animated:YES completion:nil];
}
BookViewController.h
#interface BookViewController : UIViewController
-(id)initWithBookTitle:(NSString *)bookTitle;
#end
BookViewController.m
#implementation BookViewController
-(id)initWithBookTitle:(NSString *)theBookTitle{
self = [super init];
if(self){
bookTitle = [NSString stringWithFormat:#"%#", theBookTitle];
[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
NSLog(#"init a BookViewController with bookTitle: %#", bookTitle);
}
return self;
}
edit 1:
Every time a book is tapped, bookTapped: is called, and thee console always prints:
2012-08-31 16:29:51.750 AppName[25713:c07] init a BookViewController with bookTitle: book1
So if a new instance of BookViewController is being created, how come it seems to be returning the old one?
edit 2:
I inserted NSLog(#"bookViewController %#",bookViewController); just before the line [self presentViewController:bookViewController. The console output is:
2012-08-31 16:37:41.426 Henry[25784:c07] bookViewController <BookViewController: 0x6a21540>
2012-08-31 16:38:23.321 Henry[25784:c07] bookViewController <BookViewController: 0xe425540>
2012-08-31 16:38:53.393 Henry[25784:c07] bookViewController <BookViewController: 0x6839330>
Your variables are declared outside of the #implementation of the class (you are declaring global variables).
I suspect that you are using the ivars instead of the properties. Please replace bookViewController with self.bookViewController.
Try:
if(self){
self.bookTitle
The variables that were maintaing their state in the new instance were declared thus:
#import "BookViewController.h"
int currentPage = 0;
#implementation BookViewController
-(id)initWithBookTitle:(NSString *)theBookTitle{
...
So I managed to fix the issue by initialising the variables in the init method:
-(id)initWithBookTitle:(NSString *)theBookTitle{
self = [super init];
if(self){
currentPage = 0; //added this line
bookTitle = [NSString stringWithFormat:#"%#", theBookTitle];
[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
NSLog(#"init a BookViewController with bookTitle: %#", bookTitle);
}
return self;
}
But this doesn't solve the underlying problem, which is that doing this:
bookViewController = [[BookViewController alloc] initWithBookTitle:bookTitle];
[self presentViewController:bookViewController animated:YES completion:nil];
still presents a bookViewController with the old value for currentPage! This might be because I'm not declaring it as a property, nor initialising it in the init method... Any thoughts?

i try to understand initializers in obj-c and i can't

I try to understand how to work simple init funcion and I don't know where I have made a mistake. Can somebody assist?
Rectangle.h
#interface Rectangle : NSObject
{
int width;
int height;
}
-(id)initObject;
#end
Rectangle.m
#implementation Rectangle
-(id)initObject{
if (self = [super init]) {
height = 5;
width = 7;
}
return self;
}
#end
And in ViewController.h i import Rectangle.h, declare *rect object and in .m i execute(? run?) initObject.
ViewController.h
#import <UIKit/UIKit.h>
#import "Rectangle.h"
#interface ViewController : UIViewController
{
Rectangle *rect;
}
#end
ViewController.m
-(void)viewDidLoad
{
rect = [[Rectangle alloc] initObject];
NSLog(#"%#", rect);
[super viewDidLoad];
}
initObject return me:
2011-11-21 09:43:02.625 initializers[43693:f803] <Rectangle: 0x6ab1660>
The only problem with your code that I can see is you called your initializer -initObject for no good reason. It's not taking any parameters at all, so you really should just call it -init like every other parameterless initializer in the system.
As for the log output, I imagine your confusion lies in the fact that it says <Rectangle: 0x6ab1660>. This is perfectly normal. The default implementation of -description (the method that returns this output) is the name of the class of the object followed by the object's address. In other words, -[NSObject description] is likely to be implemented something like the following:
- (NSString *)description {
return [NSString stringWithFormat:#"<%#: %p>",
NSStringFromClass([self class]),
self];
}
This means that instance variables of your object are not going to be printed. A number of built-in classes do print their instance variables when logged, but this was implemented specifically for that class and is not a generic mechanism. If you want to verify that your Rectangle object is correct, you could implement -description like so:
- (NSString *)description {
return [NSString stringWithFormat:#"<%#: %p width=%d, height=%d>",
NSStringFromClass([self class]),
self,
width,
height];
}
No error! Since your Rectangle class has no description method, calling NSLog(#"%#", rect); will return the class of the object, followed by its address in memory.
If you want to print width and height of the rectangle you may use something like:
in Rectangle.h
#interface Rectangle : NSObject
{
int width;
int height;
}
-(id)initObject;
#property int width, height;
#end
in Rectangle.m
#implementation Rectangle
#synthesize width, height;
-(id)initObject{
if (self = [super init]) {
height = 5;
width = 7;
}
return self;
}
#end
and then call
NSLog(#"width=%d, height=%d", [rect width], [rect height]);

Set value of NSTextField

I am trying to set the value of an NSTextField, but it's not working properly.
I have a button linked to an IBAction, and when I set it using self, it works fine:
#import <Foundation/Foundation.h>
#interface TestMessage : NSObject {
IBOutlet NSTextField *text;
}
- (IBAction) setMessage: (id) controller;
- (void) Message:(NSString *) myMessage;
#end
#import "TestMessage.h"
#implementation TestMessage
- (IBAction) setMessage: (id) controller {
// This works
[self Message:#"Hello"];
// but this doesn't
TestMessage * messageTest= [TestMessage new];
[messageTest Message:#"Hi"];
}
- (void) Message: (NSString *) myMessage {
[text setStringValue: myMessage];
NSLog(#"Message Was Called");
// This returns <NSTextField: 0x1001355b0> when called
// using self, but null when called the other way.
NSLog(#"%#", text);
}
#end
I've searched for a while, but still can't find the answer.
I guess it has something to do with the delegate, but I'm not sure.
Thanks in advance.
Are you sure message is called when you call it from anotherFuntion? If anotherFuntion is a method of another class, calling [self message:] won't work as you expected to...
I know this is an old post, but I have been fiddling with the same issue today.
You have to return string value in textfield:
[textField stringValue];
The code
TestMessage * messageTest = [TestMessage new];
is unusual, specifically new. I'm going to assume that new is just a class method does normal alloc/init equivalent to
TestMessage * messageTest = [[TestMessage alloc] init];
The main problem is that IBOutlet NSTextField *text will be initialized only if the class TestMessage is loaded with a Nib file. It would have to be named as the class of an object in Interface Builder, like so
and you would have to implement initWithCoder and encodeWithCoder something like this in order to extract your field value from the IB encoding:
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
self.text = [coder decodeObjectForKey:#"text"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeObject:self.text forKey:#"text"];
}
Fundamentally, IBOutlet fields do not get wired up wherever you create an instance of that class. If they did, how would you express that field A should be wired to UI object A and field B should be wired to UI object B? The connection is established only in the context of loading a class from a Nib file.

How to assert a UILabel.text property is equal to an instance of NSString in objective-c

I'm new to objective-c and I'm finding that I don't know how to correctly assert that a text property on some given label is equal to a raw string value. I'm not sure if I just need to cast the label as NSString or if I need to modify my assert statement directly.
#interface MoreTest : SenTestCase {
MagiczzTestingViewController* controller;
}
- (void) testObj;
#end
#implementation MoreTest
- (void) setUp
{
controller = [[MagiczzTestingViewController alloc] init];
}
- (void) tearDown
{
[controller release];
}
- (void) testObj
{
controller.doMagic;
STAssertEquals(#"hehe", controller.label.text, #"should be hehe, was %d instead", valtxt);
}
#end
The implementation of my doMagic method is below
#interface MagiczzTestingViewController : UIViewController {
IBOutlet UILabel *label;
}
#property (nonatomic, retain) UILabel *label;
- (void) doMagic;
#end
#implementation MagiczzTestingViewController
#synthesize label;
- (void) doMagic
{
label.text = #"hehe";
}
- (void)dealloc {
[label release];
[super dealloc];
}
#end
The build is fine when I modify the assert to compare a raw NSString to another but when I try to capture the text value (assuming it's of type NSString) it fails. Any help would be much appreciated!
STAssertEquals() checks for identity of the two values provided, so it's equivalent to doing this:
STAssertTrue(#"hehe" == controller.label.text, ...);
Instead, you want STAssertEqualObjects(), which will actually run an isEqual: check like the following:
STAssertTrue([#"hehe" isEqual:controller.label.text], ...);
You need to load the nib of the view controller. Otherwise there won't be any objects for the label outlet to be hooked up to.
One way to do this is to add an ivar for the view controller's view to your test case:
#interface MoreTest : SenTestCase {
MagiczzTestingViewController *controller;
UIView *view;
}
#end
#implementation MoreTest
- (void)setUp
{
[super setUp];
controller = [[MagiczzTestingViewController alloc] init];
view = controller.view; // owned by controller
}
- (void)tearDown
{
view = nil; // owned by controller
[controller release];
[super tearDown];
}
- (void)testViewExists
{
STAssertNotNil(view,
#"The view controller should have an associated view.");
}
- (void)testObj
{
[controller doMagic];
STAssertEqualObjects(#"hehe", controller.label.text,
#"The label should contain the appropriate text after magic.");
}
#end
Note that you also need to invoke super's -setUp and -tearDown methods appropriately from within yours.
Finally, do not use dot syntax for method invocation, it is not a generic replacement for bracket syntax in message expressions. Use dot syntax only for getting and setting object state.