2011-04-13 63 views
0

我正在研究一個非常簡單的iOS應用程序,以使我開始編程。這是一個基於標籤的應用程序,因此有一個MainWindow.xib以及一個FirstView.xib和SecondView.xib。所有這些都發生在第一視圖中。我想向第一個視圖添加一個進度條,並且當​​我添加該對象時,它將自己附加到FirstView.xib並顯示並讓我移動它。爲了測試,alpha設置爲1.00,進度設置爲0.5。無論如何,無論我做什麼,它都不會出現。我究竟做錯了什麼?製作進度視圖可見

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 { 
    //[email protected]"Start Pressed"; 
    progressBar.alpha=1.0; 
//run timer } 


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

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

當我們不知道自己在做什麼時,我們應該如何知道自己做錯了什麼?如果您發佈一些代碼,幫助您更容易。否則,我們只是在黑暗中拍攝。 – 2011-04-13 20:21:18

+1

您要麼不將FirstView的視圖控制器添加到選項卡控制器,要麼因爲Erik提到不將進度條添加爲FirstView的子視圖。如果你做得很好,我們需要看到更多的信息,比如代碼來理解問題。 – 2011-04-13 20:31:36

+0

我將代碼添加到委託和視圖控制器。如果需要,我也可以將.xib文件附加到某處。 – Laxsnor 2011-04-13 20:41:22

回答

2

有你需要以做它已經經過了兩件事創建:您必須將其添加爲可見視圖的子視圖並設置適當的框架。

這裏是如何做到這一點:

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

編輯:可能完全無關你的問題,但是這是完全錯誤的:

- (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; 
} 

你可能希望它看起來有點像以下:

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

確保你明白你做錯了什麼。這很重要,否則你的應用程序將會泄漏和/或崩潰。

其餘的代碼並沒有真正做任何事情。你似乎在做IB的一切,所以我想這就是你的問題所在。

+0

我在第一視圖中看到三個按鈕和標籤,是否需要爲進度條創建新視圖? – Laxsnor 2011-04-13 20:42:09

+1

+1。 @Laxsnor,如果你已經完成了IB的所有工作,請確保你的'UIProgressView'是'FirstViewController'的'view'出口設置的'UIView'的子視圖。只要「UIProgressView」不隱藏或透明,您就不需要在您的應用程序中看到它。 Erik對'viewDidUnload'的評論是正確的。祝你好運。 – XJones 2011-04-13 22:20:10

+0

我應該在哪裏設置UIProgressView作爲FirstView控制器視圖插座的子視圖? 現在我在FirstViewController.h viewDidLoad中執行以下操作: [super viewDidLoad]; progressBar = [progressBar initWithProgressViewStyle:UIProgressViewStyleBar]; [self.view addSubview:progressBar]; [progressBar setFrame:CGRectMake(100,100,200,200)]; – Laxsnor 2011-04-14 04:03:41