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?
Related
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.
A really strange problem. I have to init an array in - (void)viewDidLoad.
The array, prjMemberArray is declared as a property:
#property(nonatomic, retain) NSMutableArray* prjMemberArray;
If I use this
prjMemberArray = [[NSMutableArray alloc] initWithObjects:#"someone",#"someone",#"someone" ,nil];
with release called in viewDidUnload,
then when the view loaded , it will crashes immediately But when I use this:
prjMemberArray = [[NSMutableArray alloc] initWithCapacity:0];
[prjMemberArray addObject:#"someone"];
it works well. Can anyone explain this? I use a storyboard to present the current view controller, like this:
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
prj_Detail = [sb instantiateViewControllerWithIdentifier:#"ProjectDetailVC"];
[self presentModalViewController:prj_Detail animated:YES];
Where prjMemberArray is a property of prj_Detail.
Are you sure you have not misspelled items and written e.g. "someone" instead of #"someone" in the crashing scenario?
Don't forget to use self when referring to properties. Here's the a safe way to declare that without having to worry about leaks:
Header:
#property(nonatomic, retain) NSMutableArray* prjMemberArray;
Implementation:
#synthesize prjMemberArray=_prjMemberArray;
- (void) viewDidLoad {
[super viewDidLoad];
NSMutableArray *prjMemberArray = [[NSMutableArray alloc] initWithObjects:#"someone", #"someone", #"someone" ,nil];
self.prjMemberArray = prjMemberArray;
[prjMemberArray release];
}
- (void) dealloc {
[_prjMemberArray release];
[super dealloc];
}
#property creates the getter and setter for your variable but is often confused for a variable itself. When they released XCode4 I believe they added the ability to set what you want the instance variable to be named by doing:
#synthesize prjMemberArray=_prjMemberArray;
Before XCode4 you simply did:
#synthesize prjMemberArray;
So what #property is doing behind the scenes is a little something like this:
-(NSMutableArray*) prjMemberArray {
return _prjMemberArray;
}
-(void) setPrjMemberArray:(NSMutableArray *) val {
if( _prjMemberArray != nil )
[prjMemberArray release];
_prjMemberArray = [val retain];
}
So don't think of #property as a variable itself and remember to always use self when referring to them. That should save you a lot of pain and a few memory leaks as well.
I'm trying to assign data from a string to another string within a different viewcontroller however it seems that the data is not retained - i get a null response in NSLog. I would like to know why, thanks ..
Try changing the order a bit, as below, and use retain instead of copy:
SchoolDetailViewController *schoolController = [[SchoolDetailViewController alloc]initWithNibName:nil bundle:nil];
schoolController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
schoolController.courseDetails = #"passing new content";
[self presentModalViewController:schoolController animated:YES];
NSLog(#" %#",schoolController.courseDetails); // 'passing new content' is shown
.h
NSString *courseDetails;
#property (nonatomic, retain) NSString *courseDetails;
.m
#synthesize courseDetails;
- (void)viewDidLoad {
NSLog(#" text : %#",courseDetails); // returns null ... why?
[super viewDidLoad];
}
This should work.
Well that is because the viewDidLoad method is called when you present the view controller with or without animation.
So just flip these 2 statements
[self presentModalViewController:schoolController animated:YES];
schoolController.courseDetails = #"passing new content";
like this
schoolController.courseDetails = #"passing new content";
[self presentModalViewController:schoolController animated:YES];
And then check the results once again...
I have an object name usr. I want to change views and I want to pass it along to the new view. How can I pass this object?
RVUser *usr = [[RVUser alloc] init];
UIViewController* exampleController = [[exampleClass alloc] initWithNibName:#"RVListsController" bundle:nil];
if (exampleController) {
exampleController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:exampleController animated:YES];
if (exampleController.title == nil) {
NSLog(#"enters");
//exampleController.title = #"Revistas Destacadas";
}
[exampleController release];
}
}
One way of doing it is to declare a property of type RVUser on exampleClass and assign it to that property after creating exampleController.
You should set properties BEFORE using [self presentModalViewController:exampleController animated:YES];
RVUser *usr = [[RVUser alloc] init];
UIViewController* exampleController = [[exampleClass alloc] initWithNibName:#"RVListsController" bundle:nil];
if (exampleController) {
exampleController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
if (exampleController.title == nil) {
NSLog(#"enters");
//exampleController.title = #"Revistas Destacadas";
}
//TODO: Set exampleController properties, declared as #property (nonatomic, release) RVUser *passedUsr;
//exampleController.passedUsr = usr;
//[usr release];
[self presentModalViewController:exampleController animated:YES];
}
[exampleController release];
You'll want to declare the instance of ExampleController as your custom UIViewController instead of UIViewController - this will give you code completion and compile time warnings if you call a method/property on it that doesn't exit. Then, change your definition of exampleClass (read the naming convention guide also) to have a property of type RVUser, like so:
#property (nonatomic, retain) RVUser *user;
And in the implementation file:
#synthesize user;
Now you can pass your object to that controller before you display it:
ExampleController.user = usr;
You really should read the intro guides on the Apple developer site, they cover this and a lot more that you need to know if you want to write iOS apps.
I've been stuck on this for days and each time I come back to it I keep making my code more and more confusing to myself, lol. Here's what I'm trying to do. I have table list of charges, I tap on one and brings up a model view with charge details. Now when the model is presented a object is created to fetch a XML list of users and parses it and returns a NSMutableArray via a custom delegate. I then have a button that presents a picker popover, when the popover view is called the user array is used in an initWithArray call to the popover view. I know the data in the array is right, but when [pickerUsers count] is called I get an EXC_BAD_ACCESS. I assume it's a memory/ownership issue but nothing seems to help. Any help would be appreciated.
Relevant code snippets:
Charge Popover (Charge details model view):
#interface ChargePopoverViewController .....
NSMutableArray *pickerUserList;
#property (nonatomic, retain) NSMutableArray *pickerUserList;
#implementation ChargePopoverViewController
#synthesize whoOwesPickerButton, pickerUserList;
- (void)viewDidLoad {
JEHWebAPIPickerUsers *fetcher = [[JEHWebAPIPickerUsers alloc] init];
fetcher.delegate = self;
[fetcher fetchUsers];
}
-(void) JEHWebAPIFetchedUsers:(NSMutableArray *)theData {
[pickerUserList release];
pickerUserList = theData;
}
- (void) pickWhoPaid: (id) sender {
UserPickerViewController* content = [[UserPickerViewController alloc] initWithArray:pickerUserList];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:content];
[popover presentPopoverFromRect:whoPaidPickerButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
content.delegate = self;
}
User Picker View Controller
#interface UserPickerViewController .....
NSMutableArray *pickerUsers;
#property(nonatomic, retain) NSMutableArray *pickerUsers;
#implementation UserPickerViewController
#synthesize pickerUsers;
-(UserPickerViewController*) initWithArray:(NSMutableArray *)theUsers {
self = [super init];
if ( self ) {
self.pickerUsers = theUsers;
}
return self;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
// Dies Here EXC_BAD_ACCESS, but NSLog(#"The content of array is%#",pickerUsers); shows correct array data
return [pickerUsers count];
}
I can provide additional code if it might help. Thanks in advance.
You declare the ivar holding the array as this...
#property (nonatomic, retain) NSMutableArray *pickerUserList;
But then you have a method implemented like this:
-(void) JEHWebAPIFetchedUsers:(NSMutableArray *)theData {
[pickerUserList release];
pickerUserList = theData;
}
You aren't retaining theData and you aren't calling the synthesized setter. If you did Build and Analyze, it should catch this problem and tell you about it. If not, file a bug.