Question

I'm very new to iOS programming and I'm trying to learn how to add a label and a button to a view without using the storyboard. It's not going very well for me however. I get no errors but neither of my objects are showing when I run the code.

This is my .h file:

@interface SomeViewController : UIViewController

@property (weak, nonatomic) UILabel *quoteLabel;
@property (weak, nonatomic) UIButton *helloButton;

@end

And this is the code i've got in my .m file:

- (void)loadView {
    [super loadView];
// Should i override this loadView in order to add more graphical elements? How would i do that?

// Creating a view here which i thought i could add my button and label to. 
// Is this the right way to go or is it unnecessary? 

UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    self.view = contentView;

// Creating my buttonWithType 
// (does this also make my button allocated and initiated or do i need to do that manually?)
// Is this unnecessary considering iOS 7's default buttons is just a text?

self.helloButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.helloButton.frame = CGRectMake(100.0, 35.0, 120.0, 30.0);
    [self.helloButton setTitle:@"Hello" forState:UIControlStateNormal];
    [self.helloButton addTarget:self
                         action:@selector(helloButtonPressed)
               forControlEvents:UIControlEventTouchUpInside];
    self.helloButton.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

// Try to add this button to the view, but it won't show.
    [contentView addSubview:self.helloButton];

// Same issue for my label object.

    self.quoteLabel.frame = CGRectMake(55.0, 85.0, 210.0, 21.0);
    self.quoteLabel.textAlignment =  NSTextAlignmentCenter;
    self.quoteLabel.textColor = [UIColor whiteColor];
    self.quoteLabel.backgroundColor = [UIColor blackColor];
    self.quoteLabel.font = [UIFont fontWithName:@"System" size:(17.0)];
    self.quoteLabel.text = @"Test";

// Trying to add it to the contentView
    [contentView addSubview:self.quoteLabel];

I would very much like to know how to add those elements, which have been declared as a property in the .h, to the view. Doing something like the following:

UILabel *someLabel = [ [UILabel alloc ] initWithFrame:CGRectMake(55.0, 85.0, 210.0, 21.0) ];
    someLabel.textAlignment =  NSTextAlignmentCenter;
    someLabel.textColor = [UIColor whiteColor];
    someLabel.backgroundColor = [UIColor blackColor];
    someLabel.font = [UIFont fontWithName:@"System" size:(18.0)];
    [contentView addSubview:someLabel];
    someLabel.text = @"Test";

Works fine already.

Was it helpful?

Solution

// Creating my buttonWithType 
// (does this also make my button allocated and initiated or do i need to do that manually?)
// Is this unnecessary considering iOS 7's default buttons is just a text?

Yes, buttonWithType does allocate and initiate the button.

I think buttons have more states so they are different.

I don't see where quoteLabel is initialized though.

You should probably be using strong instead of weak references in your .h file and you may have to refresh the view after adding subviews to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top