0

I am using presentModalDialog to show a viewcontroller's view however the viewDidLoad never gets called?!

Is there any method that is called where I can place my logic that fills and configures the view?

EDIT:

It's a bit difficult to put a small amount of code, you kind of need to see it all, but here goes:

I have 2 nibs and 2 view controllers (portrait-mainvc/landscape) which both inherit from 1 baseclass which has the logic and the iboutlets. This is to allow me to re-use code. When the orientation changes in the main controller, it switches between the 2 controllers (modal dialog) which in turn use their respective nib's however they all use the same base code to configure the UI items.

@implementation HomeViewControllerBase

- (void)configureBestSellItems
{
    [self startRetrievingRegions];

    // load all the images from our bundle and add them to the scroll view
    //  NSUInteger i;
    for (int i = 0; i <= 150; i++)
    {
        NSString *imageName = @"tempImage.jpg";
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
        CGRect rect = imageView.frame;
        rect.size.height = kScrollObjHeight;
        rect.size.width = kScrollObjWidth;
        imageView.frame = rect;
        imageView.tag = i;  // tag our images for later use when we place them in serial fashion
        [self.bestSellScrollView addSubview:imageView];
        [imageView release];
    }

    [self layoutScrollImages];  // now place the photos in serial layout within the scrollview
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configureBestSellItems];
}

@end

@interface HomeViewController : HomeViewControllerBase
{

}

@interface HomeViewController_Landscape : HomeViewControllerBase
{

}

@implementation HomeViewController

//This works for portrait
- (void)viewDidLoad
{
    [super viewDidLoad];
}

@end

@implementation HomeViewController_Landscape

//This does not work
- (void)viewDidLoad
{
    [super viewDidLoad];
}

//This works as suggested
- (void)awakeFromNib
{
    [super configureBestSellItems];
}

@end
TheLearner
  • 19,387
  • 35
  • 95
  • 163
  • Could you provide some code? Sounds strange, have you remembered to call the super class implementation of viewDidLoad? – LuckyLuke Apr 11 '11 at 19:55
  • +1 for posting some code. `viewDidLoad` will always be called so something's up in your implementation. – XJones Apr 11 '11 at 20:59
  • @Andreas - I have added some code, am keen to hear your opinion – TheLearner Apr 12 '11 at 08:33
  • Nothing seems to be wrong at the code you provided... And your app launches correctly? And you probably have tested with a NSLog in both the base controller and the derived one straitght after the super invocation? – LuckyLuke Apr 12 '11 at 11:57

3 Answers3

0

It's a common problem in objective c: you should review the flowing:

1-initiation of the view controller you should initialize it in the right way in app delegate. 2-review the file owner of the view it must be the right class which usually the same name of uiview.

Mohammed
  • 334
  • 1
  • 5
  • 22
0

You can try these in your viewcontroller:

-(void)awakeFromNib
{
   [super awakeFromNib];
   // your code here
}

or

-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    // your code here
}

However, you should try to find why viewDidLoad is never called. You can also check this link.

Community
  • 1
  • 1
quaertym
  • 3,917
  • 2
  • 29
  • 41
  • Don't really like this answer. `awakeFromNib` may work but `viewWillAppear` will have undesirable side effects as the view can appear/disappear w/o needing to be reloaded. It would be far better to figure out why `viewDidLoad` "isn't being called." Hard to say w/o seeing some code. That's the proper place to modify the view whenever it is loaded. – XJones Apr 11 '11 at 20:58
  • Another reason I don't like `awakeFromNib` is it's specific to using IB. If you're like me and you eventually decide to create all of your views programmatically, `awakeFromNib` will no longer be called. If you fix and use `viewDidLoad` you won't have to change code after replacing what IB creates with `loadView`. If you do try use `awakeFromNib`, make sure to call `[super awakeFromNib]` as the first line. – XJones Apr 11 '11 at 21:11
  • this works but am keen to hear what @XJones thinks of my edited post – TheLearner Apr 12 '11 at 08:33
0

You may have a custom function in there named presentModalDialog:, as searching apple's docs doesn't have that function. You're calling a custom function that loads a Xib.

I'm doing the same thing.

You want this:

[self presentModalViewController:controller...

ViewDidLoad is called by Controllers, if the awakeFromNib is being called, you're probably getting, by itself, the view.

Stephen J
  • 2,367
  • 2
  • 25
  • 31