So, here's the issue. I'm relatively new to iOS programming, and I've taken on a giant project.
I'm working on a game with multiple levels which basically follow the same pattern, but have different sprite images and values, and I just decided to lay out all the levels in IB for speed's sake (not necessarily best practices, I know, but work with me). Each "level" has its own view controller, along the lines of "FireLevel1ViewController," "FireLevel2ViewController," etc. All of these view controllers inherit from a custom subclass of UIViewController I created called "GameController."
My problem is, when I open each level on my test device, GameController's viewDidLoad is getting called before the init or viewDidLoad methods of my subclass controllers, and so none of my level images/values are getting assigned to the superclass properties. Specifically, I have a pause menu that ought to be hidden at the outset of the level (I am doing setHidden in GameController's viewDidLoad), but since GameController's viewDidLoad runs before FireLevel1 has a chance to associate the correct IB property with PauseMenu, GameController just hides an empty view, and the actual PauseMenu never gets hidden.
I may have multiple problems going on here, but mostly I think I'm not really understanding correctly how to subclass a subclass of UIViewController and how to get the second subclass's properties/values/images to work in the first subclass's methods.
Thanks so much for any help! I hope that question made sense...
Code for GameController:
#implementation GameController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:pauseMenu];
[pauseMenu addSubview:helpMenu];
//Hides the pause and help menus until their respective buttons are pressed
[pauseMenu setHidden:YES];
[helpMenu setHidden:YES];
isPaused = NO;
}
Code for FireLevel1Controller:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self)
{
theMainview = mainview;
theScene = scene;
theBG = bg;
theHudView = hud;
thePauseView = pauseMenu;
theHelpView = helpMenu;
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
firstTurret = [[StationaryEnemy alloc]init:turretImage1 baseView:base1];
secondTurret = [[StationaryEnemy alloc]init:turretImage2 baseView:base2];
NSLog(#"I'm in view did load");
}
Did you try using viewWillAppear? That method should be called after all the visible UI elements have been initialized.
Ahhhh I figured it out - I was accidentally negating my variables. I should've been doing mainview = theMainview; instead of theMainview = mainview - I was just assigning them all to zero. I also moved them all out of init into viewDidLoad, and moved [super viewDidLoad] underneath them, and now it works perfectly!
Related
Prior to storyboards, and working with .xib files, I used this piece of code to do screen adjustments during init.
- (id)initForNewItem:(BOOL)isNew {
self = [super initWithNibName:#"NAME" bundle:nil];
if (self) {
if (isNew) {
// Place some buttons only when isNew is true
}
}
return self;
}
Then I also implemented this to generate an exception when initWithNibName is directly called because I wanted to avoid that:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
#throw [NSException exceptionWithName:#"Wrong initializer" reason:#"Use initForNewItem:" userInfo:nil];
return nil;
}
Then another viewcontroller could call the custom init and the screen would be set:
MyViewController *myViewController = [[MyViewController alloc] initForNewItem:YES]; // Or NO ofcourse depending on the situation.
Now I'm using storyboard and initWithNibName is never called. Instead only initWithCoder is called but this method can only be called by the storyboard right? So how would I do something similar while using storyboard?
You wouldn't do this with storyboards.
The way to do it is to use properties to give the correct values once the view controller has been initialised.
This is done in - (void)prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender.
You can access segue.destinationViewController and then use this to put values in to.
I am having a weird error with NSViewController where if I allocate a view using the viewcontroller's regular init message, the view created is not my view, but when using the default NIB name, it does work.
Specifically, this code works all the time. It creates the view defined in the nib file, and displays it in the parentView.
+ (void)createTransparentViewCenteredInView:(NSView*)parentView withText:(NSString*)text duration:(NSTimeInterval)duration {
TransparentAccessoryViewController* controller = [[TransparentAccessoryViewController alloc] initWithNibName:#"TransparentAccessoryViewController" bundle:nil];
NSLog(#"%#", [controller.view class]); // Returns "TransparentAccessoryView" -- CORRECT
[parentView addSubview:controller.view];
}
However, the following code works SOME of the time (which is weird in that it doesn't always fail). With some parentViews, it works perfectly fine, and with others, it doesn't. The parent views are just random custom NSViews.
+ (void)createTransparentViewCenteredInView:(NSView*)parentView withText:(NSString*)text duration:(NSTimeInterval)duration {
TransparentAccessoryViewController* controller = [TransparentAccessoryViewController new];
NSLog(#"%#", [controller.view class]); // Returns "NSSplitView" -- INCORRECT
[parentView addSubview:controller.view];
}
The errors that comes up are as follows (I have no idea why it is bringing up an NSTableView, as I don't have an NSTableView here at all. Also, it is weird that it complains about an NSTableView when the type it prints is an NSSplitView):
2013-04-07 21:33:12.384 Could not connect the action refresh: to
target of class TransparentAccessoryViewController
2013-04-07 21:33:12.384 Could not connect the action remove: to target
of class TransparentAccessoryViewController
2013-04-07 21:33:12.385 * Illegal NSTableView data source
(). Must implement
numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:
The NIB file defines a custom subclassed NSView, called TransparentAccessoryView, and hooks this up to the File Owner's view property, standard stuff (all I did was change the custom class name to TransparentAccessoryView). I added an NSLog's to see what was going on, and for some reason, in the second case, the view class type is incorrect and thinks it is an NSSplitView for some reason. The ViewController class is as follows:
#implementation TransparentAccessoryViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
- (void)awakeFromNib {
self.textField.stringValue = #"";
}
+ (void)createTransparentViewCenteredInView:(NSView*)parentView withText:(NSString*)text duration:(NSTimeInterval)duration {
TransparentAccessoryViewController* controller = [[TransparentAccessoryViewController alloc] initWithNibName:#"TransparentAccessoryViewController" bundle:nil];
NSLog(#"%#", [controller.view class]);
[parentView addSubview:controller.view];
}
#end
I thought that the default init message triggers the viewcontroller to load the NIB named after the viewcontroller, which seems to be the case some of the time as the second version of my code works in certain conditions.
Does anyone know why this behavior is occurring at all?
From the docs:
If you pass in a nil for nibNameOrNil then nibName will return nil and
loadView will throw an exception; in this case you must invoke
setView: before view is invoked, or override loadView.
Therefore, if you're initializing a NSViewController with -init, you should call -setView: to set the view controller's view, or override -loadView. In the latter case, you could certainly implement the UIViewController-like behavior that you're probably expecting -- if nibNameOrNil is nil, try to load a nib that has the same name as the class.
I think that when you call init on a NSViewController, you're assuming that the implementation of init for NSViewController searches for a nib with the same name as the view controller and uses it. However, this is undocumented API or at least I can't seem to find any documentation supporting that assumption. The link you posted on your comments doesn't cite any documentation either and even reiterates that this is undocumented and that Apple could change this implementation at any point.
I think to assure that your code works in future versions of the SDK (and since it is already creating undesired behavior), you should not rely on this assumption. To achieve the same outcome simply override init and initWithNibName:bundle: in such a way as explained by this post:
#implementation MyCustomViewController
// This is now the designated initializer
- (id)init
{
NSString *nibName = #"MyCustomViewController";
NSBundle *bundle = nil;
self = [super initWithNibName:nibName bundle:bundle];
if (self) {
...
}
return self;
}
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
// Disregard parameters - nib name is an implementation detail
return [self init];
}
I've been at this for a few hours now. I've got several View Controllers in this project and not a single one is causing issues but now all of a sudden this new one is. I even deleted it and made a "Test" View Controller, but no dice. The best I can tell it is not actually creating its view, thus when the view is referenced the app crashes. The test VC has no added or deleted code except for a log statement in the -viewDidLoad method. I am not overriding -loadView. I have tried adding the view to a subview, have tried pushing the VC into the Navigation Controller, I have even tried simply logging test.view. I have tried creating the VC with a NIB and have tried it without one. Absolutely nothing works at all. Any help will be appreciated.
Where VC is being created inside of another VC. The log statment causes the crash. But so does adding as a subview and even pushing into nav controller.
TestViewController *test = [[TestViewController alloc] init];
NSLog(#"test view = ", test.view);
Implementation of TestViewController.
#implementation TestViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"view = %#", self.view);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I'd say to double check all the connections from the view. Especially if its been copied or moved as its easy to leave a connection to an old VC
if you are not using ARC in your project then dont check the autolayout box for the nib file. If you are using ARC then the connections of it to the file's owner can be the only issue.
if you use xib,and the items is copied from other xib or storyboard ,make sure the root view is exist,and drag tothe file's owner.
Is it possible to, say, use a block as a completion handler in a View Controller's init method so that the parent view controller is able to fill in the details in a block without having to create a custom initWithNibName:andResourceBundle:andThis:andThat: for each possible properties ?
// ... in the didSelectRowAtIndexPath method of the main view controller :
SubViewController *subviewController = [[SubViewController alloc] initWithNibName:nil bundle:nil completionHandler:^(SubViewController * vc) {
vc.property1 = NO;
vc.property2 = [NSArray array];
vc.property3 = SomeEnumValue;
vc.delegate = self;
}];
[self.navigationController pushViewController:subviewController animated:YES];
[subviewController release];
in SubViewController.m :
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil completionHandler:(void (^)(id newObj))block {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
block(self);
}
return self;
}
instead of
// ... in the didSelectRowAtIndexPath method of the main view controller :
SubViewController *subviewController = [[SubViewController alloc] initWithNibName:nil bundle:nil andProperty1:NO andProperty2:[NSArray array] andProperty3:SomeEnumValue andDelegate:self];
[self.navigationController pushViewController:subviewController animated:YES];
[subviewController release];
with
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andProperty1:(BOOL)p1 andProperty2:(NSArray *)p2 andProperty3:(enum SomeEnum)p3 andDelegate:(id<MyDelegateProtocol>)myDelegate {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.property1 = p1;
self.property2 = p2;
self.property3 = p3;
self.delegate = myDelegate;
}
return self;
}
So that I can do whatever I want in the main controller vs calling a predefined init method (and having to write one for each possible initialization).
Is it something bad ? will there be retain cycles ?
Which advantages do you see in using a block? The initializer is commonly used to set up private state of the instance. This private state could not be accessed from the block since the block is implemented somewhere else.
If you only use public properties why not setting them up after initialization?
SubViewController *vc = [[SubViewController alloc] initWithNibName:nil bundle:nil];
vc.property1 = NO;
vc.property2 = [NSArray array];
vc.property3 = SomeEnumValue;
vc.delegate = self;
That's exactly what the block version does (without the hassle).
Is it something bad ? will there be retain cycles ?
No, but I would dismiss your proposition for architectural reasons: You are either breaking encapsulation of the class or do not gain any advantage over just doing what the block does after initialization.
The questions are:
What is the benefit of this way?
Will you ba able to manage this code efficient?
Be aware that when program is growing new levels of invocation will be added and this will make your code hard to read, maintain, extend or develop. Think about future subclassing as well and how will you debug this code in the future to find some mismatched value.
Blocks can make you code faster, but delegate pattern will make your code clean and run in one thread, which is easy to maintain in the future, and this is real value for the professional programmer.
You could define your
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil completionHandler:(void (^)(id newObj))block;
method in a category of UIViewController; from there you would call initWithNib and then execute your completion block on the just allocated self:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil completionHandler:(void (^)(id newObj))block
{
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
block(self);
}
return self;
}
I think this should work fine.
This is possible, no retain problems. Everything will be called synchronously on the same thread.
BUT
What is the benefit of not doing this the simple way - calling another method after init, e.g.
MyController* controller = [[[MyController alloc] init] autorelease];
[self updateController:controller];
Does the code have be called from the init method?
In general, I would advice to create separate init... methods if you want to initialize the object in different ways.
i'm really not sure why you would take this approach (unless you want to get the attention of the OOD police). it's not a good idea.
your controller can define a function or method which returns an instance, initialized the way it wants instead.
i started a single view template in Xcode 4.2(recently upgraded to Xcode 4.2 and ios5)
so now i have only one view controller.
I added a new class to the project which is a subclass of UIViewcontroller.
Now in the main controller class viewdidLoad method
- (void)viewDidLoad
{
// Override point for customization after application launch.
[super viewDidLoad];
[self presentQuizcontroller];
}
-(void) presentQuizcontroller
{
_QuizController = [[[Quiz alloc] initWithNibName:#"Quiz" bundle:nil] autorelease];
_QuizController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:_QuizController animated:YES]; // Do any additional setup after loading the view, typically from a nib.
}
the problem is in my Quiz class
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
the initWithNibName method does get called(i checked by using breakpoint) but it doesn't passes the condition of if(self) . and hence the view don't appears.
Any ideas?
Edit
After the first answer i tried this way too
- (void)viewDidLoad
{
// Override point for customization after application launch.
[super viewDidLoad];
}
-(void) presentQuizcontroller
{
_QuizController = [[[Quiz alloc] initWithNibName:#"Quiz" bundle:nil] autorelease];
_QuizController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:_QuizController animated:YES]; // Do any additional setup after loading the view, typically from a nib.
}
-(void) awakeFromNib
{
[self presentQuizcontroller];
}
same thing Quiz.m initwithnib name method does not passes the condition if(self).
I think you need to use awakefromnib.
Here is the Link for another StackOverflow post if you want to read more.
PresentModalViewController is depreciated, I think you should now use presentViewController instead of it.
Are you sure that "Quiz" is the name of your file? That string should be same as the name of your xib file, namely something like "QuizController" or "QuizViewController"
Make sure the xib file is properly connected to header/implementation files by checking:
Owner of the xib file should be set as the viewController
View on the xib file (the one above all if you have multiple views) should be connected to viewControllers view.