0

I have set up an UIScrollView with an UIView (contentViewForScroll) to hold all the content, as indicated in http://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/ , IB is not showing any warning about misplacing, expected widths, and so on....

But when I tried to add programmatically the constraints to set the width of the "contentViewForScroll" on viewDidLoad, I got a weird error description, after google it I found it means your object's property is returning "nil". That's exactly what was happening!! Then I realized that I have an UILabel not changing its value when I modified its property. The other UILabels and UIButtons changed their text and title properties as expected.

I have tried renaming both controls (that is why it has such a long name), changing definition in .h file or in .m file. Nothing works.

¿any ideas?

This is my code in .m file:

@interface MarketsViewController ()
    @property (strong, nonatomic) IBOutlet UILabel *labelMarketName;  // This is returning nil
    @property (strong, nonatomic) IBOutlet UIImageView *imageVenueCover;
    @property (strong, nonatomic) IBOutlet UILabel *labelMarketAddress;
    @property (strong, nonatomic) IBOutlet UILabel *labelMarketCity;
    @property (strong, nonatomic) IBOutlet UIView *contentViewForScroll; // This is returning nil
@end

@implementation MarketsViewController
    - (void)viewDidLoad {
        [super viewDidLoad];

        NSLog(@"UIView: %@", self.contentViewForScroll);   // this is returning "UIView: (null)"
        NSLog(@"UILabel: %@", self.labelMarketName);   // this is returning "UILabel: (null)"
    }
@end

Thanks

raululm
  • 105
  • 1
  • 1
  • 10

1 Answers1

0

Did you link the objects in storyboard to the outlets? To do so, you can open up the assistant editor and ctrl drag from the object in storyboard to the outlet in the code above. Or you can drag from the object to file owner and select the outlet from there.

One thing to note is that usually you want to make your IBOutlets weak rather than strong as you have above.

Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
  • Yes, I have added via ctrl+drag, and the little circle besides the property declaration is "gray". – raululm Jan 05 '15 at 22:22
  • At the view controller's class in Storyboard is MarketsViewController? – Josh Gafni Jan 05 '15 at 22:24
  • Ahh... actually viewDidLoad is called before the properties are set. Try putting the code in awakeFromNib. I forgot about this :) http://stackoverflow.com/questions/6302629/viewdidload-and-awakefromnib-timing – Josh Gafni Jan 05 '15 at 22:26
  • Is in xib file, and yes is the view controller's class, and as I said, the other UILabels are responding as expected. Very strange. – raululm Jan 05 '15 at 22:26
  • I ve putted also in "willViewAppear", but I'm going to try your suggestion. But whay then the other labels are working fine? – raululm Jan 05 '15 at 22:28
  • Ahh actually wait a second. I noticed you are setting a property for the contentView. Usually you would want to set a property for the scrollView (which automatically contains a contentView). You just need to make sure to set the contentSize property. – Josh Gafni Jan 05 '15 at 22:36
  • Yes Joshua, but as you can see in the link provided in the question, and this technical note of apple https://developer.apple.com/library/ios/technotes/tn2154/_index.html , when you are using autolayout you do not set directly contentSize, you use a contentView with constraints to all the edges of the scroll view, and autolayout will figure it out the contentSize automatically. :) – raululm Jan 05 '15 at 22:41
  • When you look at the outline, are all of the objects subviews of the main view? – Josh Gafni Jan 05 '15 at 22:47
  • Yes, actually the view tree is as this: view ....scrollView ........view (contentViewForScroll) ..............The rest of the controls at this level – raululm Jan 05 '15 at 22:49
  • Hmm... I cannot seem to figure out why the contentView would be nil. Could you paste the exact error you are getting? Also, this is how I would normally do the layout: https://www.youtube.com/watch?v=WkJrzhMY-Pc You don't really need an outlet to the contentView to do the autolayout you require. – Josh Gafni Jan 05 '15 at 22:59
  • There is no "error", as both objects are "nil", no matter which method you invoke on them...it won't give any error! Thanks Joshua, I m going to watch your video :) – raululm Jan 05 '15 at 23:30
  • So strange... I wouldn't mind looking at your code tonight. Feel free to send to me. – Josh Gafni Jan 05 '15 at 23:39
  • Joshua, I think I ve found the problem, I m testing the app in an iPhone 4. The project set up is very old (the older developers didn't update their templates), and yesterday I was playing to try to get the support for 64 bits (mandatory to send app to iTunes starting Feb 1st)...Well,I ve been working in a new xib, and modified another one adding a UIButton to it, whe I run the code...the UIButton does not appear...and I started to think that the version running on my iPhone is old...so thats why it is not working. may be? Do you know how to reset the app settings to default of xcode 6? – raululm Jan 06 '15 at 00:18
  • is it an xib file rather than a storyboard? If so, I have found it difficult to do the conversion in the project itself. You might want to start a new project, copy in the classes you need (the .h and .m files) but use the storyboard file rather than the xib. You would need to create the xib file in one of the scenes of the storyboard and reconnect your outlets. – Josh Gafni Jan 06 '15 at 00:36
  • Yes, I have the idea of switching to storyboard for an upcoming version, but I need to send this version to the app store, I m close to the deadline. Definately, I m going to copy to a new fresh project, this old one has give me enough headaches! Is there any way to reward your attention? By the way, I've tried the simulator, and all is working perfectly, it looks that all the problem was that I was not getting installed last version of the xibs, but yes of the code...very strange, and with no errors. – raululm Jan 06 '15 at 00:50