Search code examples
iphoneobjective-ciosios4xcode4

Making Progress View Visible


I'm working on a very simple iOS app to get me started programming. It's a tab based application, so there is a MainWindow.xib along with a FirstView.xib and SecondView.xib. All of this is happening in the first view. I want to add a Progress bar to the First View, and when I added the object it attached itself to FirstView.xib and shows up and lets me move it around. To test, the alpha is set at 1.00 and the progress is set at 0.5. Regardless of this, it doesn't show up no matter what I do. What am I doing wrong?

AppDelegate.m:

@synthesize window=_window;

@synthesize tabBarController=_tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Add the tab bar controller's current view as a subview of the window
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
}

- (void)applicationWillTerminate:(UIApplication *)application
{
}

- (void)dealloc
{
    [_window release];
    [_tabBarController release];
    [super dealloc];
}
@end

FirstViewController.h:

#import <UIKit/UIKit.h>
NSTimer *stopWatchTimer;
NSDate *startDate;

@interface FirstViewController : UIViewController {

    UILabel *label;
    UILabel *stopWatchLabel;
    UIProgressView *progressBar;
    UIButton *topButton;
}
@property (nonatomic, retain) IBOutlet UILabel *stopWatchLabel;
@property (nonatomic, retain) IBOutlet UIProgressView *progressBar;
- (IBAction)onStartPressed:(id)sender;
- (IBAction)onStopPressed:(id)sender;
- (IBAction)onResetPressed:(id)sender;

@end

FirstViewController.m

#import "FirstViewController.h"


@implementation FirstViewController
@synthesize progressBar;
@synthesize stopWatchLabel;


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


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload
{
    [self setStopWatchLabel:nil];
    [topButton release];
    topButton = nil;
    [super viewDidUnload];

    // Release any[progress release];
    progressBar = nil;
    [progressBar release];
    progressBar = nil;
    [self setProgressBar:nil];
    //retained subviews of the[self setProgressBar:nil];

    // e.g. self.myOutlet = nil;
}

static NSInteger counter=0;
static NSInteger secs=0;
static NSInteger mins=0;
static NSInteger hrs=0;

- (void)dealloc
{
    [stopWatchLabel release];
    [label release];

    [topButton release];
    [super dealloc];
}

-(void)updateTimer {
//updates the timer    }

    -(void)clearTimer {
//clears the timer        


}

-(void)stopTimer{
//stops the timer    }


- (IBAction)onStartPressed:(id)sender {
    //stopWatchLabel.text=@"Start Pressed";
    progressBar.alpha=1.0;
//run timer    }


- (IBAction)onStopPressed:(id)sender {
    [self stopTimer];
}

- (IBAction)onResetPressed:(id)sender {
    [self stopTimer];
    [self clearTimer];
}
@end

Solution

  • There are two things you need to do with a view after it's been created: You have to add it as a subview of a visible view and set a proper frame.

    Here's how to do that:

     [self.view addSubview:progressBar];
    progressBar.frame = CGRectMake(x, y, width, height);
    

    EDIT: Probably completely unrelated to your question, but this is all wrong:

    - (void)viewDidUnload
    {
        [self setStopWatchLabel:nil];
        [topButton release];
        topButton = nil;
        [super viewDidUnload];
    
        // Release any[progress release];
        progressBar = nil;
        [progressBar release];
        progressBar = nil;
        [self setProgressBar:nil];
        //retained subviews of the[self setProgressBar:nil];
    
        // e.g. self.myOutlet = nil;
    }
    

    You probably want it to look something like the following:

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        [self setStopWatchLabel:nil];
        [topButton release];
        topButton = nil;
        [label release];
        label = nil;
        self.progressBar = nil;
    }
    

    Make sure you understand what you did wrong. It's very important that you get this right or your app will leak and/or crash.

    The rest of your code doesn't really do anything. You seem to be doing everything in IB, so I guess that's where your problem is.